1

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

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

[调试] ... responseString:{“receipt”:{“item_id”:“328348691”,“original_transaction_id”:“1000000000081203”,“bvrs”:“1.0”,“product_id”:“julia_01”,“purchase_date”: "2009-10-05 23:47:00 Etc/GMT", "quantity":"1", "bid":"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'};


?>

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

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

4

1 回答 1

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);
...
于 2009-10-06T00:14:55.333 回答