使用PHP搜索Stripe的JSON响应值

时间:2017-04-04 15:56:10

标签: php json

我知道这个问题已经进行了好几次,但经过几次测试后我真的无法做到这一点。 我正在使用Stripe实施SEPA付款。我已设法将所有信息发布到Stripe,但很难在JSON响应中获取值。

这就是我需要的是“ mand_url ”值到最后:

{
  "id": "XXXXXXXXXXX",
  "object": "customer",
  "account_balance": 0,
  "created": XXXXXXXXXXX,
  "currency": null,
  "default_source": "XXXXXXXXXXX",
  "delinquent": false,
  "description": null,
  "discount": null,
  "email": null,
  "livemode": true,
  "metadata": {},
  "shipping": null,
  "sources": {
    "object": "list",
    "data": [
      {
        "id": "XXXXXXXXXXX",
        "object": "source",
        "amount": null,
        "client_secret": "XXXXXXXXXXX",
        "created": XXXXXXXXXXX,
        "currency": "eur",
        "customer": "XXXXXXXXXXX",
        "flow": "none",
        "livemode": true,
        "metadata": {},
        "owner": {
          "address": {
            "city": "XXXXXXXXXXX",
            "country": "XXXXXXXXXXX",
            "line1": null,
            "line2": null,
            "postal_code": "00000",
            "state": null
          },
          "email": null,
          "name": "John Doe",
          "phone": null,
          "verified_address": null,
          "verified_email": null,
          "verified_name": null,
          "verified_phone": null
        },
        "status": "chargeable",
        "type": "sepa_debit",
        "usage": "reusable",
        "sepa_debit": {
          "bank_code": "XXXXXXXXXXX",
          "branch_code": "XXXXXXXXXXX",
          "country": "XXXXXXXXXXX",
          "fingerprint": "XXXXXXXXXXX",
          "last4": "XXXXXXXXXXX",
          "mandate_reference": "XXXXXXXXXXX",
          "mandate_url": "https://hooks.stripe.com/adapter/sepa_debit/file/random_keys"
        }
      }
    ],
    "has_more": false,
    "total_count": 1,
    "url": "XXXXXXXXXXX"
  },
  "subscriptions": {
    "object": "list",
    "data": [],
    "has_more": false,
    "total_count": 0,
    "url": "XXXXXXXXXXX"
  }
}

没有任何方法可以在没有创造巨大的foreach的情况下获得它吗?

谢谢:)

1 个答案:

答案 0 :(得分:0)

首先关闭你的json是无效的,因为你有几个"created": XXXXXXXXXXX,实例,所以下面是更正json和使用json_decode的例子

<?php


$json = '{
    "id": "XXXXXXXXXXX",
    "object": "customer",
    "account_balance": 0,
    "created": "XXXXXXXXXXX",
    "currency": null,
    "default_source": "XXXXXXXXXXX",
    "delinquent": false,
    "description": null,
    "discount": null,
    "email": null,
    "livemode": true,
    "metadata": {},
    "shipping": null,
    "sources": {
        "object": "list",
        "data": [{
            "id": "XXXXXXXXXXX",
            "object": "source",
            "amount": null,
            "client_secret": "XXXXXXXXXXX",
            "created": "XXXXXXXXXXX",
            "currency": "eur",
            "customer": "XXXXXXXXXXX",
            "flow": "none",
            "livemode": true,
            "metadata": {},
            "owner": {
                "address": {
                    "city": "XXXXXXXXXXX",
                    "country": "XXXXXXXXXXX",
                    "line1": null,
                    "line2": null,
                    "postal_code": "00000",
                    "state": null
                },
                "email": null,
                "name": "John Doe",
                "phone": null,
                "verified_address": null,
                "verified_email": null,
                "verified_name": null,
                "verified_phone": null
            },
            "status": "chargeable",
            "type": "sepa_debit",
            "usage": "reusable",
            "sepa_debit": {
                "bank_code": "XXXXXXXXXXX",
                "branch_code": "XXXXXXXXXXX",
                "country": "XXXXXXXXXXX",
                "fingerprint": "XXXXXXXXXXX",
                "last4": "XXXXXXXXXXX",
                "mandate_reference": "XXXXXXXXXXX",
                "mandate_url": "https://hooks.stripe.com/adapter/sepa_debit/file/random_keys"
            }
        }],
        "has_more": false,
        "total_count": 1,
        "url": "XXXXXXXXXXX"
    },
    "subscriptions": {
        "object": "list",
        "data": [],
        "has_more": false,
        "total_count": 0,
        "url": "XXXXXXXXXXX"
    }
}';

$data = json_decode($json);
$mandate_url = $data->sources->data[0]->sepa_debit->mandate_url);
echo $mandate_url;


?>

或者,您可以将json放入关联数组并使用以下语法

$data = json_decode($json, true);
$mandate_url = $data['sources']['data'][0]['sepa_debit']['mandate_url'];
echo $mandate_url;