尝试使用PayPal的API并需要帮助将Curl转换为PHP

时间:2014-07-03 14:25:41

标签: php curl paypal

这是PayPal获取访问令牌的基本沙盒请求。

$curl = "curl -v https://api.sandbox.paypal.com/v1/oauth2/token
-H 'Accept: application/json' \
-H 'Accept-Language: en_US' \
-u 'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp'
-d 'grant_type=client_credentials'";

这就是我转换到目前为止所做的事情,但它失败了。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded',
    'Connection: Keep-Alive',
    'Accept-Language: en_US',
    'Accept: applictaion/json'
));
curl_setopt($ch, CURLOPT_USERPWD, 'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

print 'Running curl';

$res = curl_exec($ch);
echo $res;
curl_close($ch);

1 个答案:

答案 0 :(得分:0)

尝试删除

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded',
    'Connection: Keep-Alive',
    'Accept-Language: en_US',
    'Accept: applictaion/json'
));

我复制并粘贴了你的代码,然后删除了CURLOPT_HTTPHEADER选项,它就运行了!