带有 PHP 和 cURL 的 Paypal API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15729167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:50:54  来源:igfitidea点击:

Paypal API with PHP and cURL

phpcurlpaypal

提问by Lee

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

我正在尝试 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"

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

我已经在 PHP 中构建了一个 curl 实例,除了最后一个之外,还有上述所有标头。-dPHP中的标志转换为 curl 选项是什么?据我所知,那里几乎没有解释。我设法推断-uCURLOPT_USERPWD.

回答by Lee

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

?>

回答by Catluc

@Lee point the right way but if u are using and old php version it wont work. But Lee version will not show up the error. Use these instead, i only add the error part to see what is going on.

@Lee 指出正确的方法,但如果您使用的是旧的 php 版本,它将无法正常工作。但李版不会显示错误。改用这些,我只添加错误部分以查看发生了什么。

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

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);
$err = curl_error($ch);

$access_token="";
if ($err) {
  echo "cURL Error #:" . $err;
}
else
{
    $json = json_decode($result);
   // print_r($json->access_token);
    $access_token = $json->access_token;
}

If u are having and old PHP version it is posible it wont work and it will show this error:

如果您使用的是旧 PHP 版本,则可能无法正常工作,并且会显示以下错误:

cURL Error #:error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure

Just stack overflow it! Here they discuss the problem

就栈溢出吧! 他们在这里讨论问题

回答by ReverseEMF

JSONand US Englishappear to be the Defaults, but to be in perfect compliance, add the following line:

JSON美国英语似乎是Defaults,但要完全符合要求,请添加以下行:

curl_setopt($ch, CURLOPT_HTTPHEADER, "Accept: application/json, Accept-Language: en_US");