verifying apple receipt with PHP is not working sometime

Viewed 307

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)
1 Answers

Add error checking might be something there,right after curl_exec

if (curl_errno($ch)) {
    $error_message = curl_error($ch);
    echo $error_message;
}
Related