php版微信自動(dòng)登錄并獲取昵稱的方法
本文實(shí)例講述了php版微信自動(dòng)登錄并獲取昵稱的方法。分享給大家供大家參考,具體如下:
微信自動(dòng)登錄并獲取昵稱是可以通過(guò)api接口來(lái)獲取的也是通過(guò)微信開放的接口來(lái)實(shí)現(xiàn)了,下面我們一起來(lái)看一個(gè)例子
僅記錄:微信獲取昵稱自動(dòng)登錄
經(jīng)過(guò)反復(fù)幾次驗(yàn)證,發(fā)現(xiàn)我這個(gè)方法有缺陷:
微信內(nèi) 未關(guān)注進(jìn)入網(wǎng)站,無(wú)法獲得昵稱。
關(guān)注后用我這個(gè)方法可以獲得昵稱。
是否是因?yàn)榈谝淮紊蒾penid 所以還未生成昵稱?待測(cè)試.
/**
* 獲取當(dāng)前頁(yè)面完整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){ //注冊(cè)進(jìn)入
$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');
}
$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, "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;
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP微信開發(fā)技巧匯總》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- 利用php實(shí)現(xiàn)一周之內(nèi)自動(dòng)登錄存儲(chǔ)機(jī)制(cookie、session、localStorage)
- php中實(shí)現(xiàn)記住密碼下次自動(dòng)登錄的例子
- php中實(shí)現(xiàn)記住密碼自動(dòng)登錄的代碼
- phpMyAdmin自動(dòng)登錄和取消自動(dòng)登錄的配置方法
- php利用cookie實(shí)現(xiàn)自動(dòng)登錄的方法
- PHP登錄驗(yàn)證功能示例【用戶名、密碼、驗(yàn)證碼、數(shù)據(jù)庫(kù)、已登陸驗(yàn)證、自動(dòng)登錄和注銷登錄等】
- PHP基于cookie與session統(tǒng)計(jì)網(wǎng)站訪問(wèn)量并輸出顯示的方法
- php頁(yè)面跳轉(zhuǎn)session cookie丟失導(dǎo)致不能登錄等問(wèn)題的解決方法
- PHP中cookie和session的區(qū)別實(shí)例分析
- PHP根據(jù)session與cookie用戶登錄狀態(tài)操作類的代碼
- php同時(shí)使用session和cookie來(lái)保存用戶登錄信息的實(shí)現(xiàn)代碼
- PHP cookie,session的使用與用戶自動(dòng)登錄功能實(shí)現(xiàn)方法分析
相關(guān)文章
thinkphp5+layui實(shí)現(xiàn)的分頁(yè)樣式示例
這篇文章主要介紹了thinkphp5+layui實(shí)現(xiàn)的分頁(yè)樣式,結(jié)合實(shí)例形式詳細(xì)分析了thinkPHP5框架結(jié)合layui實(shí)現(xiàn)的分頁(yè)功能相關(guān)的配置、查詢等操作技巧,需要的朋友可以參考下2019-10-10
將PHP的session數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中的代碼實(shí)例
這里我們將分享兩個(gè)將PHP的session數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中的代碼實(shí)例,分別針對(duì)PostgreSQL與MySQL,需要的朋友可以參考下2016-06-06
LAMP環(huán)境使用Composer安裝Laravel的方法
這篇文章主要介紹了LAMP環(huán)境使用Composer安裝Laravel的方法,簡(jiǎn)單分析了Ubuntu服務(wù)器環(huán)境下Composer的安裝及使用Composer安裝Laravel的相關(guān)命令與使用技巧,需要的朋友可以參考下2017-03-03

