简体   繁体   English

在PHP中解析JSON数组

[英]Parsing a JSON array in PHP

I am a beginner in working with JSON, so please bear with me. 我是使用JSON的初学者,所以请多多包涵。 Basically, I am receiving this JSON array back from Stripe, which is great (and I don't believe there is any sensitive information in it as it's a test account with a bogus credit card number and no keys listed). 基本上,我从Stripe那里收到了这个JSON数组,这很棒(并且我不相信其中包含任何敏感信息,因为它是一个带有虚假信用卡号且未列出密钥的测试帐户)。

Basically, at this point, I would be happy with any method in obtaining the following single piece of data: current_period_end 基本上,在这一点上,我将对使用任何一种方法来获取以下单个数据感到满意:current_period_end

Here is the array when I dd() it: 这是当我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: []
}

Here's what it looks like with just echoing it out: 只需将其回显即可,如下所示:

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" }

The above is probably enough to find me a simple solution to my problem. 以上可能足以为我找到解决问题的简单方法。 However, here is some more optional extra material that might help you finding me a solution if you are familiar with Laravel and Cashier 但是,如果您熟悉Laravel和Cashier,这里有一些可选的其他材料,可能会帮助您找到解决方案。

I'm able to pull out a couple of values from the JSON response I'm getting such as the value "last4" (last four digits of the credit card) by doing this: 通过执行以下操作,我可以从JSON响应中提取几个值,例如值“ last4”(信用卡的后四位数字):

    $this_customer = \Stripe\Customer::retrieve($user->stripe_id);  
    $last_four = $this_customer->sources->retrieve($this_customer->default_source)->last4;

and I am also able to get the "exp_year" value by doing something very similar: 并且我也可以通过执行类似的操作来获取“ 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;

However, I have not been able to get the "current_period_end" value. 但是,我无法获取“ current_period_end”值。 Here is what I have tried: 这是我尝试过的:

    $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;

One reason for the trouble I'm having is likely that the JSON that gets returned to me gas the subscriptions" array is in a different location from the "sources" array. However, I have been banging my head against a wall for a while trying to figure out how to fix my code to access it, even though I am sure there is more than 1 simple solution to the above problem. 我遇到麻烦的一个原因很可能是返回给我的json数组与“ sources”数组位于不同的位置。但是,我已经将头撞墙了一段时间试图弄清楚如何修复我的代码以访问它,即使我确信上述问题有不止一种简单的解决方案。

<?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);

Output: 输出:

1441426427 1441426427

And finally the date you can obtain the date with this code: 最后,您可以使用以下代码获取日期:

echo date('Y-m-d', $current_period_end);

Date: 日期:

2015-09-05 2015-09-05

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM