我正在构建一个脚本来验证Apple的itunesconnect站点(iphone dev)的交易收据,我无法弄清楚我的代码中的错误在哪里。我希望获得“状态”值。
请帮我找出我做错了什么:
<?php
include("config.php");
$receipt = json_encode(array("receipt-data" => $_GET["receipt"]));
$response_json = do_post_request($url, $receipt);
$response = json_decode($response_json);
//Here's where I try to get the "status" key but doesn't work
echo $response['status'];
//echo $response->status;
function do_post_request($url, $data)
{
//initialize cURL
$ch = curl_init();
// set the target url
curl_setopt($ch, CURLOPT_URL,$url);
// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);
// the receipt as parameter
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$result= curl_exec ($ch);
curl_close ($ch);
return $result;
}
?>
并且iPhone的答案如下:
{"receipt":{"item_id":"327931059", "original_transaction_id":"1000000000074412", "bvrs":"1.0", "product_id":"sandy_01", "purchase_date":"2009-09-29 16:12:46 Etc/GMT", "quantity":"1", "bid":"com.latin3g.chicasexy1", "original_purchase_date":"2009-09-29 16:12:46 Etc/GMT", "transaction_id":"1000000000074412"}, "status":0}
但只有“状态”:0现在很重要 - 谢谢
答案 0 :(得分:0)
返回一个对象或者是可选的 assoc参数为TRUE,an 而是返回关联数组。 如果json不能返回NULL 被解码或编码数据是 比递归限制更深。
因此要么为第二个参数发送TRUE
$response = json_decode($response_json, true);
或使用对象语法
访问已解码的JSON$response->status;
隔离测试
$json = <<<JSON
{"receipt":{"item_id":"327931059", "original_transaction_id":"1000000000074412", "bvrs":"1.0", "product_id":"sandy_01", "purchase_date":"2009-09-29 16:12:46 Etc/GMT", "quantity":"1", "bid":"com.latin3g.chicasexy1", "original_purchase_date":"2009-09-29 16:12:46 Etc/GMT", "transaction_id":"1000000000074412"}, "status":0}
JSON;
$response = json_decode( $json );
echo $response->status;
$response2 = json_decode( $json, true );
echo $response2['status'];
输出
00
答案 1 :(得分:0)
你应该可以使用:
$response->status;
将一个true的可选参数传递给json_decode函数会将结果作为关联数组返回。
您是否检查过response_json变量以检查数据是否正确反序列化?即:
var_dump($response);
哪个应该产生:
object(stdClass)#1 (2) {
["receipt"]=>
object(stdClass)#2 (9) {
["item_id"]=>
string(9) "327931059"
["original_transaction_id"]=>
string(16) "1000000000074412"
["bvrs"]=>
string(3) "1.0"
["product_id"]=>
string(8) "sandy_01"
["purchase_date"]=>
string(27) "2009-09-29 16:12:46 Etc/GMT"
["quantity"]=>
string(1) "1"
["bid"]=>
string(22) "com.latin3g.chicasexy1"
["original_purchase_date"]=>
string(27) "2009-09-29 16:12:46 Etc/GMT"
["transaction_id"]=>
string(16) "1000000000074412"
}
["status"]=>
int(0)
}