0

我正在构建一个脚本来验证与 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 很重要 - 谢谢

4

2 回答 2

0

来自json_decode()的手册

返回一个对象,或者如果可选的 assoc 参数为 TRUE,则返回一个关联数组。如果无法解码 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

于 2009-09-29T16:42:51.557 回答
0

You should be able to use:

$response->status;

Passing an optional param of true to the json_decode function will bring the results back as an associative array.

Have you checked your response_json variable to check that the data is being deserialised correctly? i.e:

var_dump($response);

Which should yield:

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)

}

于 2009-09-29T16:49:44.743 回答