Rana Faiz Ahmad
Rana Faiz Ahmad

Reputation: 1698

Getting a blank response trying to verify google recaptcha from google's api using curl with PHP

Here is the documentation of the API I am trying to use: https://developers.google.com/recaptcha/docs/verify

I am using curl with PHP to send a POST request and get a response back but I keep getting an empty response. Below is the code I am using:

$fields = [
    'response' => $token,
    'secret' => $secretKey
];

//url-ify the data for the POST
$fields_string = http_build_query($fields);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

//execute post and return the results
$response = curl_exec($ch);

curl_close($ch);

echo $response;

The value of $response is always blank.

I tried to send the same data using curl from command line and using the Postman app and it always work. For some reason this code isn't working and I need some help.

Upvotes: 1

Views: 676

Answers (1)

Abdul Razak Zakieh
Abdul Razak Zakieh

Reputation: 764

Put this line of code to turn off SSL certificate checking:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false)

Upvotes: 1

Related Questions