Stripe API JSON 返回 null [英] Stripe API JSON returns null
问题描述
在 PHP 中使用 Stripe API 的所有尝试都返回 null,r 根本不显示结果.我试过了
Using the Stripe API in PHP all attempts to pull a value off of it is returning null, r not displaying a result at all. I've tried
$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
的输出是
but each time both result in a blank echo. However, when I just output the original variable without parsing it to JSON or anything, it returns a string full of JSON values, just not parsed. The output of just raw $customers
is
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 } ] }
推荐答案
问题是数据字段不是一个对象,而是一个对象数组.坚持使用 $customer = $customers->__toArray(true);
方法,您应该尝试以下操作:
The issue is that the data field is not an object, but an array of objects. Sticking with the $customer = $customers->__toArray(true);
method, you should try the following:
echo $customer['data'][0]['id'];
如果您希望遍历所有客户,请尝试这样做:
If you wish to iterate through all customers, try doing this:
foreach($customer['data'] as $currentCustomerData){
// do stuff here
}
这篇关于Stripe API JSON 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!