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

Krlinhos's avatar

I can't recieve postfields data

Hello!

I try to make cURL with post method and postdata so:

private static function curl($url, $method, $postFields = null) {
        $curlHandler = curl_init();

        curl_setopt($curlHandler, CURLOPT_URL, $url);
        curl_setopt($curlHandler, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'X-API-Key:' . env('API_KEY')));
        curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curlHandler, CURLOPT_FRESH_CONNECT, true);

        if ($method == 'POST') {
            curl_setopt($curlHandler, CURLOPT_POST, 1);
            if ($postFields) {
                curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $postFields);
            }
        } elseif (in_array($method, ['PUT', 'PATCH', 'DELETE'])) {
            curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, $method);
        }

        $exec = curl_exec($curlHandler);
        curl_close($curlHandler);

        return json_decode($exec);
    }

but when in the function that supposedly recieves the datas, recieves nothing...

public function store(Request $request)
    {    
        return var_dump($request->all());
}

It's empty :(

Someone can help me?

Thanks!!

PD.- postFiel previously in other function I do http_build_query($data, '', '&')

0 likes
11 replies
Krlinhos's avatar

Thanks, but I would like to try to resolve it with curl, no other third party library. I wonder what is wrong I have that function :(

pmall's avatar

use guzzle, it simplify the use of curl

Krlinhos's avatar

@pmall Hello again!

I'm trying to use guzzle, I have installed it with composer so:

php composer.phar require guzzlehttp/guzzle:~6.0

Next, in my controller I have:

use GuzzleHttp\Client;

And in the function this:

$client = new \GuzzleHttp\Client();


        $response = $client->request('POST', 'http://api.mylocal.es', [
            'form_params' => [
                'field_name' => 'abc',
                'other_field' => '123',
                'nested_field' => [
                    'nested' => 'hello'
                ]
            ]
        ]);

        return $response;

But when execute it, I recieved an error:

FatalErrorException in Router.php line 1223: Class 'Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory' not found

Can you help me please?

PS.- I had installed guzzle previously for send email with Mandrill, and I had not problems.. So I dont know if it is bad installed or there are something more that I have to do for request.. :(

Krlinhos's avatar

Just fix it! :) Sorry, was a little mistake... hehehe

But now, when I print $request->all( ) in api, return { }

Why not recieve fields in the api? :S

I guess that it is for Content-Type, because when in use PostMan, if I choose "x-www-form-urlencoded", so I retrieve, but when choose form-data, not recieve.

I'm trying to set "application/x-www-form-urlencoded" so:

$client = new \GuzzleHttp\Client();

        $response = $client->request('POST', $url, [
            'form_params' => $data,
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded',
            ]
        ]);

But nothing...

Any idea?

thanks!!

pmall's avatar

Ive no idea but I think you are on the right path. Looks way better than the code in the original post.

jerimiah.short's avatar

@Krlinhos How did you fix your 'little mistake?' I just tried incorporating Guzzle as well and got the same error. :(

jerimiah.short's avatar

It might be a typo, but I'm not seeing $request in your code, it's $response. Also, there's quite a bit of flexibility in the $response object returned by Guzzle. You might look into that. :)

Please or to participate in this conversation.