デコードされたオブジェクトからPHPの戻り値
質問
このコードではエラーがどこにあるか誰かが私に教えてくださいすることができ?私はリンゴに情報を送信するPHPスクリプト股関節を呼び出すために、モバイルiPhoneアプリケーションを使用しています。 Appleはその後、連想配列で複数の値を含むJSONオブジェクトを返します。
私は「状態」値に到達したいが、私は携帯電話でコードを実行するたびに、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秒 その他、各種/ GMT」、 "量": "1"、 "入札": "com.latin3g.chicasexy1"、 "original_purchase_date": "2009-10-05 午後十一時47分00秒その他、各種/ 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に RETURNTRANSFER
のオプションを設定してみてください文字列としてリクエストされた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);
...