PHP implementation PayPal Authorized login, PHP implementation PayPal authorization
PHP enables PayPal authorized login
<?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 permissions for 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 _secret, $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 = $t His->_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 to make the access token invisible in the user agent, it is recommended to use the Code
A value. If you want to receive both the authorization code and the Id_token in the response, pass Code+id_token. Another possible response_type value is token--, which 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 identifiers, reduces the risk of replay attacks. The simple functions are: (timestamp + BASE64 encoding (random\[16\])). ' state ' = $this->_state,//CSRF Verification Code)); return $url; }/** * Get PayPal access token * @param string $code? * @return String access token */Public Function Acquire_access_token ($code) {$accessToken = null; try {$postvals = 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_returntransfer = 1, Curlopt_ssl_verifypeer = FALSE,//curlopt_sslversion =& Gt 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 (Exception $e) {throw new Exception ($e->getmessage (), 1); } return $accessToken; }/** * Get the PayPal user profile, decoded * @param string $accessToken * @return Object */Public function A Cquire_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_sslve Rsion = 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 mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/1008005.html www.bkjia.com true http://www.bkjia.com/PHPjc/1008005.html techarticle PHP Implementation PayPal authorized login, PHP implementation PayPal authorized PHP implementation PayPal Authorized login PHP/** * @project PayPal Login * @author jiangjianhe * @date 2015-04- */class paypallogin{//Sand ...