views:

147

answers:

1

Hi,

Can someone please tell me where is the error in this code?, I use a mobile iphone application to call a php script tha will send information to apple. Apple then will return a JSON object containing several values in an associative array.

I want to reach the 'status' value but every time I run the code in the phone, the php script sends me the complete apple's returned string. In the XCode debugger the received string looks like:

[DEBUG]... 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 Etc/GMT", "transaction_id":"1000000000081203"}, "status":0}

but the only piece I care in the string is the "status" value. I've already looked inside documentation but can't find the solution. I'm new in php but this getting too long. Here the script:

<?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'};


?>

Even if I don't put any echo at the end, the script still returns a complete string (odd to me)

Thank's in advance and apollogies if I insist again from another question

+2  A: 

Try setting the RETURNTRANSFER option to 1 so you can capture the output from the requested URL as a string. It seems that the default behaviour of cURL is to output the result directly to the browser:

...
$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);
...
karim79
million thanks!!!
Carlos