【问题标题】:PHP return value from decoded objectPHP从解码对象返回值
【发布时间】:2009-10-06 00:01:24
【问题描述】:

有人能告诉我这段代码的错误在哪里吗?我使用移动 iphone 应用程序调用一个 php 脚本,它将向苹果发送信息。然后,Apple 将返回一个 JSON 对象,其中包含关联数组中的多个值。

我想达到“状态”值,但每次我在手机中运行代码时,php 脚本都会向我发送完整的苹果返回字符串。在 XCode 调试器中,接收到的字符串如下所示:

[调试]...响应字符串: {“收据”:{“item_id”:“328348691”, “original_transaction_id”:“1000000000081203”, “bvrs”:“1.0”,“product_id”:“julia_01”, "purchase_date":"2009-10-05 23:47:00 等/GMT", "数量":"1", “出价”:“com.latin3g.chicasexy1”, "original_purchase_date":"2009-10-05 23:47:00 等/格林威治标准时间", "transaction_id":"1000000000081203"}, “状态”:0}

但我在字符串中唯一关心的是“状态”值。 我已经查看了内部文档,但找不到解决方案。我是 php 新手,但这太长了。这里是脚本:

<?php
//The script takes arguments from phone's GET call
$receipt = json_encode(array("receipt-data" => $_GET["receipt"]));

//Apple's url
$url = "https://sandbox.itunes.apple.com/verifyReceipt";

//USe cURL to create a post request
//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,$receipt);

$result = curl_exec ($ch);
//Here the code "breaks" and return the complete string (i've tested that)
//and apparently doesn't get to the json_decode function (i think something's wrong there, so code breaks here)
curl_close ($ch);

$response = json_decode($result);   

echo $response->{'status'};


?>

即使我没有在末尾添加任何回声,脚本仍然会返回一个完整的字符串(对我来说很奇怪)

如果我再次坚持另一个问题,请提前致谢并道歉

【问题讨论】:

    标签: php iphone json decode


    【解决方案1】:

    尝试将RETURNTRANSFER 选项设置为 1,这样您就可以将请求的 URL 的输出捕获为字符串。看来cURL的默认行为是将结果直接输出到浏览器:

    ...
    $ch = curl_init();
    
    // set the target url
    curl_setopt($ch, CURLOPT_URL,$url);
    
    // howmany parameter to post
    curl_setopt($ch, CURLOPT_POST, 1);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // <---- Add this
    
    
    // the receipt as parameter
    curl_setopt($ch, CURLOPT_POSTFIELDS,$receipt);
    ...
    

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 2018-06-01
      • 1970-01-01
      • 2020-01-29
      • 2011-04-05
      • 2013-08-03
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多