Home > Blockchain >  cUrl not returning response - response is BOOL
cUrl not returning response - response is BOOL

Time:09-16

I am trying to verify my recapture with google, but I am getting a response of null

I copy and paste the information to Postman and sent the request and I received a positive response.

I copied the link in my browser as a GET request and I also got a response.

I am not sure what causing this, as all information is correct.

here is my code.

// set API URL
$url = 'https://www.google.com/recaptcha/api/siteverify';
// Collection object
$data = [
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //<--- my reCaptcha secret key
  'response' => $_POST['recaptcha']
];
// Initializes a new cURL session
$curl = curl_init($url);
// Set the CURLOPT_RETURNTRANSFER option to true
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the CURLOPT_POST option to true for POST request
curl_setopt($curl, CURLOPT_POST, true);
// Set the request data as JSON using json_encode function
curl_setopt($curl, CURLOPT_POSTFIELDS,  json_encode($data));

// Execute cURL request with all previous settings
$response = curl_exec($curl);
// Close cURL session
curl_close($curl);
echo 'the response was ' . $response . PHP_EOL;

I saw this but didn't help me. PHP cURL not return a response, POSTMAN returns response

CodePudding user response:

Because it is an HTTPS url you may need to add:

curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);

I do not think Google wants the post data as JSON.
Google is expecting an array. Try removing the json_encode().

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

If Google (unlikely) wants a JSON request, you need to add Content-Type: application/json to the HTTP header.
By adding this header curl will put the "post data" in the body and will not use the default POST header: application/x-www-form-urlencoded

$request = array();
$request = 'Content-Type: application/json';
curl_setopt($curl, CURLOPT_HTTPHEADER, $request);

I do not understand why there was no response. I would have at least expected:

{
  "success": false,
  "error-codes": [
    "missing-input-secret"
  ]
} 

You may want to add this code after your curl_exec($curl)
This should give you all the details of you request and Google's response.

$response = curl_exec($curl);
$info = rawurldecode(var_export(curl_getinfo($curl),true));
echo "\n<br>$info<br>\n";

If you want to see your outgoing request header (recommended) add this option and the header will be in the curl_getinfo:

curl_setopt($curl, CURLINFO_HEADER_OUT, true);

I'm a bit concerned about a NULL being returned. Was the word NULL returned? Or did you see nothing for $response in your string?
curl does not return a NULL. So maybe it was a false. Meaning there is likely a typo. echo does not show boolean false if $response was false you would not see it. Maybe add a

if($response == false){echo "curl failed<br>";}

Even if it failed, all the above is still true.

CodePudding user response:

Are you using your local machine in linux? If that so, it is probably because you don't have curl installed in your system. So do

sudo apt install php{version}-curl
  • Related