解析 PHP 中的 JSON 数组
Parsing a JSON array in PHP
我是使用 JSON 的初学者,所以请多多包涵。基本上,我从 Stripe 收到这个 JSON 数组,这很棒(而且我不认为其中有任何敏感信息,因为它是一个带有虚假信用卡号且未列出密钥的测试帐户)。
基本上,在这一点上,我会很高兴获得以下单个数据的任何方法:current_period_end
这是我 dd() 时的数组:
Customer {#270 ▼
#_opts: RequestOptions {#225 ▶}
#_values: array:15 [▼
"id" => "cus_6slA9S24KBk8qK"
"object" => "customer"
"created" => 1440817503
"livemode" => false
"description" => null
"email" => null
"shipping" => null
"delinquent" => false
"metadata" => AttachedObject {#276 ▶}
"subscriptions" => Collection {#257 ▼
#_opts: RequestOptions {#225 ▶}
#_values: array:5 [▼
"object" => "list"
"total_count" => 1
"has_more" => false
"url" => "/v1/customers/cus_6slA9S24KBk8qK/subscriptions"
"data" => array:1 [▼
0 => Subscription {#249 ▼
#_opts: RequestOptions {#225 ▶}
#_values: array:18 [▼
"id" => "sub_6slAE9UNlCLi24"
"plan" => Plan {#293 ▶}
"object" => "subscription"
"start" => 1440817504
"status" => "trialing"
"customer" => "cus_6slA9S24KBk8qK"
"cancel_at_period_end" => false
"current_period_start" => 1440817504
"current_period_end" => 1441422304
"ended_at" => null
"trial_start" => 1440817504
"trial_end" => 1441422304
"canceled_at" => null
"quantity" => 1
"application_fee_percent" => null
"discount" => null
"tax_percent" => null
"metadata" => AttachedObject {#255 ▶}
]
#_unsavedValues: Set {#294 ▶}
#_transientValues: Set {#289 ▶}
#_retrieveOptions: []
}
]
]
#_unsavedValues: Set {#280 ▶}
#_transientValues: Set {#272 ▶}
#_retrieveOptions: []
}
"discount" => null
"account_balance" => 0
"currency" => "usd"
"sources" => Collection {#260 ▼
#_opts: RequestOptions {#225 ▶}
#_values: array:5 [▼
"object" => "list"
"total_count" => 1
"has_more" => false
"url" => "/v1/customers/cus_6slA9S24KBk8qK/sources"
"data" => array:1 [▼
0 => Card {#322 ▼
#_opts: RequestOptions {#225 ▶}
#_values: array:23 [▼
"id" => "card_16ezeJKTaUqqkEkTmcBfksKh"
"object" => "card"
"last4" => "4242"
"brand" => "Visa"
"funding" => "credit"
"exp_month" => 10
"exp_year" => 2017
"fingerprint" => "Y1GhThZ3NCmHOdTv"
"country" => "US"
"name" => "blah@yahoo.com"
"address_line1" => null
"address_line2" => null
"address_city" => null
"address_state" => null
"address_zip" => null
"address_country" => null
"cvc_check" => null
"address_line1_check" => null
"address_zip_check" => null
"tokenization_method" => null
"dynamic_last4" => null
"metadata" => AttachedObject {#335 ▼
#_opts: RequestOptions {#225 ▶}
#_values: []
#_unsavedValues: Set {#339 ▶}
#_transientValues: Set {#340 ▶}
#_retrieveOptions: []
}
"customer" => "cus_6slA9S24KBk8qK"
]
#_unsavedValues: Set {#336 ▶}
#_transientValues: Set {#337 ▶}
#_retrieveOptions: []
}
]
]
#_unsavedValues: Set {#321 ▶}
#_transientValues: Set {#332 ▶}
#_retrieveOptions: []
}
"default_source" => "card_16ezeJKTaUqqkEkTmcBfksKh"
]
#_unsavedValues: Set {#269 ▶}
#_transientValues: Set {#288 ▶}
#_retrieveOptions: []
}
下面是它只是回显出来的样子:
Stripe\Customer JSON: { "id": "cus_6smHAG99OncrSm", "object": "customer", "created": 1440821625, "livemode": false, "description": null, "email": null, "shipping": null, "delinquent": false, "metadata": [], "subscriptions": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/subscriptions", "data": [ { "id": "sub_6smHbsmTA6JhrP", "plan": { "id": "yearly", "interval": "year", "name": "yearly", "created": 1439229255, "amount": 5000, "currency": "usd", "object": "plan", "livemode": false, "interval_count": 1, "trial_period_days": 7, "metadata": [], "statement_descriptor": "Degree Tracker yearly" }, "object": "subscription", "start": 1440821627, "status": "trialing", "customer": "cus_6smHAG99OncrSm", "cancel_at_period_end": false, "current_period_start": 1440821627, "current_period_end": 1441426427, "ended_at": null, "trial_start": 1440821627, "trial_end": 1441426427, "canceled_at": null, "quantity": 1, "application_fee_percent": null, "discount": null, "tax_percent": null, "metadata": [] } ] }, "discount": null, "account_balance": 0, "currency": "usd", "sources": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/sources", "data": [ { "id": "card_16f0ioKTaUqqkEkT7osN0knY", "object": "card", "last4": "4242", "brand": "Visa", "funding": "credit", "exp_month": 10, "exp_year": 2017, "fingerprint": "Y1GhThZ3NCmHOdTv", "country": "US", "name": "blah@yahoo.com", "address_line1": null, "address_line2": null, "address_city": null, "address_state": null, "address_zip": null, "address_country": null, "cvc_check": null, "address_line1_check": null, "address_zip_check": null, "tokenization_method": null, "dynamic_last4": null, "metadata": [], "customer": "cus_6smHAG99OncrSm" } ] }, "default_source": "card_16f0ioKTaUqqkEkT7osN0knY" }
以上内容可能足以让我找到解决问题的简单方法。但是,如果您熟悉 Laravel 和 Cashier
,这里还有一些 optional extra material 可能会帮助您找到我的解决方案
我可以从我得到的 JSON 响应中提取几个值,例如值 "last4"(信用卡的最后四位数字),方法是:
$this_customer = \Stripe\Customer::retrieve($user->stripe_id);
$last_four = $this_customer->sources->retrieve($this_customer->default_source)->last4;
我也可以通过做一些非常相似的事情来获得 "exp_year" 值:
$this_customer = \Stripe\Customer::retrieve($user->stripe_id); //This is the first billing date
$expiration_year = $this_customer->sources->retrieve($this_customer->default_source)->exp_year;
但是,我一直无法获得 "current_period_end" 值。这是我尝试过的:
$this_customer = \Stripe\Customer::retrieve($user->stripe_id); //This is the first billing date
$expiration_year = $this_customer->subscriptions->retrieve($this_customer->default_source)->current_period_end;
我遇到麻烦的一个原因可能是返回给我的 JSON 气体订阅”数组与 "sources" 数组位于不同的位置。但是,我一段时间以来,我一直在用头撞墙,试图弄清楚如何修复我的代码以访问它,尽管我确信上述问题有不止 1 个简单的解决方案。
<?php
$json = '{ "id": "cus_6smHAG99OncrSm", "object": "customer", "created": 1440821625, "livemode": false, "description": null, "email": null, "shipping": null, "delinquent": false, "metadata": [], "subscriptions": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/subscriptions", "data": [ { "id": "sub_6smHbsmTA6JhrP", "plan": { "id": "yearly", "interval": "year", "name": "yearly", "created": 1439229255, "amount": 5000, "currency": "usd", "object": "plan", "livemode": false, "interval_count": 1, "trial_period_days": 7, "metadata": [], "statement_descriptor": "Degree Tracker yearly" }, "object": "subscription", "start": 1440821627, "status": "trialing", "customer": "cus_6smHAG99OncrSm", "cancel_at_period_end": false, "current_period_start": 1440821627, "current_period_end": 1441426427, "ended_at": null, "trial_start": 1440821627, "trial_end": 1441426427, "canceled_at": null, "quantity": 1, "application_fee_percent": null, "discount": null, "tax_percent": null, "metadata": [] } ] }, "discount": null, "account_balance": 0, "currency": "usd", "sources": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/sources", "data": [ { "id": "card_16f0ioKTaUqqkEkT7osN0knY", "object": "card", "last4": "4242", "brand": "Visa", "funding": "credit", "exp_month": 10, "exp_year": 2017, "fingerprint": "Y1GhThZ3NCmHOdTv", "country": "US", "name": "blah@yahoo.com", "address_line1": null, "address_line2": null, "address_city": null, "address_state": null, "address_zip": null, "address_country": null, "cvc_check": null, "address_line1_check": null, "address_zip_check": null, "tokenization_method": null, "dynamic_last4": null, "metadata": [], "customer": "cus_6smHAG99OncrSm" } ] }, "default_source": "card_16f0ioKTaUqqkEkT7osN0knY" }';
$data = json_decode($json, true);
$current_period_end = $data['subscriptions']['data'][0]['current_period_end'];
print_r($current_period_end);
输出:
1441426427
最后是日期你可以用这个代码获取日期:
echo date('Y-m-d', $current_period_end);
日期:
2015-09-05
我是使用 JSON 的初学者,所以请多多包涵。基本上,我从 Stripe 收到这个 JSON 数组,这很棒(而且我不认为其中有任何敏感信息,因为它是一个带有虚假信用卡号且未列出密钥的测试帐户)。
基本上,在这一点上,我会很高兴获得以下单个数据的任何方法:current_period_end
这是我 dd() 时的数组:
Customer {#270 ▼
#_opts: RequestOptions {#225 ▶}
#_values: array:15 [▼
"id" => "cus_6slA9S24KBk8qK"
"object" => "customer"
"created" => 1440817503
"livemode" => false
"description" => null
"email" => null
"shipping" => null
"delinquent" => false
"metadata" => AttachedObject {#276 ▶}
"subscriptions" => Collection {#257 ▼
#_opts: RequestOptions {#225 ▶}
#_values: array:5 [▼
"object" => "list"
"total_count" => 1
"has_more" => false
"url" => "/v1/customers/cus_6slA9S24KBk8qK/subscriptions"
"data" => array:1 [▼
0 => Subscription {#249 ▼
#_opts: RequestOptions {#225 ▶}
#_values: array:18 [▼
"id" => "sub_6slAE9UNlCLi24"
"plan" => Plan {#293 ▶}
"object" => "subscription"
"start" => 1440817504
"status" => "trialing"
"customer" => "cus_6slA9S24KBk8qK"
"cancel_at_period_end" => false
"current_period_start" => 1440817504
"current_period_end" => 1441422304
"ended_at" => null
"trial_start" => 1440817504
"trial_end" => 1441422304
"canceled_at" => null
"quantity" => 1
"application_fee_percent" => null
"discount" => null
"tax_percent" => null
"metadata" => AttachedObject {#255 ▶}
]
#_unsavedValues: Set {#294 ▶}
#_transientValues: Set {#289 ▶}
#_retrieveOptions: []
}
]
]
#_unsavedValues: Set {#280 ▶}
#_transientValues: Set {#272 ▶}
#_retrieveOptions: []
}
"discount" => null
"account_balance" => 0
"currency" => "usd"
"sources" => Collection {#260 ▼
#_opts: RequestOptions {#225 ▶}
#_values: array:5 [▼
"object" => "list"
"total_count" => 1
"has_more" => false
"url" => "/v1/customers/cus_6slA9S24KBk8qK/sources"
"data" => array:1 [▼
0 => Card {#322 ▼
#_opts: RequestOptions {#225 ▶}
#_values: array:23 [▼
"id" => "card_16ezeJKTaUqqkEkTmcBfksKh"
"object" => "card"
"last4" => "4242"
"brand" => "Visa"
"funding" => "credit"
"exp_month" => 10
"exp_year" => 2017
"fingerprint" => "Y1GhThZ3NCmHOdTv"
"country" => "US"
"name" => "blah@yahoo.com"
"address_line1" => null
"address_line2" => null
"address_city" => null
"address_state" => null
"address_zip" => null
"address_country" => null
"cvc_check" => null
"address_line1_check" => null
"address_zip_check" => null
"tokenization_method" => null
"dynamic_last4" => null
"metadata" => AttachedObject {#335 ▼
#_opts: RequestOptions {#225 ▶}
#_values: []
#_unsavedValues: Set {#339 ▶}
#_transientValues: Set {#340 ▶}
#_retrieveOptions: []
}
"customer" => "cus_6slA9S24KBk8qK"
]
#_unsavedValues: Set {#336 ▶}
#_transientValues: Set {#337 ▶}
#_retrieveOptions: []
}
]
]
#_unsavedValues: Set {#321 ▶}
#_transientValues: Set {#332 ▶}
#_retrieveOptions: []
}
"default_source" => "card_16ezeJKTaUqqkEkTmcBfksKh"
]
#_unsavedValues: Set {#269 ▶}
#_transientValues: Set {#288 ▶}
#_retrieveOptions: []
}
下面是它只是回显出来的样子:
Stripe\Customer JSON: { "id": "cus_6smHAG99OncrSm", "object": "customer", "created": 1440821625, "livemode": false, "description": null, "email": null, "shipping": null, "delinquent": false, "metadata": [], "subscriptions": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/subscriptions", "data": [ { "id": "sub_6smHbsmTA6JhrP", "plan": { "id": "yearly", "interval": "year", "name": "yearly", "created": 1439229255, "amount": 5000, "currency": "usd", "object": "plan", "livemode": false, "interval_count": 1, "trial_period_days": 7, "metadata": [], "statement_descriptor": "Degree Tracker yearly" }, "object": "subscription", "start": 1440821627, "status": "trialing", "customer": "cus_6smHAG99OncrSm", "cancel_at_period_end": false, "current_period_start": 1440821627, "current_period_end": 1441426427, "ended_at": null, "trial_start": 1440821627, "trial_end": 1441426427, "canceled_at": null, "quantity": 1, "application_fee_percent": null, "discount": null, "tax_percent": null, "metadata": [] } ] }, "discount": null, "account_balance": 0, "currency": "usd", "sources": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/sources", "data": [ { "id": "card_16f0ioKTaUqqkEkT7osN0knY", "object": "card", "last4": "4242", "brand": "Visa", "funding": "credit", "exp_month": 10, "exp_year": 2017, "fingerprint": "Y1GhThZ3NCmHOdTv", "country": "US", "name": "blah@yahoo.com", "address_line1": null, "address_line2": null, "address_city": null, "address_state": null, "address_zip": null, "address_country": null, "cvc_check": null, "address_line1_check": null, "address_zip_check": null, "tokenization_method": null, "dynamic_last4": null, "metadata": [], "customer": "cus_6smHAG99OncrSm" } ] }, "default_source": "card_16f0ioKTaUqqkEkT7osN0knY" }
以上内容可能足以让我找到解决问题的简单方法。但是,如果您熟悉 Laravel 和 Cashier
,这里还有一些 optional extra material 可能会帮助您找到我的解决方案我可以从我得到的 JSON 响应中提取几个值,例如值 "last4"(信用卡的最后四位数字),方法是:
$this_customer = \Stripe\Customer::retrieve($user->stripe_id);
$last_four = $this_customer->sources->retrieve($this_customer->default_source)->last4;
我也可以通过做一些非常相似的事情来获得 "exp_year" 值:
$this_customer = \Stripe\Customer::retrieve($user->stripe_id); //This is the first billing date
$expiration_year = $this_customer->sources->retrieve($this_customer->default_source)->exp_year;
但是,我一直无法获得 "current_period_end" 值。这是我尝试过的:
$this_customer = \Stripe\Customer::retrieve($user->stripe_id); //This is the first billing date
$expiration_year = $this_customer->subscriptions->retrieve($this_customer->default_source)->current_period_end;
我遇到麻烦的一个原因可能是返回给我的 JSON 气体订阅”数组与 "sources" 数组位于不同的位置。但是,我一段时间以来,我一直在用头撞墙,试图弄清楚如何修复我的代码以访问它,尽管我确信上述问题有不止 1 个简单的解决方案。
<?php
$json = '{ "id": "cus_6smHAG99OncrSm", "object": "customer", "created": 1440821625, "livemode": false, "description": null, "email": null, "shipping": null, "delinquent": false, "metadata": [], "subscriptions": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/subscriptions", "data": [ { "id": "sub_6smHbsmTA6JhrP", "plan": { "id": "yearly", "interval": "year", "name": "yearly", "created": 1439229255, "amount": 5000, "currency": "usd", "object": "plan", "livemode": false, "interval_count": 1, "trial_period_days": 7, "metadata": [], "statement_descriptor": "Degree Tracker yearly" }, "object": "subscription", "start": 1440821627, "status": "trialing", "customer": "cus_6smHAG99OncrSm", "cancel_at_period_end": false, "current_period_start": 1440821627, "current_period_end": 1441426427, "ended_at": null, "trial_start": 1440821627, "trial_end": 1441426427, "canceled_at": null, "quantity": 1, "application_fee_percent": null, "discount": null, "tax_percent": null, "metadata": [] } ] }, "discount": null, "account_balance": 0, "currency": "usd", "sources": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/sources", "data": [ { "id": "card_16f0ioKTaUqqkEkT7osN0knY", "object": "card", "last4": "4242", "brand": "Visa", "funding": "credit", "exp_month": 10, "exp_year": 2017, "fingerprint": "Y1GhThZ3NCmHOdTv", "country": "US", "name": "blah@yahoo.com", "address_line1": null, "address_line2": null, "address_city": null, "address_state": null, "address_zip": null, "address_country": null, "cvc_check": null, "address_line1_check": null, "address_zip_check": null, "tokenization_method": null, "dynamic_last4": null, "metadata": [], "customer": "cus_6smHAG99OncrSm" } ] }, "default_source": "card_16f0ioKTaUqqkEkT7osN0knY" }';
$data = json_decode($json, true);
$current_period_end = $data['subscriptions']['data'][0]['current_period_end'];
print_r($current_period_end);
输出:
1441426427
最后是日期你可以用这个代码获取日期:
echo date('Y-m-d', $current_period_end);
日期:
2015-09-05