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!