2

我正在使用下面的代码使用 PHP 验证苹果收据,使用 CURL 我正在调用苹果服务器并尝试获取响应。此代码工作正常,但有时苹果 JSON 响应为空,我也没有收到任何错误消息. 它只是空白。

这是使用 PHP 验证苹果收据的唯一方法吗?或者请在我的代码中更正我犯的错误是什么,因为当我尝试调试时,我得到的是空响应,但如果我发送 10 个请求,7 个正在给出响应,3 个返回空白/空,这个问题并非一直存在。

谢谢。

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)
4

1 回答 1

0

在 curl_exec 之后可能会添加错误检查

if (curl_errno($ch)) {
    $error_message = curl_error($ch);
    echo $error_message;
}
于 2020-11-04T13:32:41.220 回答