【问题标题】:verifying apple receipt with PHP is not working sometime使用 PHP 验证苹果收据有时不起作用
【发布时间】:2020-07-06 16:54:38
【问题描述】:

我正在使用下面的代码使用 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)

【问题讨论】:

  • 有什么解决办法吗?我有同样的问题。
  • 同样的问题,空白回复。你解决过这个问题吗?
  • 您是在验证沙盒收据还是生产收据?由于您的代码可以同时处理这两种情况,因此您可以重定向到沙箱并在那里获得空响应。我在沙盒中遇到了类似的问题,想知道这是否会在生产中重现?

标签: php php-curl receipt-validation in-app-purchase-receipt


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    相关资源
    最近更新 更多