Struggling with recaptcha v2 and form submission

Using curl instead of file_get_contents (should you, like me, want file_get_contents to be disabled in the server settings)

$post_data = "secret=__your_secret_key__&response=".
   $_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR'] ;

$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, 
   array('Content-Type: application/x-www-form-urlencoded; charset=utf-8', 
   'Content-Length: ' . strlen($post_data)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
$googresp = curl_exec($ch);       
$decgoogresp = json_decode($googresp);
curl_close($ch);

if ($decgoogresp->success == true)
    {
    // Success
    }

I found that sometimes, depending on the PHP version/config, accessing an object directly won't work, so use json_decode().

/* $response object returned from https://www.google.com/recaptcha/api/siteverify via which ever method you use */

$obj = json_decode($response);
if($obj->success == true)
{
    //passes test
}
else
{
    //error handling
}