Executing Curl in PHP to do a Stripe subscription -
the stripe api allows curl calls made. example, command:
curl https://api.stripe.com//v1/customers/cus_5ucscmnxf3jssy/subscriptions -u sk_test_redacted:
returns subscription of customer cus_5ucscmnxf3jssy.
how can use php call curl command (i trying avoid using php stripe libraries).
i trying following:
<?php // create curl resource $ch = curl_init(); // set url curl_setopt($ch, curlopt_url, "https://api.stripe.com//v1/customers/cus_5ucscmnxf3jssy/subscriptions -u sk_test_redacted:"); //return transfer string curl_setopt($ch, curlopt_returntransfer, 1); // $output contains output string $output = curl_exec($ch); print($output); // close curl resource free system resources curl_close($ch); ?>
however, seems curl not take -u parameter of url. following error:
{ "error": { "type": "invalid_request_error", "message": "you did not provide api key. need provide api key in authorization header, using bearer auth (e.g. 'authorization: bearer your_secret_key'). see https://stripe.com/docs/api#authentication details, or can @ https://support.stripe.com/." }
how can pass -u sk_test_redacted: parameter curl call?
i ran same issue. wanted use php's curl functions instead of using official stripe api because singletons make me nauseous.
i wrote own simple stripe class utilizes api via php , curl.
class stripe { public $headers; public $url = 'https://api.stripe.com/v1/'; public $fields = array(); function __construct () { $this->headers = array('authorization: bearer '.stripe_api_key); // stripe_api_key = stripe api key } function call () { $ch = curl_init(); curl_setopt($ch, curlopt_httpheader, $this->headers); curl_setopt($ch, curlopt_url, $this->url); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_postfields, http_build_query($this->fields)); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_ssl_verifypeer, false); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); // return php array api response } } // create customer , use email identify them in stripe $s = new stripe(); $s->url .= 'customers'; $s->fields['email'] = $_post['email']; $customer = $s->call(); // create customer subscription credit card , plan $s = new stripe(); $s->url .= 'customers/'.$customer['id'].'/subscriptions'; $s->fields['plan'] = $_post['plan']; // name of stripe plan i.e. my_stripe_plan // credit card details $s->fields['source'] = array( 'object' => 'card', 'exp_month' => $_post['card_exp_month'], 'exp_year' => $_post['card_exp_year'], 'number' => $_post['card_number'], 'cvc' => $_post['card_cvc'] ); $subscription = $s->call();
you can dump $customer , $subscription via print_r
see response arrays if want manipulate data further.
Comments
Post a Comment