php 模拟登录paypal,ThinkPHP实现PayPal登录

namespace Home\Controller;

use Think\Controller;

class User extends Controller{

public function paypal(){

$_SESSION['state'] = md5(uniqid(rand(), TRUE));

$client_id = '你的client_id';

$client_secret = '你的client_secret';

$nonce = time() . rand();

$app_return_url = 'http://yourdomain/user/paypal_return';  //这里的返回地址必须要与你在paypal上创建client_id时填写的返回地址一致

$scopes = 'profile+email+address+phone';

$paypal_auth_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?"

."client_id=".$client_id

."&response_type=code"

."&scope=".$scopes

."&nonce=".$nonce

."&state=".$_SESSION['state']

."&redirect_uri=".urlencode($app_return_url);

//echo $paypal_auth_url;exit;

header("Location: $paypal_auth_url");

}

/**

* Paypal返回地址

* @return void

*/

public function paypal_return(){

$code = trim($_GET['code']);

$client_id = '你的client_id';

$client_secret = '你的client_secret';

//根据授权码获取到access_token

if(!isset($_SESSION['access_token'])){

$token_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice";

$postvals = "client_id=".$client_id

."&client_secret=".$client_secret

."&grant_type=authorization_code"

."&code=".$code;

$response = Http::fsockopenDownload($token_url,array(

'post' => $postvals,

));

$atoken = json_decode($response);

$access_token = $atoken->access_token;

$_SESSION['access_token'] = $access_token;  //token可以保存起来使用,貌似15分钟后才会失效

}

//获取到用户资料

$profile_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?"

."schema=openid"

."&access_token=".$_SESSION['access_token'];

$profile = file_get_contents($profile_url);

$profile = json_decode($profile);

if(isset($profile->error)){

exit($profile->message);

}

//注意:有些资料不一定会返回

$email = $profile->email;

$name = $profile->name;

$first_name = $profile->family_name;

$last_name = $profile->given_name;

$phone = $profile->phone_number;

$locale = $profile->locale;

$address = $profile->address;

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值