PHP implementation of micro-letter automatic login and get nickname method examples

Source: Internet
Author: User
Tags openid
This article mainly introduces the PHP version of automatic login and get the nickname of the method, involving PHP for the public platform interface calls related operations skills, the need for friends can refer to the following

Automatic login and get nickname can be obtained through API interface is also through the open interface to achieve, let's look at an example

Record only: Get nickname Auto Login

After repeated verification, I found this method is flawed:

not be concerned about entering the website, unable to get a nickname.

Follow me with this method to get a nickname.

Did you generate a nickname for the first time you generated the OpenID? Pending testing.

/*** get the current page full URL address */function get_url () {$sys _protocal = isset ($_server[' Server_port ']) && $_server[' Server_ PORT '] = = ' 443 '? ' 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 ']; $appsec ret = $wxch _config[' Appsecret '); $APPID = $appid; $SCRETID = $appsecret; if (!$_session[' user_id ') && Strpos ($_ server[' http_user_agent '], ' Micromessenger ') {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 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 into $PASSW = MD 5 (' Shanmao.me '. Rand (1,18650144002)); $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 ');} $url 3 = "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= ". $APPID." &secret= ". $SCRETID; $re 3 = Curl_get_contents1 ($url 3); $re 3arr = Json_decode ($re 3,true); $token = $re 3arr[' Access_ Token ']; $url 2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=". $token. " &openid= ". $openid." &AMP;LANG=ZH_CN "; $re 2 = Curl_get_contents1 ($url 2); $rearr 2 = Json_decode ($re 2,true); $uc _username = $rearr 2[' nickname ' ]? $rearr 2[' 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, "IE 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;}

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.