verifying apple receipt with PHP is not working sometime

I'm using below code to verify apple receipt using PHP, using CURL i'm calling apple server and trying to get response.This code is working fine but sometimes apple JSON response is coming empty and i'm not getting any error message also. It just goes blank.


Is this is the only method to verify apple receipt using PHP? or please correct me in my code what is the mistake I made because when I try to debug this i'm getting empty response but this issue is not all time if I send 10 request 7 are giving response and 3 are returning blank/empty.

Thanks.


function subscription_curl ($request)
{
ini_set('max_execution_time', 0);
  $decodeResponse = "";  
  $appleurl = "https://buy.itunes.apple.com/verifyReceipt"; // for production  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_URL, $appleurl);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  $encodedResponse = curl_exec($ch);//Encoded apple response
  curl_close($ch);
  $decodeResponse = json_decode($encodedResponse, TRUE);//Decoded apple response
  $applestatus1 = $decodeResponse['status'];
  if($applestatus1 == 21007)
  {
  $appleurl = "https://sandbox.itunes.apple.com/verifyReceipt"; // for sandbox  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_URL, $appleurl);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  $encodedResponse = curl_exec($ch);//Encoded apple response
  curl_close($ch);
  $decodeResponse = json_decode($encodedResponse, TRUE);//Decoded apple response
  }
  return array($encodedResponse, $decodeResponse);
}

$decodeResponse1 = subscription_curl($request); // Call curl function to send request to apple  
$encodedResponse = $decodeResponse1[0];
$decodeResponse = $decodeResponse1[1];
print_r($decodeResponse)

Replies

> if I send 10 request 7 are giving response and 3...


Are all requests being sent with the same receipt? Or, at least, do you find that the same receipt will sometimes give one result and sometimes give a different result?

Did your verification work in the past? My PHP-based receipt verification server has been working well for the past weeks, but suddenly stopped working yesterday. I can still verify receipts by bypassing my server and sending them directly to Apple, but that exposes my revenue to man-in-the-middle attacks.

I have found that I had to implement a retry loop on my server because of connection problems that appear to originate on Apple's end. 30% sounds about right for the failure rate that I have been seeing.


I have sometimes had to retry the same call to verifyReceipt up to 4 times before it would successfully return.