0

I created a database customers.txt where are saved all my created customers in Stripe. Now I want to list all the customers. This is my code in php for listing customers.

   $e= \Stripe\Customer::all(array(
        'limit' => 3
       

     ));
   
echo $e;


    }

But the output is weird:

IMAGE: enter image description here

Can someone help me to list the customers?

Now I have got my JSON and run this:

$e=\Stripe\Customer::all(array(
    "limit"=>10

 ));

$customers=json_decode($e,true);
var_dump($customers);

I get just NULL response!

user6591546
  • 25
  • 1
  • 7

1 Answers1

2

That output is not wyrd, it's a Stripe JSON string. standing for Javascript Object Notation (website).

There's a lot of questions on Stackoverflow about JSON so ask things such as How to convert JSON string into a PHP array. Also Stripes own documentation (which is very good), states:

JSON is returned by all API responses, including errors, although our API libraries convert responses to appropriate language-specific objects.

from the Stripe Documentation

Edit: You can read A useful question about turning a JSON string into an object and vice versa

So now you know what JSON is

Using it to get a PHP customer object. (revised)

$e // customer JSON of all customers.  
$customers = $e->__toArray(true);
//$customers = json_decode($e);

And then process the array $customers as you need to in your applicaton.

NOTE:

The value of $customers or $customersArray will be an Object or a String data type so you need to treat the appropriately, and they will not display with echo because echo is a string output function, so you need to use print_r() or var_dump() to display these values -in their raw form- on the screen.

EDIT TWO

Recommended that from your screenshot that you format the API response from Stripe into an Array of Objects. This can be done by following this Stack Overflow Answer here.

Please review my revised code above.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • So is this the right way to list the customers in Stripe? Except the convertion to php array? – user6591546 Jul 19 '16 at 17:32
  • Once you've got the JSON you can then use a `json_decode` function to turn the JSON data into a PHP object (or several). I will update my answer.... – Martin Jul 19 '16 at 17:38
  • Now I'm taking blank response after the convertion. – user6591546 Jul 19 '16 at 17:39
  • Because you're trying to echo an array, instead use `print_r()` or `var_dump()`. Also please do not change the question but add additions to it, future people will be confused as you have not shown your JSON screen shot, which I used to answer your original query. @user6591546 – Martin Jul 19 '16 at 17:42
  • No worries @user6591546 . If that solved your issue then you can tick the tick next to my answer, and possibly give a +1 (up arrow) too. :-) – Martin Jul 19 '16 at 17:48
  • it now outputs NULL eventhough are customers in database – user6591546 Jul 19 '16 at 17:58
  • Does this question help? http://stackoverflow.com/questions/32336921/stripe-api-json-returns-null . You need to research a lot of Qs on SO as there are many regarding stripe. By the looks of your image your JSON should be formed into an **Object** rather than an array.(remove the `true`) – Martin Jul 19 '16 at 17:58
  • My database is .txt. – user6591546 Jul 19 '16 at 17:59
  • I don't see how that relates to the JSON output. – Martin Jul 19 '16 at 18:00
  • I have updated my answer with further references and another approach to solving this issue. – Martin Jul 19 '16 at 18:04
  • I voted your question but I cant tick it. It doesnt solve my problem. – user6591546 Jul 19 '16 at 18:10