Home  >  Article  >  php教程  >  ThinkPHP实现PayPal登录

ThinkPHP实现PayPal登录

WBOY
WBOYOriginal
2016-06-07 11:37:341304browse

最近做跨境电子商务程序的第三方登录用到了PayPal登录,发上来给需要的人。
<?php <br /> namespace Home\Controller;<br> use Think\Controller;<br> class User extends Controller{<br>     public function paypal(){<br>         $_SESSION['state'] = md5(uniqid(rand(), TRUE)); <br>         $client_id = '你的client_id';<br>         $client_secret = '你的client_secret';<br>         $nonce = time() . rand();<br>         $app_return_url = 'http://yourdomain/user/paypal_return';  //这里的返回地址必须要与你在paypal上创建client_id时填写的返回地址一致<br>         $scopes = 'profile+email+address+phone';<br>     $paypal_auth_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?"<br>             ."client_id=".$client_id<br>             ."&response_type=code"<br>             ."&scope=".$scopes<br>             ."&nonce=".$nonce<br>             ."&state=".$_SESSION['state']<br>             ."&redirect_uri=".urlencode($app_return_url);<br>         //echo $paypal_auth_url;exit;<br>     header("Location: $paypal_auth_url"); <br>     }<br> <br>     /**<br>      * Paypal返回地址<br>      * @return void<br>      */<br>     public function paypal_return(){<br>         $code = trim($_GET['code']);<br>         <br>         $client_id = '你的client_id';<br>         $client_secret = '你的client_secret';<br>         //根据授权码获取到access_token    <br>         if(!isset($_SESSION['access_token'])){    <br>             $token_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice";    <br>         $postvals = "client_id=".$client_id<br>             ."&client_secret=".$client_secret<br>             ."&grant_type=authorization_code"<br>             ."&code=".$code;<br>         $response = Http::fsockopenDownload($token_url,array(<br>             'post' => $postvals,<br>         ));<br>         $atoken = json_decode($response);<br>         $access_token = $atoken->access_token;<br>         $_SESSION['access_token'] = $access_token;  //token可以保存起来使用,貌似15分钟后才会失效<br>       }<br>        //获取到用户资料<br>        $profile_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?"<br>             ."schema=openid"<br>             ."&access_token=".$_SESSION['access_token'];<br>     $profile = file_get_contents($profile_url);<br>     $profile = json_decode($profile);<br>     if(isset($profile->error)){<br>         exit($profile->message);<br>     }<br>     //注意:有些资料不一定会返回<br>     $email = $profile->email;<br>     $name = $profile->name;<br>     $first_name = $profile->family_name;<br>     $last_name = $profile->given_name;<br>     $phone = $profile->phone_number;<br>     $locale = $profile->locale;<br>     $address = $profile->address;<br>   }<br> }<br> ?>

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn