使用PHP和cURL的Paypal API [英] Paypal API with PHP and cURL

查看:294
本文介绍了使用PHP和cURL的Paypal API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试第一次调用,如Paypal API文档所述。这是一个例子,如果我遵循:

I'm attempting "the first call" as outlined by the Paypal API documentation. This is the example provided that I'm following:

curl 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"



我已经在PHP中构建了一个curl实例上面的标题除了最后一个。 -d 标志在PHP中转换为curl选项是什么?有很少的解释,只要我能告诉。我设法推导出 -u CURLOPT_USERPWD

I have constructed a curl instance in PHP with all the above headers apart from the last one. What does a -d flag convert to as a curl option in PHP? There is little explanation there as far as I can tell. I managed to deduce -u as CURLOPT_USERPWD.

推荐答案

有一个好的拖网,我把来自开发人员的零件与其他问题拼凑在一起。我使用以下代码成功获得了访问令牌:

Having a good trawl around I pieced together parts from developers with other problems. I successfully gained my access token using the following code:

<?php

$ch = curl_init();
$clientId = "myId";
$secret = "mySecret";

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

$result = curl_exec($ch);

if(empty($result))die("Error: No response.");
else
{
    $json = json_decode($result);
    print_r($json->access_token);
}

curl_close($ch);

?>

这篇关于使用PHP和cURL的Paypal API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆