Skip to content

Commit

Permalink
Prefer curl, but fall back to file_get_contents
Browse files Browse the repository at this point in the history
  • Loading branch information
greggilbert committed Dec 14, 2014
1 parent 982d271 commit b769409
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/Greggilbert/Recaptcha/CheckRecaptchaV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,31 @@ public function check($challenge, $response)
'response' => $response,
));

$curl = curl_init('https://www.google.com/recaptcha/api/siteverify?' . $parameters);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$curlResponse = curl_exec($curl);
$decodedResponse = json_decode($curlResponse, true);
$url = 'https://www.google.com/recaptcha/api/siteverify?' . $parameters;
$checkResponse = null;

// prefer curl, but fall back to file_get_contents
if(function_exists('curl_version'))
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$checkResponse = curl_exec($curl);
}
else
{
$checkResponse = file_get_contents($url);
}

if(is_null($checkResponse) || empty($checkResponse))
{
return false;
}

$decodedResponse = json_decode($checkResponse, true);
return $decodedResponse['success'];
}

Expand Down

0 comments on commit b769409

Please sign in to comment.