PHP version of the micro-letter automatically login and get nicknames _php instance

Source: Internet
Author: User
Tags openid

This article describes the PHP version of the micro-letter automatic login and get a nickname method. Share to everyone for your reference, specific as follows:

Micro-letter Automatic login and get nickname is through the API interface can be obtained through the micro-letter open Interface to achieve, let's take a look at an example

Record only: Micro-credit get nickname Auto Login

After several repeated verification, I found this method is flawed:

The micro-letter did not pay attention to access to the site, can not get nickname.

After paying attention, I can get a nickname with this method.

Is the nickname not generated because of the first generation of OpenID? Pending Test.

/** * Get current page complete URL address/function Get_url () {$sys _protocal = isset ($_server[' Server_port ']) && $_server[' Server_po RT '] = = ' 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 '];
$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 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) {//Sign in $PASSW = MD5 (' 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;

 }

For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

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.