国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php實現paypal 授權登錄

2024-05-04 23:35:42
字體:
來源:轉載
供稿:網友

本文給大家分享的是php實現的paypal授權登錄的代碼,十分的簡單實用,有需要的小伙伴可以參考下。

php實現paypal 授權登錄

 

 
  1. <?php 
  2.  
  3. /** 
  4. * @project paypal login 
  5. * @author jiangjianhe  
  6. * @date 2015-04-03 
  7. */ 
  8.  
  9.  
  10. class paypallogin 
  11.  
  12. //沙箱token鏈接 
  13. private $_sanbox_oauth2_auth_uri = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize'
  14. private $_live_oauth2_auth_uri = 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize'
  15.  
  16. private $_acquire_user_profile_sandbox_url = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?schema=openid&access_token='
  17. private $_acquire_user_profile_live_url = 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?schema=openid&access_token='
  18.  
  19. //沙箱token鏈接 
  20. private $_token_service_sandbox_url = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice';  
  21. private $_token_service_live_url = 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice'
  22. private $_sanbox_flag = true; 
  23. private $_client_id = null; 
  24. private $_client_secret = null; 
  25. private $_redirect_uri = null; 
  26. private $_state = ''
  27. private $_scope = 'openid email phone profile address https://uri.paypal.com/services/paypalattributes'; //scope 參數決定訪問令牌的訪問權限 各個參數詳解url;:https://www.paypal-biz.com/product/login-with-paypal/index.html#configureButton 
  28.  
  29. public $token = null; 
  30. public $protocol = "http"
  31.  
  32.  
  33. /** 
  34. * @name 構造函數 
  35. * @param $flag 是否沙箱環境 
  36. */ 
  37. public function __construct($redirect_uri$client_id,$client_secret,$scope,$state,$flag = true) 
  38. $this->_sanbox_flag = $flag
  39. $this->_redirect_uri = $redirect_uri
  40. $this->_client_id = $client_id
  41. $this->_client_secret = $client_secret
  42. $this->_scope = $scope
  43. $this->_state = $state
  44.  
  45. /** 
  46. * 創建paypal request url 
  47. * @return string 
  48. */ 
  49. public function create_request_url() 
  50. $oauth2_auth_uri = $this->_sanbox_flag ? $this->_sanbox_oauth2_auth_uri :$this->_live_oauth2_auth_uri; 
  51. $url = $oauth2_auth_uri.'?'
  52. http_build_query( 
  53. array
  54. 'client_id' => $this->_client_id, //通過應用程序注冊流程獲得的唯一客戶端標識符。必需。 
  55. 'response_type' =>'code'//表明授權代碼被發送回應用程序返回URL。為了使訪問令牌在用戶代理中不可見, 建議使用<code>code</code>一值。如果您希望在響應中同時收到授權代碼和 id_token ,請傳遞 code+id_token。另一個可能的 response_type 值是 token ——大部分由javascript和移動客戶端等公共客戶端使用。 
  56. 'scope' => $this->_scope,//;implode(',', $this->scope), 
  57. 'redirect_uri' => urlencode($this->_redirect_uri), //應用程序的返回URL。結構、主機名和端口必須與您在注冊應用程序時設置的返回URL相符。 
  58. 'nonce' => time().rand(), //不透明的隨機標識符,可減少重放攻擊風險。簡單的函數是:(timestamp + Base64 encoding (random/[16/]))。 
  59. 'state' => $this->_state, // CSRF驗證碼 
  60. ); 
  61. return $url
  62.  
  63. /** 
  64. * get PayPal access token 
  65. * @param string $code ? 
  66. * @return string access token 
  67. */ 
  68. public function acquire_access_token($code ) { 
  69. $accessToken = null; 
  70.  
  71. try { 
  72. $postvals = sprintf("client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s",$this->_client_id,$this->_client_secret,$code); 
  73. if($this->_sanbox_flag) 
  74. $ch = curl_init($this->_token_service_sandbox_url); 
  75. else 
  76. $ch = curl_init($this->_token_service_live_url);  
  77.  
  78. $options = array
  79. CURLOPT_POST => 1, 
  80. CURLOPT_VERBOSE => 1, 
  81. CURLOPT_POSTFIELDS => $postvals
  82. CURLOPT_RETURNTRANSFER => 1, 
  83. CURLOPT_SSL_VERIFYPEER => FALSE, 
  84. //CURLOPT_SSLVERSION => 2 
  85. ); 
  86.  
  87. curl_setopt_array($ch$options); 
  88. $response = curl_exec($ch); 
  89. $error = curl_error($ch); 
  90.  
  91. curl_close( $ch ); 
  92.  
  93. if (!$response ) { 
  94. throw new Exception( "Error retrieving access token: " . curl_error($ch)); 
  95. $jsonResponse = json_decode($response ); 
  96.  
  97. if ( isset( $jsonResponse->access_token) ) { 
  98. $accessToken = $jsonResponse->access_token; 
  99.  
  100. } catch( Exception $e) { 
  101. throw new Exception($e->getMessage(), 1); 
  102.  
  103. return $accessToken
  104.  
  105. /** 
  106. * get the PayPal user profile, decoded 
  107. * @param string $accessToken 
  108. * @return object 
  109. */ 
  110. public function acquire_paypal_user_profile($accessToken ) { 
  111. try { 
  112. if($this->_sanbox_flag) 
  113. $url = $this->_acquire_user_profile_sandbox_url . $accessToken
  114. else 
  115. $url = $this->_acquire_user_profile_live_url . $accessToken;  
  116.  
  117. $ch = curl_init( $url ); 
  118. $options = array
  119. CURLOPT_RETURNTRANSFER => 1, 
  120. CURLOPT_SSL_VERIFYPEER => FALSE, 
  121. //CURLOPT_SSLVERSION => 2 
  122. ); 
  123. curl_setopt_array($ch$options); 
  124.  
  125. $response = curl_exec($ch); 
  126. $error = curl_error( $ch); 
  127. curl_close( $ch ); 
  128.  
  129. if (!$response )  
  130. return false; 
  131. return json_decode($response); 
  132. } catch( Exception $e ) { 
  133. return false; 
  134. ?> 

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 三门县| 汕尾市| 彭水| 神农架林区| 金山区| 山东省| 明光市| 高密市| 兴和县| 儋州市| 南京市| 威信县| 奉化市| 本溪市| 涟水县| 玉溪市| 山丹县| 康乐县| 乌苏市| 法库县| 永安市| 宁国市| 普兰店市| 太原市| 赫章县| 罗田县| 东阿县| 富阳市| 陈巴尔虎旗| 锦州市| 固原市| 雷山县| 宁陵县| 昔阳县| 驻马店市| 沽源县| 绥化市| 合肥市| 新河县| 塘沽区| 手游|