Stripe API JSON返回null

时间:2015-09-01 16:45:28

标签: php json runtime-error stripe-payments

在PHP中使用Stripe API,所有尝试从中提取值都返回null,r根本不显示结果。我试过了

$customers = \Stripe\Customer::all();
$customers_json = $customers->__toJSON();
json_decode($customers_json);

echo $customers_json->data->id;

以及

$customers = \Stripe\Customer::all();
$customer = $customers->__toArray(true);

echo $customer["data"]["id"];

但每次都会产生空白回声。但是,当我只输出原始变量而不将其解析为JSON或任何东西时,它返回一个充满JSON值的字符串,只是没有解析。原始$customers的输出是

Stripe\Collection JSON: { "object": "list", "has_more": false, "url": "\/v1\/customers", "data": [ { "id": "cus_6u32tOQ6MRXuqm", "object": "customer", "created": 1441114587, "livemode": false, "description": "test customer", "email": "test@respice.xyz", "shipping": null, "delinquent": false, "metadata": [ ], "subscriptions": { "object": "list", "total_count": 0, "has_more": false, "url": "\/v1\/customers\/cus_6u32tOQ6MRXuqm\/subscriptions", "data": [ ] }, "discount": null, "account_balance": 0, "currency": null, "sources": { "object": "list", "total_count": 0, "has_more": false, "url": "\/v1\/customers\/cus_6u32tOQ6MRXuqm\/sources", "data": [ ] }, "default_source": null } ] }

1 个答案:

答案 0 :(得分:2)

问题是数据字段不是对象,而是对象数组。坚持使用$customer = $customers->__toArray(true);方法,您应该尝试以下方法:

echo $customer['data'][0]['id'];

如果您希望遍历所有客户,请尝试执行以下操作:

foreach($customer['data'] as $currentCustomerData){
    // do stuff here
}