PHP从解码对象返回值

时间:2009-10-06 00:01:24

标签: php iphone json decode

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

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

  

[DEBUG] ... responseString:   { “收据”:{ “ITEM_ID”: “328348691”,   “original_transaction_id”: “1000000000081203”,   “bvrs”:“1.0”,“product_id”:“julia_01”,   “purchase_date”:“2009-10-05 23:47:00   Etc / GMT“,”数量“:”1“,   “出价”:“com.latin3g.chicasexy1”   “original_purchase_date”:“2009-10-05   23:47:00 Etc / GMT“,   “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'};


?>

即使我最后没有放回任何回音,脚本仍然会返回一个完整的字符串(奇怪的是我)

如果我再次坚持另一个问题,请提前感谢和apollogies

1 个答案:

答案 0 :(得分:2)

尝试将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);
...