2013-10-07 50 views
1

我想弄清楚paypal API,並且我有下面的代碼,它應該進行調用,獲取訪問令牌,然後進行API調用。第一部分工作(直到$accesstoken行),並正確返回訪問令牌,但第二部分不返回任何內容。這是應該模仿的代碼可以在這裏找到:Make Your First CallPayPal API,Curl不返回任何輸出

$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); 

捲翹不作完整意義上的對我來說,任何幫助,將不勝感激。

更新: 我改變了標題的格式:

$headers2 = array(
    'Content-Type: application/json', 
    'Authorization: Bearer ' . $accesstoken 
); 

按其中一個答案。現在,它顯示:

[name] => MALFORMED_REQUEST 
[message] => Incoming JSON request does not map to API request 
[information_link] => https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST 
[debug_id] => f53a882702a04 
+1

在你的產品代碼總是驗證同行和驗證安全主機:http://docforge.com/wiki/PHP/Curl –

+1

從不假定外部調用成功。你不檢查curl_exec()中的返回值是否爲boolean false,這表示失敗。你只需要返回的值,並嘗試json_decode它。至少你應該更像'if(($ result = $ curl_exec($ handle)=== FALSE){die(curl_error($ handle));}' –

+0

@Matt,我現在正在測試,但是($結果= curl_exec($ sale))=== FALSE){echo「Error。」; die(curl_error($ sale))當然,SSL是一個必須的,一旦我在線 –

回答

2

您沒有正確設置你的頭......

$headers = array(
    'Accept: application/json', 
    'Accept-Language: en_US' 
); 

$headers2 = array(
    'Content-Type: application/json', 
    'Authorization: Bearer ' . $accesstoken 
); 

是正確的格式。

還要注意(空間)承載後,插圖中的$的accessToken

編輯:更新您的JSON(我認爲這是正確的,但呼應出來,並檢查它與參考,我可能有一對多陣列()

$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" 
      ) 
     ) 
    ) 
); 
+0

現在它給出了關於格式錯誤的請求的錯誤,「傳入的JSON請求沒有映射到API請求」 –

+1

現在它給了我:「貨幣金額必須是非負數,可以選擇包含精確的2位小數由'。'隔開,可選的千位分隔符',',限制在小數點前的7位數「 –

+0

加0(0.99 vs .99)固定。 –

1

你需要在這裏

$headers2 = array(
    'Content-Type' => 'application/json', 
    'Authorization' => 'Bearer ' . $accesstoken // Added a space after Bearer 
); 

的空間看看現在的工作

+0

這似乎沒有改變任何東西,儘管你對這個空間是正確的,我的意思是包括它。 –

0

更改 curl_setopt($ sale,CURLOPT_POSTFIELDS,json_encode($ data));

到 curl_setopt($ sale,CURLOPT_POSTFIELDS,$ data);

JSON的編碼功能打破它