Reputation: 25
I'm new to PHP and JSON, so hoping someone can help me.
I have a PHP which a 3rd party performs a POST against, delivering JSON data. An example of the data sent would be:
> { "created": 1326853478, "livemode": false, "id":
> "evt_00000000000000", "type": "charge.succeeded", "object":
> "event", "data": {
> "object": {
> "id": "ch_00000000000000",
> "object": "charge",
> "created": 1366838716,
> "livemode": false,
> "paid": true,
> "amount": 1000,
> "currency": "gbp",
> "refunded": false,
> "fee": 59,
> "fee_details": [
> {
> "amount": 59,
> "currency": "usd",
> "type": "stripe_fee",
> "description": "Stripe processing fees",
> "application": null,
> "amount_refunded": 0
> }
> ],
> "card": {
> "object": "card",
> "last4": "4242",
> "type": "Visa",
> "exp_month": 2,
> "exp_year": 2016,
> "fingerprint": "cniJDyeew54ashW6Iyr",
> "country": "US",
> "name": "wibble3",
> "address_line1": null,
> "address_line2": null,
> "address_city": null,
> "address_state": null,
> "address_zip": null,
> "address_country": null,
> "cvc_check": "pass",
> "address_line1_check": null,
> "address_zip_check": null
> },
> "captured": true,
> "failure_message": null,
> "amount_refunded": 0,
> "customer": null,
> "invoice": null,
> "description": null,
> "dispute": null
> } } }
I would like to be able to extract certain items to then process depending on these values.
Using this code, I can extract the 'type' easily enough:
$body = @file_get_contents('php://input');
$event_json = json_decode($body);
print $event_json->{'type'};
But I cannot extract "fee" for example, which seems to be a level below where I am successfully extracting a value from, or "description" which is another level down again.
I would like to be able to extract only certain items from this JSON string (for example, Type, Fee, Description, Last4), but I am completely lost. I appreciate this is likely a fairly basic task, but I'm getting no where.
Any help, gratefully received!
Upvotes: 0
Views: 513
Reputation: 12834
you have multi levels of objects so use:
echo($event_json->data->object->fee);
Basically you are echoing fee
, which is a member of the object name object
, which in turn is the attribute of an object named data
, which again in turn is a member of your $event_json
object.
Upvotes: 1
Reputation: 8939
You can quickly acheive this in following manner:
$body = @file_get_contents('php://input');
$event_json = json_decode($body, true);
print $event_json['data']['object']['fee'];
Upvotes: 2
Reputation: 778
When you do a json_decode, you get an array of this data. So, you can access data like this:
$event_json['data']['object']['fee']
good luck
Upvotes: 0
Reputation: 324820
First, I would suggest passing true
as the second argument to json_decode
. This results in an associative array rather than an object, which makes it a little easier to work with.
Then, you can do this:
$fee = $event_json['data']['object']['fee'];
You can chain these square brackets as many times as needed to delve deeper into the array.
Upvotes: 2