0

I'm trying to get a nested property within objects that is created via a JSON call.

I'm not entirely sure if what I'm doing is actually right because I don't get the nested property at all.

I just need to write that value in text file.

This is my entire code:

$input = @file_get_contents("php://input");
$event_json = json_decode($input);

// Do something with $event_json


//print_r($event_json);

$user = $event_json->data->object->lines->data->plan->id;

http_response_code(200); // PHP 5.4 or greater

//if ($event_json->type == "charge.failed") {
if ($event_json->type == 'invoice.payment_succeeded') {
$file = fopen("file.txt","w");
fwrite($file,$user);
fclose($file);
}

This is how I am trying to get the nested property:

$user = $event_json->data->object->lines->data->plan->id;

and this is the entire JSON reply:

{
  "created": 1326853478,
  "livemode": false,
  "id": "evt_00000000000000",
  "type": "invoice.payment_succeeded",
  "object": "event",
  "request": null,
  "pending_webhooks": 1,
  "api_version": "2015-10-16",
  "data": {
    "object": {
      "id": "in_00000000000000",
      "object": "invoice",
      "amount_due": 1900,
      "application_fee": null,
      "attempt_count": 1,
      "attempted": true,
      "charge": "_00000000000000",
      "closed": true,
      "currency": "gbp",
      "customer": "cus_00000000000000",
      "date": 1449067309,
      "description": null,
      "discount": null,
      "ending_balance": 0,
      "forgiven": false,
      "lines": {
        "data": [
          {
            "id": "sub_7SXJcYv81SUnhB",
            "object": "line_item",
            "amount": 1900,
            "currency": "gbp",
            "description": null,
            "discountable": true,
            "livemode": true,
            "metadata": {},
            "period": {
              "start": 1451747394,
              "end": 1454425794
            },
            "plan": {
              "id": "someemail@email.com",
              "object": "plan",
              "amount": 1900,
              "created": 1449067298,
              "currency": "gbp",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {},
              "name": "someemail@email.com",
              "statement_descriptor": null,
              "trial_period_days": null
            },
            "proration": false,
            "quantity": 1,
            "subscription": null,
            "type": "subscription"
          }
        ],
        "total_count": 1,
        "object": "list",
        "url": "/v1/invoices/in_17DbnlFMZc35g8A2r4s2DYmb/lines"
      },
      "livemode": false,
      "metadata": {},
      "next_payment_attempt": null,
      "paid": true,
      "period_end": 1449067309,
      "period_start": 1449067309,
      "receipt_number": null,
      "starting_balance": 0,
      "statement_descriptor": null,
      "subscription": "sub_00000000000000",
      "subtotal": 1900,
      "tax": null,
      "tax_percent": null,
      "total": 1900,
      "webhooks_delivered_at": 1449067325
    }
  }
}

all i need to get is the value of this: "id": "someemail@email.com",

could someone please advise on this issue?

Question to the person that marked this as duplicated and downvoted:

The answer that you provided to the other question is identical to what i am doing in my code! so this should not be marked as duplicated as I am asking for a help in finding out if I am doing something wrong or missing something!

Would be good to explain which part of this question is duplicated of the question that you pointed to?!?

Thanks for your contribution.

Here is what the print_r gives me:

stdClass Object
(
    [created] => 1326853478
    [livemode] => 
    [id] => evt_00000000000000
    [type] => invoice.payment_succeeded
    [object] => event
    [request] => 
    [pending_webhooks] => 1
    [api_version] => 2015-10-16
    [data] => stdClass Object
        (
            [object] => stdClass Object
                (
                    [id] => in_00000000000000
                    [object] => invoice
                    [amount_due] => 1900
                    [application_fee] => 
                    [attempt_count] => 1
                    [attempted] => 1
                    [charge] => _00000000000000
                    [closed] => 1
                    [currency] => gbp
                    [customer] => cus_00000000000000
                    [date] => 1449067309
                    [description] => 
                    [discount] => 
                    [ending_balance] => 0
                    [forgiven] => 
                    [lines] => stdClass Object
                        (
                            [data] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => sub_7SYJYTaAqgniTB
                                            [object] => line_item
                                            [amount] => 1900
                                            [currency] => gbp
                                            [description] => 
                                            [discountable] => 1
                                            [livemode] => 1
                                            [metadata] => stdClass Object
                                                (
                                                )

                                            [period] => stdClass Object
                                                (
                                                    [start] => 1451751114
                                                    [end] => 1454429514
                                                )

                                            [plan] => stdClass Object
                                                (
                                                    [id] => email@email.com
                                                    [object] => plan
                                                    [amount] => 1900
                                                    [created] => 1449067298
                                                    [currency] => gbp
                                                    [interval] => month
                                                    [interval_count] => 1
                                                    [livemode] => 
                                                    [metadata] => stdClass Object
                                                        (
                                                        )

                                                    [name] => email@email.com
                                                    [statement_descriptor] => 
                                                    [trial_period_days] => 
                                                )

                                            [proration] => 
                                            [quantity] => 1
                                            [subscription] => 
                                            [type] => subscription
                                        )

                                )

                            [total_count] => 1
                            [object] => list
                            [url] => /v1/invoices/in_17DbnlFMZc35g8A2r4s2DYmb/lines
                        )

                    [livemode] => 
                    [metadata] => stdClass Object
                        (
                        )

                    [next_payment_attempt] => 
                    [paid] => 1
                    [period_end] => 1449067309
                    [period_start] => 1449067309
                    [receipt_number] => 
                    [starting_balance] => 0
                    [statement_descriptor] => 
                    [subscription] => sub_00000000000000
                    [subtotal] => 1900
                    [tax] => 
                    [tax_percent] => 
                    [total] => 1900
                    [webhooks_delivered_at] => 1449067325
                )

        )

)

Second Edit:

I've tried this and still nothing:

$user = $event_json->data->object->lines->data->plan["id"];

Also tried this:

$user = $event_json->data->object->lines->data["plan"]->id;

And this:

$user = $event_json->data->object->lines->data["id"];
H.HISTORY
  • 520
  • 9
  • 28
  • Have you read the entire duplicate carefully? If and if you are still stuck, where and why? – Rizier123 Dec 02 '15 at 15:50
  • *"Would be good to explain which part of this question is duplicated of the question that you pointed to?!?"* You ask how to access a nested object/array and the duplicate exactly shows you how you can access these data structures. – Rizier123 Dec 02 '15 at 15:52
  • Yes I have read the entire thing! and I am stuck where I clearly stated stated in my question. in your answer on the other question you stated that you can access the objects by using -> and for each object we keep going forward using a -> until we get to the object we want. and that is exactly what i am doing. – H.HISTORY Dec 02 '15 at 15:54
  • @Rizier123, so what you are saying is that because someone had "sort" of similar question in the past, no one else should be allowed to ask their questions because it might have the same/duplicated context? I think you need to re-read the STO tos before keep marking people's questions as duplicated and posting a link to your own answers as well.. – H.HISTORY Dec 02 '15 at 15:56
  • Here we have the error. You don't only have nested objects, you also have arrays in it. Use debug functions such as: `echo "
    "; print_r($decodedJSON);` to see the structure. Use the output with the duplicate to find the way how to access the data you want.
    – Rizier123 Dec 02 '15 at 15:57
  • 1
    Of course same questions can be asked even if they are already answered. But then they need a clear statement why the question is not a duplicate or the duplicate didn't helped to get to the goal. – Rizier123 Dec 02 '15 at 16:00
  • Now since you posted the output of: `print_r()` you can simply start from your data which you want to access and work your way out and always look if you have an object(`->`) or array(`[]`) which you need to access, like described in the duplicate. – Rizier123 Dec 02 '15 at 16:20
  • 1
    You are getting closer, but still mix some things up. `data` is an array here. So to access the `id` you would have to do: `$user = $event_json->data->object->lines->data[0]->plan->id;`. I would recommend you to look at the dupe again, so you can understand how I got to this solution ← + that you can get to it yourself next time. (If you really don't understand how I got to this solution ↑ I'm happy to explain it to you in more detail, if you want.) – Rizier123 Dec 02 '15 at 16:27

0 Answers0