php - 带有 PHP 和 cURL 的 Paypal API

标签 php curl paypal

我正在尝试 Paypal API 文档中概述的“第一次通话”。这是我正在关注的示例:

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 实例,其中包含除最后一个之外的所有上述 header 。 -d 标志转换为 PHP 中的 curl 选项是什么?据我所知,那里几乎没有解释。我设法将 -u 推断为 CURLOPT_USERPWD

最佳答案

通过四处搜寻,我将开发人员提出的其他问题的部分拼凑在一起。我使用以下代码成功获得了访问 token :

<?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 - 带有 PHP 和 cURL 的 Paypal API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15729167/

相关文章:

php - curl post后,如何检查postfield - 调试

curl - Facebook 在尝试创建广告时抛出 "Please specify an image to run with this ad."

javascript - 如何使用 node-fetch 模块模拟 curl 请求

javascript - 使用 I/O 从 webpge 运行 UNIX 可执行文件

php - 查询数据库时使用 PHP 函数

php - Yii2 从相关模型中获取值(value)

c++ - 使用 C++ 将网页保存到磁盘

iphone - 将 PayPal 与 iOS 应用程序集成编译错误

php - Laravel 更新/安装 composer 包

PHP/MySql 连接开销