Php implements paypal authorized login, php implements paypal authorization _ PHP Tutorial

Source: Internet
Author: User
Tags openid
Php implements paypal authorized login, and php implements paypal authorization. Php implement paypal authorized login, php implement paypal authorization php implement paypal authorized login php *** @ projectpaypallogin * @ authorjiangjianhe * @ date2015-04-03 * classpaypallogin {Sha php implement paypal authorized login, implement paypal authorization in php

Paypal authorized login using php

<? 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 the access permission of the access token ;: 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 a paypal request Url * @ return string */public function create_request_url () {$ oauth2_auth_uri = $ this-> _ sanbox_flag? $ This-> _ sanbox_oauth2_auth_uri: $ this-> _ live_oauth2_auth_uri; $ url = $ oauth2_auth_uri .'? '. Http_build_query (array ('Client _ id' => $ this-> _ client_id, // unique client identifier obtained through the application registration process. Required. 'Response _ type' => 'code', // indicates that the authorization code is sent to the response URL returned by the program. We recommend that you usecodeA value. If you want to receive both the authorization code and id_token in the response, pass the code + id_token. Another possible value of response_type is token, which is mostly used by public clients such as javascript and mobile clients. 'Process' => $ this-> _ scope, //; implode (',', $ this-> scope ), 'redirect _ url' => urlencode ($ this-> _ redirect_uri), // The URL returned by the application. The structure, host name, and Port must match the returned URL you set when registering the application. 'Nonce '=> time (). rand (), // untransparent random identifier to reduce the risk of replay attacks. The simple function is: (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, expiration => 1, expiration => 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 (Exception $ e) {throw new Exception ($ e-> getMessage (), 1);} return $ accessToken;}/*** get the PayPal user profile, decoded * @ param string $ accessToken * @ return obje Ct */public function acquire_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, rows => FALSE, // CURLOPT_SSLVERSION => 2); curl_setopt_array ($ ch, $ options); $ resp Onse = 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 all the content of this article. I hope you will like it.

Login authorization, php implement paypal authorization php implement paypal authorize login php/*** @ project paypal login * @ author jiangjianhe * @ date */class paypallogin {// Sha...

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.