Php automatic login and get nickname method, automatic login nickname
This example describes how to automatically log on to php and obtain a nickname. We will share this with you for your reference. The details are as follows:
Automatic logon and nickname retrieval can be obtained through the api interface and through the open interface. Let's take a look at an example.
Record only: Get nickname Automatic Login
After several verification attempts, I found that this method is defective:
And cannot get a nickname.
You can use this method to get a nickname after you follow it.
Is a nickname not generated because openid is generated for the first time? To be tested.
/*** Get the complete URL of the current page */function get_url () {$ sys_protocal = isset ($ _ SERVER ['server _ port']) & $ _ SERVER ['server _ port'] = '000000 '? 'Https: // ': 'http: //'; $ php_self = $ _ SERVER ['php _ SELF ']? $ _ SERVER ['php _ SELF ']: $ _ SERVER ['script _ name']; $ path_info = isset ($ _ SERVER ['path _ info'])? $ _ SERVER ['path _ info']: ''; $ relate_url = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: $ php_self. (isset ($ _ SERVER ['query _ string'])? '? '. $ _ SERVER ['query _ string']: $ path_info); return $ sys_protocal. (isset ($ _ SERVER ['HTTP _ host'])? $ _ SERVER ['HTTP _ host']: ''). $ relate_url;} $ wxch_config = $ db-> getRow ("SELECT * FROM 'ecs _ weixin_config 'WHERE 'id' = 1 "); $ appid = $ wxch_config ['appid ']; $ appsecret = $ wxch_config ['appsecret']; $ appid = $ APPID; $ SCRETID = $ appsecret; if (! $ _ SESSION ['user _ id'] & strpos ($ _ SERVER ['HTTP _ USER_AGENT '], 'micromessenger ')! = False) {if (! Isset ($ _ GET ['code']) {$ backurl = get_url (); // $ url = $ jsApi-> createOauthUrlForCode ($ backurl ); $ url = "https://open.weixin.qq.com/connect/oauth2/authorize? Appid = ". $ APPID. "& redirect_uri = ". urlencode ($ backurl ). "& response_type = code & scope = snsapi_base & state = 123 # wechat_redirect"; // echo $ url; Header ("Location: $ url ");} else {// GET code to GET openid $ code =$ _ GET ['code']; $ url = "https://api.weixin.qq.com/sns/oauth2/access_token? Appid = ". $ APPID. "& secret = ". $ SCRETID. "& code = ". $ code. "& grant_type = authorization_code"; $ re = curl_get_contents1 ($ url); $ rearr = json_decode ($ re, true); $ openid = $ rearr ['openid']; // var_dump ($ rearr); // $ jsApi-> setCode ($ code); // $ openid = $ jsApi-> getOpenid (); $ user_name = $ db-> getOne ("select uname from ecs_weixin_user where wxid = '{$ openid}'"); if ($ openid &&! $ User_name) {// register to enter $ passw = md5 ('shanmao. me '. rand (); $ wxch_user_ SQL = "INSERT INTO 'ecs _ weixin_user '('wxid', 'setp') VALUES (' $ openid', '3 ')"; $ db-> query ($ wxch_user_ SQL); $ ecs_user_id = $ db-> insert_id (); if ($ ecs_user_id <= 0) {exit ('error get insert_id ');} $ url3 = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". $ APPID. "& secret = ". $ SCRETID; $ re3 = curl_get_contents1 ($ url3); $ re3arr = json_decode ($ re3, true); $ token = $ re3arr ['Access _ token']; $ url2 = "https://api.weixin.qq.com/cgi-bin/user/info? Access_token = ". $ token. "& openid = ". $ openid. "& lang = zh_CN"; $ re2 = curl_get_contents1 ($ url2); $ rearr2 = json_decode ($ re2, true); $ uc_username = $ rearr2 ['nickname']? $ Rearr2 ['nickname']: 'doubag '. $ ecs_user_id; $ time = gmtime (); $ user_ SQL = "INSERT INTO 'ecs _ users' ('user _ name', 'Password', 'reg _ Time ') VALUES ('$ uc_username', '$ passw', '$ Time') "; $ db-> query ($ user_ SQL ); $ uc_update = "UPDATE ecs_weixin_user SET 'uname' = '$ uc_username 'where 'uid' =' $ ecs_user_id '"; $ db-> query ($ uc_update ); $ user-> set_session ($ uc_username); $ user-> set_cookie ($ uc_username, 1); update_user _ Info ();/* $ up_uid = get_affiliate (); if ($ up_uid> 0) {$ SQL = 'Update ecs_users SET parent_id = '. $ up_uid. 'Where user_id = '. $ ecs_user_id; $ db-> query ($ SQL); header ('location: user. php? Newuser = 1');} */} else {$ user-> set_session ($ user_name); $ user-> set_cookie ($ user_name, 1); update_user_info ();} // setcookie ("sopenid", $ openid, time () + 864000, '/') ;}// var_dump ($ openid); function curl_get_contents1 ($ url) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_TIMEOUT, 2); curl_setopt ($ ch, CURLOPT_USERAGENT, "Internet Explorer 6.0"); curl_setopt ($ ch, CURLOPT_REFERER, ""); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false); $ r = curl_exec ($ ch); curl_close ($ ch); return $ r ;}