Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen waweru's avatar

post data to another system using api with curl request in laravel

i have 2 web apps whereby on adding data on one system,SYSTEM A,products table in its db, it automatically adds data to the other system table,SYSTEM B, with similar name in the database.i am using a curl request to send data to the api which then posts data to the table of the database.I have added everything but still the request doesnt function.i havent understood where i have gone wrong with my code. here is my save function in system A

public function saveproduct(Request $request)
 {
   if($request->ismethod('post')){
    $data=$request->all();
	$product=new Product;
    $product->name=$data['name'];
	$product->description=$data['description'];
    $product->save();
    $productsdata=$data;
    $url="http://systemb/api/push_products";
    $ch=curl_init($url);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$productsdata);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array('content-Type','application/json'));
    $result=curl_exec($ch);
            // dd($productsdata);die();
    curl_close($ch);
 }

}

here is the post route in the systemb on the api.php

	Route::namespace('api')->group(function(){
  		  Route::post('/push_products', 'APIFcController@savefromsystemA');
	});

here is the savefromsystemA function in the APIFcController

	public function savefromsystemA(Request $request)
		{
 if($request->ismethod('post')){
    $data=$request->all();
	$product=new Product;
    $product->name=$data['name'];
	$product->description=$data['description'];
    $product->save();
   }

}

0 likes
27 replies
tykus's avatar

Are you aware of the HTTP Client that ships with Laravel?

Http::asJson()->post('http://systemb/api/push_products', $request->all());

Why is this ismethod method on the Request class; why do you need to check if the request method is POST at all; that is what the Route defines!?!

Sinnbeck's avatar

@stephen waweru As Tykus pointed out case matters. isMethod with an uppercase M. It might work on your local computer, but once you put it live it will most likely break

if($request->isMethod('post')){
stephen waweru's avatar

@tykus my bad i have changed it to isMethod but the data get created but its isnt posted on the api

tykus's avatar

@stephen waweru using cUrl, or the Http client?

Anyway, as I mentioned above; why is it necessary to check the Request method at all; your route defines the method that the application will respond to?

tykus's avatar

@stephen waweru why is this necessary at all?

if($request->isMethod('post')){
stephen waweru's avatar

@tykus i have removed it in the public function savefromsystemA but still it doesnt save

  public function savefromsystemA(Request $request)
	{
$data=$request->all();
$product=new Product;
$product->name=$data['name'];
$product->description=$data['description'];
$product->save();

}

tykus's avatar

@stephen waweru are you actually hitting that method at all; does the Request make it into your B application???

tykus's avatar

@stephen waweru what is the current implementation; how are you making the Request to your other application now?

stephen waweru's avatar

@tykus i am using this line $ch=curl_init($url); to initialize the url.then start on the request to read the method

tykus's avatar

@stephen waweru like I showed you before:

public function saveproduct(Request $request)
{
	$product=new Product;
    $product->name=$request['name'];
	$product->description=$request['description'];
    $product->save();

    \Http::asJson()->post('http://systemb/api/push_products', $request->all());

    // return ?????
}

This is mimimum viable solution; you should consider validating the Request payload as well as error handling the outbound Request

1 like
stephen waweru's avatar

@tykus i am gettig an error "Call to undefined method GuzzleHttp\Client::asJson()"...i tried using http but on upgrade to laravel 8 one uses GuzzleHttp\Client

tykus's avatar

@stephen waweru use Laravel's Http client; it has the asJson method:

use Illuminate\Support\Facades\Http;

Http::asJson()->post('http://systemb/api/push_products', $request->all());
stephen waweru's avatar

@tykus am getting an error Class 'Illuminate\Support\Facades\Http not found yet have installed the GuzzleHttp\Client package

tykus's avatar

@stephen waweru what do you mean

yet have installed the httpclient

What Laravel version are you using?

tykus's avatar

@stephen waweru why; Laravel 8 comes with Http client out of the box?

stephen waweru's avatar

@tykus my project version is laravel version 5.8,i had to check it again..i updated http client to 7.4.1

ehab.aboshehab's avatar
Level 3

@stephen

Try this out : And by the way. you do not need to check the request method type ... your destination route is defining it as a post already ...

use Illuminate\Support\Facades\Http;
.
.
.
$url = 'http://systemb/api/push_products';
$body = array('name' => $data['name'], 'description' => $data['description']);

$response = Http::post($url, $body);

$jsonResponse = $response->object();
1 like
stephen waweru's avatar

@ehab.aboshehab here it is

                 $url="http://systemb/api/push_products";
                $body = array(
                'name' => $data['name'], 
                'description' => $data['description'], 
                $response = Http::post($url, $body);
                $jsonResponse = $response->object();

Please or to participate in this conversation.