简体   繁体   中英

Guzzle php: Unable to make a Guzzle post request

My Curl that works correctly looks like the following:

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $authenticateUrl);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($curl);

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    curl_close($curl);

However, my Guzzle is not working, which looks like the following:

    $client = new \GuzzleHttp\Client();
    $client->setDefaultOption('verifyPeer',false);

    $response = $client->post($authenticateUrl,array(
        'header' => $headers,
        'body'   => json_encode($user)
        )
    );

    $status = $response->getStatusCode();

What am I doing wrong in Guzzle post call?

        $request = $client->createRequest('POST', $authenticateURL);
        $request->setHeader('KEY', 'VALUE');
        $request->setBody('data');
        $response = $client->send($request);
        echo $response->getStatusCode();
        echo $response->getReasonPhrase();

Firstly I hope your $headers is an associative array

I you find the above solution to be not working try this method mentioned in the docs`

$client->get('/get', [
'headers' => [
    'User-Agent' => 'testing/1.0',
    'Accept'     => 'application/json',
    'X-Foo'      => ['Bar', 'Baz']
]
]);

I would like to know the error report in the browser because there are many mistakes which lead to the code not working.But try the first method I mentioned it is the first and foremost method I prefer to do instead of passing all the data in one line instruction.This way it makes me debug easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM