Reputation: 175
I am using an API that sends a callback using json POST requests to a page that I set.
Example of the callback:
{
"order": {
"id": "5RTQNACF",
"created_at": "2012-12-09T21:23:41-08:00",
"status": "completed",
"total_btc": {
"cents": 100000000,
"currency_iso": "BTC"
},
"custom": "order1234",
}
I need to know how I can take each of these parameters and have them set as php variables.
Upvotes: 1
Views: 3697
Reputation: 28941
Use json_decode
to take this string and get an array:
$array = json_decode($jsonString, true);
If you really want these as individual php variables, use extract
:
extract($array); // now you have a local $order variable
echo $order['custom']; // etc...
extract($order); // now you have local variables $id, $status, etc
echo $id;
Example: http://3v4l.org/dqBjT
Keep in mind that extract
could potentially overwrite other local variables, it's not generally considered good practice. I would recommend accessing the data you need in the $array
.
Update: It sounds like you're struggling to access the posting JSON string in the first place. There are two possibilities:
If the data is being sent as a normal POST key/value pair, you can do print_r($_POST)
on your page, and you should see where the json string is at.
It's possible that it is being sent as raw POST data and not key/value pairs. In which case you need to look at file_get_contents("php://input");
. Try echoing that out. If you find your JSON string, then just set $jsonString = file_get_contents("php://input");
and proceed with the json_decode
next.
Upvotes: 3