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

Kenny.kk's avatar

Problem with posted data using cUrl

Hello Guys, We are posting data from an external website to our Laravel application using curl. But I don't why, we cannot get the posted data in our Laravel app using "$request->all()" or "$request->getContent()" or "file_get_contents(php://input)". CORS is enabled and allowed for all domains, we do not have any problem with CsrfToken. Codes are given below, any help appreciated.

Curl code in external site :

$fields = http_build_query($_POST);
$url = 'http://www.domain.com/api/v1/?api_token=token_goes_here';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$result = curl_exec($ch);

Route code for api :

Route::group(['prefix' => 'api', 'middleware' => 'auth:api'], function() {
    Route::any('/v1', ['as' => 'apiV1', 'uses' => '\App\Http\Controllers\Api\ApiV1Controller@index']);
});

When I dd($request->all()) in index method I get the respose below. Token is verfied, auth working properly but all I get is api_token variable which is coming via get method.

array:1 [▼
  "api_token" => "token_goes_here"
]

To check if data sent, I created a simple php file in "public/api/v1" folder on the same app and I got all fields using "die(var_dump($_POST));". Clearly there is something that I don't know about laravel.

0 likes
1 reply
Kenny.kk's avatar

[ SOLVED ]

Hello again guys,

After reading some articles and watching some videos, I updated the curl code above and it seems like there is nothing special with Laravel about the problem I had. Everything works perfectly now.

Here is new cUrl code sending request :

$fields = http_build_query($_POST);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$result = curl_exec($ch);

Route code for api :

Route::group(['prefix' => 'api', 'middleware' => 'auth:api'], function() {
    Route::any('/v1', ['as' => 'apiV1', 'uses' => '\App\Http\Controllers\Api\ApiV1Controller@index']);
});

Index Method :

    public function index(Request $request) {
        dd($request->all());
    }

Please or to participate in this conversation.