php - Paypal API, Curl not returning any output -


i'm trying figure out paypal api, , have following code, should make call, access token, , make api call. first part works(up until $accesstoken line), , returns access token properly, second part doesn't return anything. code supposed mimic can found here: make first call

$url = "https://api.sandbox.paypal.com/v1/oauth2/token"; $headers = array(     'accept' => 'application/json',     'accept-language' => 'en_us', );  $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); curl_setopt($ch, curlopt_postfields, 'grant_type=client_credentials'); curl_setopt($ch, curlopt_httpheader, $headers); curl_setopt($ch, curlopt_userpwd, $clientid . ':' . $clientsecret); $curl = curl_exec($ch);  $x = json_decode($curl, true); print_r($x); $accesstoken = $x['access_token'];   $headers2 = array(     'content-type' => 'application/json',     'authorization' => 'bearer' . $accesstoken );  $data = array(     "intent" => "sale",     "redirect_urls" => array(         "return_url" => "http://example.com/your_redirect_url/",         "cancel_url" => "http://example.com/your_cancel_url/"     ),     "payer" => array(         "payment_method" => "paypal"     ),     "transactions" => array(         "transactions" => array(             "total" => ".99",             "currency" => "usd"         )     ) );  $saleurl = "https://api.sandbox.paypal.com/v1/payments/payment";  $sale = curl_init(); curl_setopt($sale, curlopt_url, $saleurl); curl_setopt($sale, curlopt_verbose, true); curl_setopt($sale, curlopt_returntransfer, true); curl_setopt($sale, curlopt_ssl_verifypeer, false); curl_setopt($sale, curlopt_ssl_verifyhost, false); curl_setopt($sale, curlopt_postfields, json_encode($data)); curl_setopt($sale, curlopt_httpheader, $headers2); $finalsale = curl_exec($sale);  $verb = json_decode($finalsale, true); print_r($verb); 

curl doesn't make complete sense me, appreciated.

update: changed format of headers to:

$headers2 = array(     'content-type: application/json',     'authorization: bearer ' . $accesstoken ); 

as per 1 of answers. displaying:

[name] => malformed_request [message] => incoming json request not map api request [information_link] => https://developer.paypal.com/webapps/developer/docs/api/#malformed_request [debug_id] => f53a882702a04 

you not setting headers correctly ...

$headers = array(     'accept: application/json',     'accept-language: en_us' ); 

and

$headers2 = array(     'content-type: application/json',     'authorization: bearer ' . $accesstoken ); 

is correct format.

also note (space) after bearer inbetween $accesstoken

edit: update json ( think right echo out , check against reference, might have 1 many array()

$data = array(     "intent" => "sale",     "redirect_urls" => array(         "return_url" => "http://example.com/your_redirect_url/",         "cancel_url" => "http://example.com/your_cancel_url/"     ),     "payer" => array(         "payment_method" => "paypal"     ),     "transactions" => array(array(             "amount" => array(                 "total" => ".99",                 "currency" => "usd"             )         )     ) ); 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -