I'm trying to get a specific value from JSON decode using PHP.
Basically I have an array being returned by JSON which looks like this:
{
  "created": 1326853478,
  "livemode": false,
  "id": "evt_00000000000000",
  "type": "charge.failed",
  "object": "event",
  "request": null,
  "pending_webhooks": 1,
  "api_version": "2015-10-16",
  "data": {
    "object": {
      "id": "ch_00000000000000",
      "object": "charge",
      "amount": 3000,
      "amount_refunded": 0,
      "application_fee": null,
      "balance_transaction": "txn_00000000000000",
      "captured": true,
      "created": 1449010896,
      "currency": "gbp",
      "customer": "cus_00000000000000",
      "description": null,
      "destination": null,
      "dispute": null,
      "failure_code": null,
      "failure_message": null,
      "fraud_details": {
      },
      "invoice": "in_00000000000000",
      "livemode": false,
      "metadata": {
      },
      "paid": false,
      "receipt_email": null,
      "receipt_number": null,
      "refunded": false,
      "refunds": {
        "object": "list",
        "data": [
        ],
        "has_more": false,
        "total_count": 0,
        "url": "/v1/charges/ch_17DN7sFMZc35gfghfghddd8A2gG0Ap8Hg/refunds"
      },
      "shipping": null,
      "source": {
        "id": "card_00000000000000",
        "object": "card",
        "address_city": null,
        "address_country": null,
        "address_line1": null,
        "address_line1_check": null,
        "address_line2": null,
        "address_state": null,
        "address_zip": null,
        "address_zip_check": null,
        "brand": "Visa",
        "country": "US",
        "customer": "cus_00000000000000",
        "cvc_check": "pass",
        "dynamic_last4": null,
        "exp_month": 12,
        "exp_year": 2033,
        "funding": "credit",
        "last4": "4242",
        "metadata": {
        },
        "name": "useremail@yahoo.co.uk",
        "tokenization_method": null
      },
      "statement_descriptor": null,
      "status": "succeeded"
    }
  }
}
So, what I need to do is to get the value of "name": "useremail@yahoo.co.uk", which is the users email so i can do some database altering on my side.
I created this code:
$input = @file_get_contents("php://input");
$event_json = json_decode($input);
// Do something with $event_json
//print_r($event_json);
$user = $event_json->name;
http_response_code(200); // PHP 5.4 or greater
$file = fopen("file.txt","w");
fwrite($file,$user);
fclose($file);
So, as you can see above, the variable $user should be written into the file.txt but nothing's being written into the file.txt.
I went ahead and tested the entire array and tried to write the entire array in the file.txt and it works absolutely fine.
$file = fopen("file.txt","w");
fwrite($file,$input);
fclose($file);
I also tried to get name value from the JSON array like so:
$json = json_decode($input, true);
$user = $json['name'];
and Still didn't get anything written in my file.txt!
Could someone please advise on this issue?
am I doing something wrong or missing something?
