Stripe API JSON returns null

陌路散爱 提交于 2020-01-16 04:25:09

问题


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;

and also

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

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

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

回答1:


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
}


来源:https://stackoverflow.com/questions/32336921/stripe-api-json-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!