. 1 ? < PHP 2 // write several functions, respectively, for acquiring code, token, openid, the user information . 3 . 4 // jump to the login page QQ authorization . 5 function code () { . 6 $ response_type = 'code' ; . 7 client_id $ = '101 353 491' ; . 8 $ the redirect_uri = 'HTTP: //www.iwebshop.com/index.php' ; . 9 $ State = 'dfs343df' ; 10 . 11 $ URL = "https://graph.qq.com /oauth2.0/authorize?response_type= $ response_type & client_id = $ client_id & redirect_uri = $ redirect_uri & State =State $ " ; 12 is // used function jump header 13 is header (" LOCATION: $ URL " ); 14 } 15 16 function token () { . 17 // . 1 requests parameters 18 is $ grant_type = 'authorization_code' ; . 19 $ client_id = '101 353 491' ; 20 is $ client_secret = 'df4e46ba7da52f787c6e3336d30526e4' ; 21 is $ code = $ _GET [ 'code']; // receiving the address bar code parameters, the code is the Authorization 22 is $ the redirect_uri= 'HTTP: //www.iwebshop.com/index.php' ; 23 is 24 // 2 construct a complete, correct address of the interface 25 $ URL = "https://graph.qq.com/oauth2.0/ ? token grant_type = $ grant_type & client_id = $ client_id & client_secret = $ client_secret & code = $ code & the redirect_uri = $ the redirect_uri " ; 26 is 27 // . 3 to above $ url retransmission request (request interface) to retrieve data 28 $ STR = file_get_contents ( $ url ); 29 //Value (data interface) return code above get is: access_token = DC5C4AF94719CB5DE6A6EF1570A1B968 & expires_in = 7776000 & refresh_token = 8E946C30FC46D6BD2C4CC17055B6532D, we only need to value access_token, that DC5C4AF94719CB5DE6A6EF1570A1B968, how to do that, we first find the position on the left side of an "=" symbol ( strpos function), to find a first left "&" position (strpos function), taken out after use substr function required values 30 $ left = strpos ( $ STR , '='); // the string $ str left start position of first obtaining a "=" symbol 31 is $ right = the strpos ( $ str , '&' ); 32 $ token = substr ( $ str , $ left + 1'd, $ right - $ left -1) ; //Acquire the content between the "=" symbol and the "&" symbol, i.e., the access token value 33 is // echo $ token; 34 is 35 // openid function call. 4, using the access token to obtain further values openid 36 openid ( $ token ) ; 37 [ 38 is } 39 40 function OpenID ( $ token ) { 41 is $ url = "https://graph.qq.com/oauth2.0/me?access_token= $ token " ; 42 is // send a request to the above $ url acquiring data 43 is $ STR = file_get_contents ( $ URL ); 44 is //After the request interface, the value obtained is: callback ({ "client_id": "101353491", "openid": "7429C3FDC8FA70FEF3252FF47D6CDDA3"});, we only need openid value, that 7429C3FDC8FA70FEF3252FF47D6CDDA3, then how do we do? First access "(" position, and then acquiring the right ")," the left position, after use substr access "(" and ")" between the string, the string is a string json format, then you use this string is converted into json_decode PHP array, to a value obtained openid 45 $ left = the strpos ( $ STR , '(' ); 46 is $ right = strrpos ( $ STR , ')' ); 47 $ STR = substr ( $ STR , $ left + 1'd, $ right - $ left -1); //Intercepting a complete json format string { "client_id": "101 353 491", "OpenID": "7429C3FDC8FA70FEF3252FF47D6CDDA3"} 48 $ Data = of json_decode ( $ STR , to true ); 49 // echo '<pre />'; 50 print_r // ($ Data); Die; 51 is / * 52 is the Array 53 is ( 54 is [client_id] => 101 353 491 55 [OpenID] => 7429C3FDC8FA70FEF3252FF47D6CDDA3 56 is ) 57 is * / 58 $ OpenID = $ Data [ 'OpenID' ]; 59 // call the function userInfo further acquire user information 60 userInfo($openid,$token); 61 } 62 63 function userInfo($openid,$token){ 64 $client_id='101353491'; 65 $url="https://graph.qq.com/user/get_user_info?access_token=$token&oauth_consumer_key=$client_id&openid=$openid"; 66 // 向上面的$url发请求,获取数据 67 $str=file_get_contents($url); 68 //echo $str; 69 /*{ "ret": 0, "msg": "", "is_lost":0, "nickname": "白雪峰", "gender": "男", "province": "辽宁", "city": "大连", "year": "1983", "constellation": "", "figureurl": "http:\/\/qzapp.qlogo.cn\/qzapp\/101353491\/7429C3FDC8FA70FEF3252FF47D6CDDA3\/30", "figureurl_1": "http:\/\/qzapp.qlogo.cn\/qzapp\/101353491\/7429C3FDC8FA70FEF3252FF47D6CDDA3\/50", "figureurl_2": "http:\/\/qzapp.qlogo.cn\/qzapp\/101353491\/7429C3FDC8FA70FEF3252FF47D6CDDA3\/100", "figureurl_qq_1": "http:\/\/thirdqq.qlogo.cn\/qqapp\/101353491\/7429C3FDC8FA70FEF3252FF47D6CDDA3\/40", "figureurl_qq_2": "http:\/\/thirdqq.qlogo.cn\/qqapp\/101353491\/7429C3FDC8FA70FEF3252FF47D6CDDA3\/100", "is_yellow_vip": "0", "vip": "0", "yellow_vip_level": "0", "level": "0", "is_yellow_year_vip": "0" } */ 70 $data=json_decode($str, To true ); 71 is // echo '<pre />'; 72 // print_r ($ Data); Die; 73 is / * 74 the Array 75 ( 76 [RET] => 0 77 [MSG] => 78 [is_lost] => 0 79 [Nickname] => red belly spines 80 [Gender] => M 81 [Province] => Liaoning 82 [City] => Dalian 83 [year] => 1983 84 [Constellation] => 85 [figureurl] => http://qzapp.qlogo.cn/qzapp/101353491/7429C3FDC8FA70FEF3252FF47D6CDDA3/30 86 [figureurl_1] => http://qzapp.qlogo.cn/qzapp/101353491/7429C3FDC8FA70FEF3252FF47D6CDDA3/50 87 [figureurl_2] => http://qzapp.qlogo.cn/qzapp/101353491/7429C3FDC8FA70FEF3252FF47D6CDDA3/100 88 [figureurl_qq_1] => http://thirdqq.qlogo.cn/qqapp/101353491/7429C3FDC8FA70FEF3252FF47D6CDDA3/40 89 [figureurl_qq_2] => http://thirdqq.qlogo.cn/qqapp/101353491/7429C3FDC8FA70FEF3252FF47D6CDDA3/100 90 [is_yellow_vip] => 0 91 [vip] => 0 92 [yellow_vip_level] => 0 93 [level] => 0 94 [is_yellow_year_vip] => 0 95 ) 96 * / 97 $ Nickname = $ Data [ 'Nickname']; // Nick 98 $ Figure = $ Data [ 'figureurl_qq_1']; // head 99 echo $ Nickname ; 100 echo 'a' ; 101 echo "< the src = IMG ' $ Figure '> " ; 102 } 103 104 // function or method that he himself is not performed, it is necessary to call at 105 IF ( isset ( $ _GET [ 'code' ])) { 106 token (); 107 }else{ 108 code(); 109 }
1 <html> 2 <head> 3 <title></title> 4 </head> 5 <body> 6 <form> 7 <p>用户名 <input type='text' name='name'></p> 8 <p>密码 <input type='text' name='password'></p> . 9 < P > < INPUT type = 'Submit' value = 'logon' > 10 <! - QQ icon -> . 11 < IMG the src = "qq.png" the onclick = 'Login ();' > 12 is 13 is </ P > 14 </ form > 15 </ body > 16 </ HTML > . 17 18 is < Script > . 19 function Login () { 20 is // click QQ login icon to jump to index.php page 21 location.href='index.php'; 22 } 23 </script>