dongqi8863 2009-09-29 16:21
浏览 40

php JSON管理:从解码的JSON对象中获取值

I'm building a script to verify a transaction receipt with Apple's itunesconnect site (iphone dev) and I can't figure out where's the error in my code. I want to get the "status" value.

Please help me to find what am I doing wrong:

 <?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;
    }

    ?>

and the answer to the iPhone looks like:

{"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}

but only "status":0 matters for now - Thank's

  • 写回答

2条回答 默认 最新

  • douyu7618 2009-09-29 16:42
    关注

    From the manual on json_decode()

    Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

    So either send TRUE for the 2nd parameter

    $response = json_decode($response_json, true);
    

    Or access the decoded JSON with object syntax

    $response->status; 
    

    EDIT

    Isolated test

    $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'];
    

    Output is

    00

    评论

报告相同问题?

悬赏问题

  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo