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"];