0

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?

H.HISTORY
  • 520
  • 9
  • 28
  • Your property `name` is nested inside other objects. Add [error reporting](http://php.net/manual/en/function.error-reporting.php) at the top of your file(s): `ini_set("display_errors", 1); error_reporting(E_ALL);` and you should get an error about: `$event_json->name;` – Rizier123 Dec 02 '15 at 10:47
  • try doing this `$user = $json['data']['name'];` – mega6382 Dec 02 '15 at 10:48
  • @mega6382, still nothing I'm afraid. – H.HISTORY Dec 02 '15 at 10:50
  • @Rizier123, I'm not viewing the file in the browser to be able to see the errors. it is writing whatever the JSON returns in the text file as I mentioned in my question. well, it only writes the entire array at the moment which is not ideal. – H.HISTORY Dec 02 '15 at 10:52
  • 1
    `$user = $event_json->data->object->source->name;` – Steve Dec 02 '15 at 10:54
  • @Steve, that did it mate. thanks ever so much. – H.HISTORY Dec 02 '15 at 10:57
  • @H.HISTORY If you read the dupe you get to this result yourself. – Rizier123 Dec 02 '15 at 10:58
  • 1
    @H.HISTORY try this `$user = $json['data']['object']['source']['name'];` – mega6382 Dec 02 '15 at 11:00

0 Answers0