PHP implementation PayPal Authorized Login

Source: Internet
Author: User
Tags auth oauth openid

This article to share is the PHP implementation of the PayPal authorization login code, very simple and practical, the need for small partners can refer to.

PHP implementation PayPal Authorized Login

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 <?php  /** * @project PayPal Login * @author jiangjianhe * @date 2015-04-03/    class Paypallogin {  //Sandbox token link private $_sanbox_oauth2_auth_uri = ' https://www.sandbox.paypal.com/webapps/auth/protocol/ Openidconnect/v1/authorize '; Private $_live_oauth2_auth_uri = ' https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize ';   Private $_acquire_user_profile_sandbox_url = ' https://www.sandbox.paypal.com/webapps/auth/protocol/ Openidconnect/v1/userinfo?schema=openid&access_token= '; Private $_acquire_user_profile_live_url = ' Https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo ? schema=openid&access_token= ';  //Sandbox token link private $_token_service_sandbox_url = ' https://www.sandbox.paypal.com/webapps/auth/protocol/ Openidconnect/v1/tokenservice '; Private $_token_service_live_url = ' https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice '; Private $_sanbox_flag = true; Private $_client_id = null; Private $_client_secret = null; Private $_redirect_uri = null; Private $_state = '; Private $_scope = ' OpenID Email phone profile address https://uri.paypal.com/services/paypalattributes '; The scope parameter determines access to the access token each parameter is detailed Url;:https://www.paypal-biz.com/product/login-with-paypal/index.html#configurebutton   public $token = null; Public $protocol = "http";    /** * @name constructor * @param $flag Sandbox environment */Public function __construct ($redirect _uri, $client _id, $client _SECR ET, $scope, $state, $flag = True) {$this->_sanbox_flag = $flag; $this->_redirect_uri = $redirect _uri; $this->_ client_id = $client _id; $this->_client_secret = $client _secret; $this->_scope = $scope; $this->_state = $state; }  /** * create PayPal Request URL * @return string/Public function Create_request_url () {$oauth 2_auth_uri = $this-&G T;_sanbox_flag? $this->_sanbox_oauth2_auth_uri: $this->_live_oauth2_auth_uri; $url = $oauth 2_auth_uri. Http_build_query Array (' client_id ' => $this->_CLIENT_ID,//Unique client identifier obtained through the application registration process. Necessary. ' Response_type ' => ' code ',//indicates that the authorization code is sent back to the application return URL. In order for the access token to be invisible in the user agent, a <code>code</code> value is recommended. If you want to receive both authorization code and Id_token in the response, pass Code+id_token. Another possible response_type value is that token--is mostly used by public clients such as JavaScript and mobile clients. ' Scope ' => $this->_scope,//;implode (', ', $this->scope), ' Redirect_uri ' => urlencode ($this->_redirect_ URI),//The return URL of the application. The structure, host name, and Port must match the return URL that you set when registering the application. ' Nonce ' => time (). Rand (),//opaque random identifier, reduces the risk of replay attacks. The Simple function is: (timestamp + Base64 encoding (RANDOM[16)). ' State ' => $this->_state,//CSRF authentication code)); return $url; }  /** * Get PayPal access token * @param string $code? * @return String access token */Public Function Acquire_access_token ($code) {$accessToken = null;   try {$postval s = sprintf ("client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s", $this->_client_ ID, $this->_client_secret, $code); if ($this->_sanbox_flag) $ch = Curl_init ($this->_token_service_sandbox_urL); else $ch = Curl_init ($this->_token_service_live_url);   $options = Array (curlopt_post => 1, curlopt_verbose => 1, curlopt_postfields => $postvals, Curlopt_return TRANSFER => 1, curlopt_ssl_verifypeer => FALSE,//curlopt_sslversion => 2);   Curl_setopt_array ($ch, $options); $response = curl_exec ($ch); $error = Curl_error ($ch);   Curl_close ($ch);   if (! $response) {throw new Exception ("Error Retrieving access token:". Curl_error ($ch)); } $jsonResponse = Json_decode ($response);   if (isset ($jsonResponse->access_token)) {$accessToken = $jsonResponse->access_token;}  } catch (Ex Ception $e) {throw new Exception ($e->getmessage (), 1);}   return $accessToken; }  /** * Get the PayPal user profile, decoded * @param string $accessToken * @return Object/Public Function Acqui Re_paypal_user_profile ($accessToken) {try {if ($this->_sanbox_flag) $url = $this->_acquire_user_profile_ Sandbox_url. $accessToken; else $url = $this->_acquire_user_profile_live_url. $accessToken;   $ch = Curl_init ($url); $options = Array (curlopt_returntransfer => 1, curlopt_ssl_verifypeer => FALSE,//curlopt_sslversion => 2); Curl_setopt_array ($ch, $options);   $response = curl_exec ($ch); $error = Curl_error ($ch); Curl_close ($ch);   if (! $response) {return false;} return Json_decode ($response); catch (Exception $e) {return false;}} }?>

The above is the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.