首頁 >後端開發 >php教程 >php實作微信自動登入並取得暱稱的方法實例詳解

php實作微信自動登入並取得暱稱的方法實例詳解

墨辰丷
墨辰丷原創
2018-06-01 10:28:532474瀏覽

這篇文章主要介紹了php版微信自動登錄並獲取暱稱的方法,涉及php針對微信公眾平台接口調用的相關操作技巧,需要的朋友可以參考下

微信自動登錄並獲取暱稱是可以透過api介面來取得的也是透過微信開放的介面來實現了,下面我們一起來看一個範例

僅記錄:微信取得暱稱自動登入

經過重複幾次驗證,發現我這個方法有缺陷:

微信內未關注進入網站,無法獲得暱稱。

關注後用我這個方法可以獲得暱稱。

是否是因為第一次產生openid 所以還未產生暱稱?待測試.

/**
* 获取当前页面完整URL地址
*/
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'];
$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
{
//获取code码,以获取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){ //注册进入
$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(&#39;error get insert_id&#39;);
}
$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[&#39;access_token&#39;];
$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[&#39;nickname&#39;]?$rearr2[&#39;nickname&#39;]: &#39;doubag&#39; . $ecs_user_id;
$time = gmtime();
$user_sql = "INSERT INTO `ecs_users` (`user_name`,`password`,`reg_time`) VALUES (&#39;$uc_username&#39;,&#39;$passw&#39;,&#39;$time&#39;)";
$db -> query($user_sql);
$uc_update = "UPDATE ecs_weixin_user SET `uname` = &#39;$uc_username&#39; WHERE `uid` = &#39;$ecs_user_id&#39;";
$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 = &#39;UPDATE ecs_users SET parent_id = &#39; . $up_uid . &#39; WHERE user_id = &#39; . $ecs_user_id;
$db ->query($sql);
header(&#39;Location: user.php?newuser=1&#39;);
}*/
}else{
$user->set_session($user_name);
$user->set_cookie($user_name,1);
update_user_info();
}
//setcookie("sopenid",$openid,time()+864000,&#39;/&#39;);
}
}
//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;
}

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

PHP實作遞歸目錄的5種方法

php函數mkdir實作遞歸建立層級目錄的方法

PHP下的浮點運算不准的解決方法

##

以上是php實作微信自動登入並取得暱稱的方法實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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