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

delyn12's avatar

Api Curl in laravel

Good day everyone, I tried to use Curl in laravel to post a form for instance

$url =   "https://invoice.bigeweb.com/license";
        $response = Http::get("https://invoice.bigeweb.com/license");


        dd($response);
    }

This is return a null response. And when I tried the post method and I tried to dd my response, am getting 419 csrf token expired. My question is that when using CURL or HTTP in Laravel API, how do I get the post request on the live server I want my API to connect with "$url". And if the url is to get data from the second application, how do I process the data to return the response to my requesting application.

0 likes
3 replies
LaryAI's avatar
Level 58

To make a POST request using Laravel's HTTP client, you can use the post method and pass in the URL and data as parameters. Here's an example:

$url = "https://example.com/api";
$data = [
    'name' => 'John Doe',
    'email' => '[email protected]',
];

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

dd($response->json());

In this example, we're making a POST request to https://example.com/api with the data ['name' => 'John Doe', 'email' => '[email protected]']. The dd function is used to dump and die the response as a JSON object.

If you're getting a 419 CSRF token expired error, it means that your request is missing a CSRF token. You can add the CSRF token to your request by including it in the data array:

$data = [
    '_token' => csrf_token(),
    'name' => 'John Doe',
    'email' => '[email protected]',
];

If you're trying to get data from a second application, you can use the get method instead of the post method:

$url = "https://example.com/api/data";
$response = Http::get($url);

dd($response->json());

In this example, we're making a GET request to https://example.com/api/data and dumping the response as a JSON object. You can process the data in the response as needed before returning it to the requesting application.

delyn12's avatar

@LaryAI Thank you so much for this response. Just to clarify, from the second application which I am sending my post request, my code will be From server B with the url "https://example.com/api";

Route::post('/api', function(){
dd($request->all());
//or inside my controller I can do anything with the request right. and return the response.
});
delyn12's avatar

When I tried the above code, from my local host I wrote the below code

$response = Http::get("https://invoice.bigeweb.com/api/key")
dd($response->body());

I was able to get the json result which I returned from "https://invoice.bigeweb.com/api/key" and the code I used there was

Route::get('api/key', function(){
$data = DB::table('invoices')->get();
return response()->json(['data' => $data])
});

This works good. But now my problem is the post method which I keep getting different errors.

from local host I have

$data = [
    'name' => 'delyn',
    'email' => '[email protected]',
];

$response = Http::post("https://invoice.bigeweb.com/api/key", $data);

dd($response->json());

and from "https://invoice.bigeweb.com/api/key" website I have the code as

Route::post('api/key', function(Request $request){
dd($request->all());
});

but am getting a result forbidden. 403

even without the dd($request->all());  I am still getting error 403. Please can anyone help me with the code to accept and process the request on my live server?

Please or to participate in this conversation.