【问题标题】:Connect with PayPal in PHP for user authentication在 PHP 中连接 PayPal 以进行用户身份验证
【发布时间】:2020-11-20 04:18:42
【问题描述】:

我正在尝试使用 PHP 和 CURL 在我的网站上实现“与 PayPal 连接”按钮。 我已经设法接收到身份验证代码并使用此代码访问令牌,但我无法接收用户数据。我的 REST 应用程序已获批准,我应该能够收到用户电子邮件。 我不想使用 PayPal PHP SDK,因为现在已弃用。

这是我的代码:

$pp_client_id = {my client id};
$pp_secret = {my secret};

$code = $_GET['code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.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, $pp_client_id.':'.$pp_secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
   'grant_type'=>'authorization_code',
   'code'=> $code
)));

$res_authcode = curl_exec($ch);

if (empty($res_authcode)) {
   // print error
} else {
   $json_authcode = json_decode($res_authcode);
   $refresh_token = $json_authcode->refresh_token;
   curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
      'grant_type'=>'refresh_token',
      'refresh_token'=> $refresh_token
   )));

   $res_token = curl_exec($ch);

   if(empty($res_token)) {
      // print error
   } else {
     $json_token = json_decode($res_token);
     $access_token = $json_authcode->access_token;

     curl_setopt($ch, CURLOPT_URL, 'https://api.paypal.com/v1/oauth2/userinfo/?schema=paypalv1.1');
     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
       'access_token'=> $access_token
     )));

     $res_userinfo = curl_exec($ch);
 
     if(empty($res_userinfo)) {
       // print error
     } else {
       $json_userinfo = json_decode($res_userinfo);
       print_r($json_userinfo);
     }
   }
}

curl_close($ch);

我收到一条 RESOURCE_NOT_FOUND 消息。我应该对我的代码进行哪些更改以获取用户信息?

【问题讨论】:

  • 您为什么不使用PayPal PHP SDK?它为您处理了很多令人讨厌的事情,因此不会出现此类问题(经常大声笑)。
  • @IncredibleHat 感谢您的错字建议。我不想使用 SDK,因为它已被弃用
  • Anyhoo,longhang 方法,我认为你的 URL 是错误的...尝试https://api.paypal.com/v1/identity/oauth2/userinfo 进行第二次调用。
  • 别管我提到的 PHP REST SDK...那是用于订单/付款...而不是使用 PayPal 登录(仅适用于旧的折旧 SDK)。
  • 如果这不仅仅是一个错字,那么您可以发布答案,因为与 PayPal 相关的问题/答案之间相差甚远 ;-)

标签: php curl paypal


【解决方案1】:

我终于设法解决了我的问题并通过 cURL 从 PayPal 接收用户信息。

我将连接 URL 和标头更改为:

curl_setopt($ch, CURLOPT_URL, 'https://api.paypal.com/v1/oauth2/token/userinfo?schema=openid');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'authorization: Bearer '.$access_token,
  'content-type: application/json'
));

$res_userinfo = curl_exec($ch);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 2012-12-23
    • 2014-10-13
    • 1970-01-01
    相关资源
    最近更新 更多