Convert Stripe API response to JSON using stripe-php library

前端 未结 7 1729
迷失自我
迷失自我 2021-01-01 21:06

I\'m accessing customer data from the Stripe API, which I\'d like to convert to JSON. Usually I\'d convert an object to an array and use json_encode() but I don

7条回答
  •  感情败类
    2021-01-01 21:44

    PHP has reserved all method names with a double underscore prefix for future use. See https://www.php.net/manual/en/language.oop5.magic.php

    Currently, in the latest php-stripe library, you can convert the Stripe Object to JSON by simpl calling **->toJSON().

    [PREVIOUSLY]

    All objects created by the Stripe PHP API library can be converted to JSON with their __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. Remember to set true as argument otherwise you will get an array filled with stripe objects.

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

提交回复
热议问题