2013-03-12 62 views
1

我嘗試新的PayPal REST API,並且想要學習如何使用PHP/curl。PayPal OAuth2 REST API基礎

我是新來使用curl所以請原諒......

從PayPal的開發者文檔,我收集所需的字段和我放在一起如下:

$ch = curl_init(); 
$postDataArray = array("grant_type=client_credentials"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataArray); 
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/oauth2/token'); 
$headerArray = array('Accept: application/json','Accept-Language: en_US'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray); 
$clientID_Secret = "clientID:secret"; 
curl_setopt($ch, CURLOPT_USERPWD, $clientID_Secret); 

接下來的幾行,我增加由於谷歌搜索。

curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$result = curl_exec($ch); 
print_r($result); 

這不會返回任何內容。 請幫忙。

+0

在這裏你需要確保clientID和secret是不是字符串文字的值:$ clientID_Secret =「clientID:secret」; – Praveen 2013-03-12 19:12:29

回答

4

這裏是一個示例:https://github.com/paypal/rest-api-curlsamples/blob/master/execute_all_calls.php

複製捲曲具體代碼:

$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_USERPWD, $clientId . ":" . $clientSecret); 
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); 
+0

太棒了 - 謝謝。現在我們來學習如何將信用卡交易推向這種REST方法。 – 2013-03-12 19:40:22

1

您的代碼工作。但是你必須改變這個$ postDataArray = array(「grant_type = client_credentials」);到$ postDataArray =「grant_type = client_credentials」;.需要刪除數組類型;