首頁  >  文章  >  後端開發  >  php實現新浪、騰訊、淘寶登陸的程式碼

php實現新浪、騰訊、淘寶登陸的程式碼

WBOY
WBOY原創
2016-07-25 08:56:181075瀏覽
  1. session_start();

  2. class openlogin{
  3. public $_URL = "";
  4. public $config = array();
  5. public function __construct(){

  6. $this->openlogin();
  7. }
  8. function openlogin(){
  9. }

  10. /*取得登陸頁面URL*/

  11. public function login_url(){
  12. if(empty($this ->config)){
  13. 返回false;
  14. }
  15. $config = $this->config;
  16. $login_url = $config['login_url'];
  17. $_SESSION['state'] = $state = md5( uniqid (rand(), TRUE));
  18. $array = array(
  19. "response_type"=>"code",
  20. "state" => $state,
  21. "client_id"= >$config ['appkey'],
  22. "redirect_uri"=>urlencode( $config['redirect_uri'] )
  23. );
  24. $this->set($array);

  25. $url = $this->combineURL($login_url , $this->_param);
  26. if($url){
  27. @header( "位置:".$url);
  28. }else {
  29. return false;
  30. }
  31. }
  32. /*取得access_token*/

  33. public function get_access_token(){
  34. if(empty($this- >config)){
  35. 返回false;
  36. }
  37. $config = $this->config;

  38. if(! $config['code'] = $_REQUEST['code' ] ){

  39. return false;
  40. }
  41. $url = $config['authorization_url'];

  42. $state = $_SESSION['state'];
  43. $array = array(
  44. "grant_type"=>"authorization_code",
  45. " client_id" => $config['appkey'],
  46. "client_secret"=>$config['appsecret'],
  47. "code"=>$config['code'],
  48. "redirect_uri"=>urlencode( $config['redirect_uri'] ),
  49. "state"=>$state
  50. );
  51. $this->set($array);
  52. return $this->post_contents($url);
  53. }
  54. /* 設定$this-> _param 佇列*/
  55. public function set($array) {
  56. if(empty($array)){
  57. return false;
  58. }
  59. $this->_param = array();
  60. foreach($array as $name=>; $value){
  61. $this->_param[$name] = $value;
  62. }
  63. }
  64. /**
  65. * post_contents
  66. * 伺服器透過post請求取得內容
  67. * @param string $url 要求的url,拼接後的
  68. * @return string 要求傳回的內容
  69. */
  70. public function post_contents($url) {
  71. if(empty($url)){
  72. return false;
  73. }
  74. $param = $this->combineURL("" , $this->_param);
  75. $ch =curl_init();
  76. // 設定URL 和對應的選項
  77. curl_setopt($ch, CURLOPT_URL, $url);
  78. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  79. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  80. curl_setopt($ch, CURLOPTPT, 0VER);
  81. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
  82. curl_setopt($ch, CURLOPT_POST, 1);
  83. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  84. // 抓取URL放置其提交給瀏覽器
  85. $reponse = curl_exec($ch);
  86. curl_close($ch);
  87. 回傳$reponse;
  88. }
  89. /**
  90. * get_contents
  91. * 伺服器透過get請求取得內容
  92. * @param string $url 要求的url,拼接後的
  93. * @return string 要求傳回的內容
  94. */
  95. public function get_contents($url){
  96. $ch =curl_init();
  97. curl_setopt($ch, CURLOPT_init();
  98. curl_setopt($ch, CURLOPT_init();
  99. curl_setopt($ch, CURLOPT_SSL_VERIFPT_SSL_PERcurl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  100. curl_setopt($ch, CURLOPT_URL, $url);
  101. $response =curl_exec($ch);
  102. curl_close($chch); ;
  103. //-------請求為空

  104. if(empty($response)){
  105. return false;
  106. }
  107. 回傳$response;

  108. }
  109. /**

  110. * combineURL
  111. * 拼接url
  112. * @param string $baseURL 基於的url
  113. * @param array $keysArr 參數列表數組
  114. * @return string 返回的url
  115. */
  116. public function mergeURL($baseURL,$keysArr){
  117. if( $baseURL== " " ){
  118. $combined = "";
  119. }else{
  120. $combined = $baseURL."?";
  121. }
  122. $valueArr = array();
  123. foreach($keysArr as $key => $val){

  124. $valueArr[] = "$key=$val";
  125. }
  126. $ keyStr = implode("&",$valueArr);

  127. $combined .= ($keyStr);
  128. return $combined;
  129. }
  130. } ;
  131. //php實作QQ登入

  132. class qq_openlogin extends openlogin{
  133. private $openname = "qq";
  134. public $config = array(
  135. "appkey"=> ;"你的應用智慧",
  136. "appsecret"=>"你的應用程式秘密",
  137. "redirect_uri"=>"XXXXX",
  138. "login_url" => "https://graph.qq; .com/oauth2.0/authorize",
  139. "scope"=>"get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo,check_page_fans,add_t,addt,d,del_list, get_other_info,get_fanslist,get_idolist,add_idol,del_idol,get_tenpay_addr",
  140. "authorization_url"=>"https://graph.qq.com/oauth2.0/token"
  141. );
  142. 函數__construct()

  143. {
  144. $this->qq_openlogin();
  145. }
  146. 函數__construct(){
  147. parent::__construct();
  148. }< ;/p>
  149. function get_access_token(){

  150. $response = Parent::get_access_token();
  151. /*偵測是否發生錯誤*/
  152. if (strpos ($response, "callback") !== false ){
  153. $lpos = strpos($response, "(");

  154. $rpos = strrpos($response, ") ");
  155. $response = substr($response, $lpos + 1, $rpos - $lpos -1);
  156. $msg = json_decode($response);
  157. if(isset($msg->error)){
  158. return false;
  159. }
  160. }
  161. $params = array();

  162. parse_str( $response , $params);
  163. /*access_token == $params[access_token]*/
  164. /*取得openid */
  165. $ response = $this->get_contents("https:/ /graph .qq.com/oauth2.0/me?access_token=".$params['access_token']);
  166. ; //--------偵測錯誤是否發生

  167. if(strpos($response, "callback") !== false){
  168. $lpos = strpos($回應, "(");

  169. $rpos = strrpos($response, ")");
  170. $response = substr($response, $lpos + 1, $rpos - $lpos -1);
  171. }
  172. $user = json_decode($response);

  173. if(isset($user->error)){
  174. return false;
  175. }
  176. /*

  177. 取得使用者資訊需要參數:openid(使用者的ID,與QQ號碼一一對應),access_token(可透過使用Authorization_Code取得Access_Token 或來取得access_token有3個月有效期限),oauth_consumer_key(用戶appid),format(返回格式)
  178. */
  179. /*資料庫儲存*/
  180. $open_param = array(
  181. "openid"=>$user ->openid,
  182. "access_token"=>$params['access_token']
  183. );
  184. //
  185. $open_param['oauth_consumer_key'] = $this->config['appkey'] ;
  186. $open_param['format'] = "json";
  187. /*拼接url*/
  188. $get_user_url = $this->combineURL("https://graph.qq.com/user/ get_user_info",$open_param);
  189. //獵取使用者資訊
  190. $userinfo = $this->get_contents($get_user_url);
  191. $userinfo = json_decode($userinfo);

  192. return $userinfo;

  193. }
  194. }
  195. //php實作微博登入

  196. class weibo_openlogin extends openlogin{
  197. private $openname = "weibo";
  198. public $config = array(
  199. "appkey"=>"your appkey",
  200. "appsecret"=>"your appsecret",
  201. "login_url"appsecret"=>"your appsecret",
  202. "login_url"appsecret"=>"your appsecret",
  203. "login_url"appsecret"=>"your appsecret",
  204. "login_url"appsecret"=>"your appsecret",
  205. "login_url" > "https://api.weibo.com/oauth2/authorize",
  206. "redirect_uri"=>"XXXXXXX",
  207. "authorization_url"=>"https://api.weibo.com/oauth2/ access_token"
  208. );
  209. function __construct()

  210. {
  211. $this->qq_openlogin();
  212. }
  213. function qq_openin🎜> }
  214. function qq_openin🎜> {
  215. parent::__construct();
  216. }
  217. function get_access_token(){

  218. $response = parent::get_access_token();
  219. $userinfo = jdecode ($response);
  220. return $userinfo;
  221. }
  222. }
  223. //php實作淘寶登入

  224. class taobao_openlogin extends openlogin{
  225. private $openname> private $name = "taobao";
  226. public $config = array(
  227. "appkey"=>"your appkey",
  228. "appsecret"=>"your appsecret",
  229. "redirect_uri"=>"XXXXX ",
  230. "authorization_url"=>"https://oauth.taobao.com/token",
  231. "login_url"=>"https://oauth.taobao.com/authorize"
  232. );
  233. function __construct()

  234. {
  235. $this->qq_openlogin();
  236. }
  237. function qq_openlogin(){
  238. parent::🎜> parent:: __construct();
  239. }
  240. function get_access_token(){

  241. $response = parent::get_access_token();
  242. $userinfo = json_decode($response);
  243. return $userinfo;
  244. }
  245. }

  246. if($_GET['openname']){
  247. $openname = $_GET['openname']."_openlogin ";
  248. $openlogin = new $openname();
  249. if(!isset($_REQUEST['code'])){
  250. //請求url
  251. $url = $openlogin->login_url ();
  252. if(!$url){
  253. echo "0";
  254. exit();
  255. }
  256. }else{
  257. if(isset($_REQUEST["state "]) && ($_SESSION['state'] != $_REQUEST["state"] )){
  258. echo "1";
  259. exit();
  260. }
$ rs = $openlogin->get_access_token();
print_r( $rs ); }}
?>複製程式碼

附,人人網路登陸代碼。

  1. class renren_openlogin extends openlogin{

  2. private $openopenname = "renren" "appid"=>"你的appid",
  3. "appkey"=>"你的appkey",
  4. "appsecret"=>"你的金鑰",
  5. "redirect_uri"=>"XXXXXX",
  6. "authorization_url"=>"https://graph.renren.com/oauth/token",
  7. "login_url"=>"https: //graph.renren .com/oauth/authorize"
  8. );
  9. function __construct()

  10. {
  11. $this->qq_openlogin();
  12. }
  13. function qq_openlogin(){
  14. parent::__construct();
  15. }< ;/p>
  16. function get_access_token(){

  17. $response = Parent::get_access_token()
  18. $userinfo = json_decode($response);

  19. return $userinfo;

  20. /*
  21. access_token:取得的Access Token;
  22. expires_in:Access Token的數量,以秒為單位;
  23. refresh_token:用於刷新Access Token的刷新Token,長期有效,不會過期;
  24. 範圍:Access Token最終的訪問範圍,既是用戶實際獲取的權限清單(使用者在授權頁面時,有可能會取消某些要求的權限)。關於權限的具體資訊請參考
  25. */
  26. }
  27. /*取得登陸頁面URL*/

  28. public function login_url(){
  29. if (empty($this->config)){
  30. return false;
  31. }
  32. $config = $this->config;
  33. $login_url = $config['login_url'];
  34. $array = array(
  35. "response_type"=> ;"code",
  36. "client_id"=>$config['appid'],
  37. "redirect_uri"=>urlencode( $config[ 'redirect_uri'] )
  38. );> ;
  39. $this->set($array);

  40. $url = $this->combineURL($ login_url , $this->_param);

  41. if($url){

  42. @header("位置:".$url);
  43. }else{
  44. return false;
  45. }
  46. }
  47. }
複製程式碼

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn