Convert Stripe API response for JSON using stripe-php library

I am accessing the client data from the Stripe API , which I would like to convert to JSON. I usually convert an object to an array and use json_encode() , but in this case I don’t see the possibility even when trying to access nested arrays.

This is the answer I'm trying to convert to json:

 Stripe_Customer Object ( [_apiKey:protected] => MY_KEY_IS_HERE [_values:protected] => Array ( [id] => cus_2dVcTSc6ZtHQcv [object] => customer [created] => 1380101320 [livemode] => [description] => Bristol : John Doe [email] => someone6@gmail.com [delinquent] => [metadata] => Array ( ) [subscription] => [discount] => [account_balance] => 0 [cards] => Stripe_List Object ( [_apiKey:protected] => MY_KEY_IS_HERE [_values:protected] => Array ( [object] => list [count] => 1 [url] => /v1/customers/cus_2dVcTSc6ZtHQcv/cards [data] => Array ( [0] => Stripe_Object Object ( [_apiKey:protected] => MY_KEY_IS_HERE [_values:protected] => Array ( [id] => card_2dVcLabLlKkOys [object] => card [last4] => 4242 [type] => Visa [exp_month] => 5 [exp_year] => 2014 [fingerprint] => NzDd6OkHnfElGUif [customer] => cus_2dVcTSc6ZtHQcv [country] => US [name] => John Doe [address_line1] => [address_line2] => [address_city] => [address_state] => [address_zip] => [address_country] => [cvc_check] => pass [address_line1_check] => [address_zip_check] => ) [_unsavedValues:protected] => Stripe_Util_Set Object ( [_elts:Stripe_Util_Set:private] => Array ( ) ) [_transientValues:protected] => Stripe_Util_Set Object ( [_elts:Stripe_Util_Set:private] => Array ( ) ) [_retrieveOptions:protected] => Array ( ) ) ) ) [_unsavedValues:protected] => Stripe_Util_Set Object ( [_elts:Stripe_Util_Set:private] => Array ( ) ) [_transientValues:protected] => Stripe_Util_Set Object ( [_elts:Stripe_Util_Set:private] => Array ( ) ) [_retrieveOptions:protected] => Array ( ) ) [default_card] => card_2dVcLabLlKkOys ) [_unsavedValues:protected] => Stripe_Util_Set Object ( [_elts:Stripe_Util_Set:private] => Array ( ) ) [_transientValues:protected] => Stripe_Util_Set Object ( [_elts:Stripe_Util_Set:private] => Array ( ) ) [_retrieveOptions:protected] => Array ( ) ) 

Any help is much appreciated!

+11
json php stripe-payments
source share
5 answers

PHP reserved all double-underlined prefix method names for future use. See https://www.php.net/manual/en/language.oop5.magic.php

Currently, in the latest php-stripe library, you can convert a Stripe object to JSON by simply calling ** β†’ toJSON ().

[BEFORE]

All objects created by the Stripe PHP API library can be converted to JSON using the __toJSON () methods.

 Stripe::setApiKey("sk_xxxxxxxxxxxxxxxxxxxxxxxxx"); $customer = Stripe_Customer::create(array( "card" => $token, "plan" => $plan, )); $customer_json = $customer->__toJSON(); 

There is also a __toArray ($ recursive = false) method. Do not forget to set the argument value to true, otherwise you will get an array filled with strip objects.

 Stripe::setApiKey("sk_xxxxxxxxxxxxxxxxxxxxxxxxx"); $customer = Stripe_Customer::create(array( "card" => $token, "plan" => $plan, )); $customer_array = $customer->__toArray(true); 
+57
source share

You can access the Stripe_Object attributes as follows:

 $customer->attribute; 

So, to get the last4 customer last4 , you can do this:

 $customer->default_card->last4; 

However, you need to make sure that you have the default_card attribute. You can get the default_card object at the same time as the rest of the client by passing the expand argument:

 $customer = Stripe_Customer::retrieve(array( "id" => "cus_2dVcTSc6ZtHQcv", "expand" => array("default_card") )); 
+4
source share

If, like me, you came here looking for a python 2.7 solution, just stripe_object to str() . This starts the internal __str__() object, which converts the object to a JSON string.

for example

 charge = stripe.Charge.... print str(charge) 
0
source share

The top-level object contains other instances of the object - the cast to (array) method affects only the top-level element. You may have to go down recursively - but I would do it differently here, given that the classes are serializable:

 $transfer = serialize($myobject); 

What are you going to do with other JSONified data?

If you are going to pass an object without class information, you can try using Reflection:

 abstract class Object { /** * initialize an object from matching properties of another object */ protected function cloneInstance($obj) { if (is_object($obj)) { $srfl = new ReflectionObject($obj); $drfl = new ReflectionObject($this); $sprops = $srfl->getProperties(); foreach ($sprops as $sprop) { $sprop->setAccessible(true); $name = $sprop->getName(); if ($drfl->hasProperty($name)) { $value = $sprop->getValue($obj); $propDest = $drfl->getProperty($name); $propDest->setAccessible(true); $propDest->setValue($this,$value); } } } else Log::error('Request to clone instance %s failed - parameter is not an object', array(get_class($this))); return $this; } public function stdClass() { $trg = (object)array(); $srfl = new ReflectionObject($this); $sprops = $srfl->getProperties(); foreach ($sprops as $sprop) { if (!$sprop->isStatic()) { $sprop->setAccessible(true); $name = $sprop->getName(); $value = $sprop->getValue($this); $trg->$name = $value; } } return $trg; } } 

This is the base class of most of my portable classes. It creates a stdClass object from a class or initializes a class from a stdClass object. You can easily take this for your needs (e.g. create an array).

-one
source share

This is already in JSON format, so you need to convert it to json_encode () again, just pass it to your script

-one
source share

All Articles