2

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)
Praba
  • 31
  • 3
  • Any solution? I have the same issue. – janosdupai Apr 03 '20 at 15:56
  • Same issue here, blank response. Did you ever solve this? – Steve Winn Nov 04 '20 at 13:33
  • Were you validating the sandbox receipts, or the production ones? Since your code can handle both, you could've got the redirect to sandbox and get an empty response there. I am getting a similar problem in sandbox, wondering if this will reproduce for production? – Roman Kozak May 19 '21 at 08:30

1 Answers1

0

Add error checking might be something there,right after curl_exec

if (curl_errno($ch)) {
    $error_message = curl_error($ch);
    echo $error_message;
}
Mihai
  • 26,325
  • 7
  • 66
  • 81