我正在使用此代码对苹果的服务器执行应用内收据验证。运行此脚本的时间大约为 50%,一切正常。然而,在另外 50% 的时间里,我得到一个空的 json 响应。我没有任何例外。任何想法为什么?
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$meta = stream_get_meta_data($fp);
print_r($meta);
stream_set_timeout($fp, 200);
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
$appleURL = "https://buy.itunes.apple.com/verifyReceipt";
$receipt = json_encode(array("receipt-data" => $receiptdata));
$response_json = do_post_request($appleURL, $receipt);
$response = json_decode($response_json);
var_dump($response_json);
这就是我 var_dump 流元数据时得到的
array(10) { ["wrapper_data"]=> array(2) { ["headers"]=> array(0) { } ["readbuf"]=> resource(6) of type (stream) } ["wrapper_type"]=> string(4) "cURL" ["stream_type"]=> string(4) "cURL" ["mode"]=> string(2) "rb" ["unread_bytes"]=> int(0) ["seekable"]=> bool(false) ["uri"]=> string(42) "https://buy.itunes.apple.com/verifyReceipt" ["timed_out"]=> bool(false) ["blocked"]=> bool(true) ["eof"]=> bool(false) }