题
有人可以告诉我在哪里,在这个代码中的错误?我用的是移动的iPhone应用程序调用PHP脚本塔将信息发送到苹果。苹果然后将在关联数组返回包含几个值JSON对象。
我想达到的“状态”的价值,但我每次运行在手机上的代码时,PHP脚本将我整个苹果的返回的字符串。在Xcode调试器所接收到的字符串如下所示:
[DEBUG] ... responseString: { “收据”:{ “ITEM_ID”: “328348691”, “original_transaction_id”: “1000000000081203”, “bvrs”: “1.0”, “PRODUCT_ID”: “julia_01”, “购买日期”:“2009-10-05 23点47分零零秒 ETC / GMT”, “量”: “1”, “出价”:“com.latin3g.chicasexy1” “original_purchase_date”:“2009-10-05 23时47分零零秒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如果我从另一个问题又来坚持
解决方案
尝试 RETURNTRANSFER
选项设置为1,以便你可以捕获从请求的URL作为字符串输出。似乎卷曲的默认行为是输出结果直接到浏览器:
...
$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);
...
不隶属于 StackOverflow