tags:

views:

454

answers:

2

I'm building a script to verify a transaction receipt with Apple's itunesconnect site (iphone dev) and I can't figure out where's the error in my code. I want to get the "status" value.

Please help me to find what am I doing wrong:

 <?php
    include("config.php");

    $receipt = json_encode(array("receipt-data" => $_GET["receipt"]));
    $response_json = do_post_request($url, $receipt);
    $response = json_decode($response_json);

    //Here's where I try to get the "status" key but doesn't work

    echo $response['status']; 
    //echo $response->status; 

    function do_post_request($url, $data)
    {
       //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,$data);

     $result= curl_exec ($ch);
     curl_close ($ch);
     return $result;
    }

    ?>

and the answer to the iPhone looks like:

{"receipt":{"item_id":"327931059", "original_transaction_id":"1000000000074412", "bvrs":"1.0", "product_id":"sandy_01", "purchase_date":"2009-09-29 16:12:46 Etc/GMT", "quantity":"1", "bid":"com.latin3g.chicasexy1", "original_purchase_date":"2009-09-29 16:12:46 Etc/GMT", "transaction_id":"1000000000074412"}, "status":0}

but only "status":0 matters for now - Thank's

A: 

From the manual on json_decode()

Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

So either send TRUE for the 2nd parameter

$response = json_decode($response_json, true);

Or access the decoded JSON with object syntax

$response->status;

EDIT

Isolated test

$json = <<<JSON
{"receipt":{"item_id":"327931059", "original_transaction_id":"1000000000074412", "bvrs":"1.0", "product_id":"sandy_01", "purchase_date":"2009-09-29 16:12:46 Etc/GMT", "quantity":"1", "bid":"com.latin3g.chicasexy1", "original_purchase_date":"2009-09-29 16:12:46 Etc/GMT", "transaction_id":"1000000000074412"}, "status":0}
JSON;

$response = json_decode( $json );

echo $response->status;

$response2 = json_decode( $json, true );

echo $response2['status'];

Output is

00

Peter Bailey
Thanks, but still prints the whole array. Not getting the status value
Carlos
Then something else is wrong. I edited my answer to provide a test.
Peter Bailey
Can you see any error within the do_post_request() method or when I invoque it?, dont'n think so but your test code works only with you JSON sample, not the same with the $response_json varible
Carlos
Should the script return a value even if I comment the echo instruction? Because it's extrange to me that the script returns the complete string again but without any echo or print command...
Carlos
A: 

You should be able to use:

$response->status;

Passing an optional param of true to the json_decode function will bring the results back as an associative array.

Have you checked your response_json variable to check that the data is being deserialised correctly? i.e:

var_dump($response);

Which should yield:

object(stdClass)#1 (2) {
["receipt"]=>
  object(stdClass)#2 (9) {
    ["item_id"]=>
    string(9) "327931059"
    ["original_transaction_id"]=>
    string(16) "1000000000074412"
    ["bvrs"]=>
    string(3) "1.0"
    ["product_id"]=>
    string(8) "sandy_01"
    ["purchase_date"]=>
    string(27) "2009-09-29 16:12:46 Etc/GMT"
    ["quantity"]=>
    string(1) "1"
    ["bid"]=>
    string(22) "com.latin3g.chicasexy1"
    ["original_purchase_date"]=>
    string(27) "2009-09-29 16:12:46 Etc/GMT"
    ["transaction_id"]=>
    string(16) "1000000000074412"
  }
  ["status"]=>
  int(0)

}

richeym
The problem remains, thats why I thought that it was just a syntax error
Carlos
That's quite strange, the JSON data surely can't match what you've posted in that case. What does a var_dump() on $response_json give you?
richeym
responseString : {"receipt":{"item_id":"327931059", "original_transaction_id":"1000000000074715", "bvrs":"1.0", "product_id":"sandy_01", "purchase_date":"2009-09-29 23:16:35 Etc/GMT", "quantity":"1", "bid":"com.latin3g.chicasexy1", "original_purchase_date":"2009-09-29 23:16:35 Etc/GMT", "transaction_id":"1000000000074715"}, "status":0}string(13) "response_json" Although I'm new in php, this is odd
Carlos
I've got what I think is the problem behind this, the pho script returns data after I call this function $result= curl_exec ($ch); and it doesn't let me decode the answer, so should I put the post method in another file?
Carlos
Slightly related question: See that Etc/GMT in the original_purchase_date? PHPs timezone_identifiers_list() does NOT list that among the choices! (They have Etc/GMT+1 and so on, always with +/- and a number.) How do we reliably convert this to a time? I could drop the Etc/GMT, but what if Apple changes the time zone down the road? Maybe it's enough to set up a Date object with the date/time parts, then call setTZByID using the time zone part, and finally use $date->getDate() and so on to manipulate as needed. (This is important to me because I am doing a subscription-based transaction.)
Joe D'Andrea