I've got this connection to an API that works perfect on Postman, it returns a Json showing what we just sent, in this example it returns {"id":9}.
However, when I use the cURL code generated by Postman it always returns nothing for the same data.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://us-central1-mitocondria-856e6.cloudfunctions.net/api/createUser",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"id\":9}",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer XXXX",
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
The code above returns nothing.
var_dump($response)
returns NULL var_dump($err)
returns string(0) "" And print_r(curl_getinfo($curl))
returns
Array ( [url] => https://us-central1-mitocondria-856e6.cloudfunctions.net/api/createUser/ [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [redirect_url] => [primary_ip] => [certinfo] => Array ( ) [primary_port] => 0 [local_ip] => [local_port] => 0 )
I also changed the raw json data with json_encode( array("id" => 8) )
and had the same outcome.
What else should I try? or there's an error I'm missing? Please help!
1- Please mind your ',' in the last parameter of the curl array.
2- Please try and add
CURLOPT_POST=> 1, // Means you have post data CURLOPT_POSTFIELDS => $curl_data
'
It was a stupid error, and it had nothing to do with the code.
One of the first things I checked was that PHP had cURL module installed, but I didn't notice that it was disabled on php.ini, so the error showed in the only log I didn't checked, apache error.log.
After the module was enabled it worked like a spell.
Thank you all that tried to help me in this.
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.