php版微信自动登录并获取昵称的方法
程序员文章站
2024-04-03 19:37:16
本文实例讲述了php版微信自动登录并获取昵称的方法。分享给大家供大家参考,具体如下:
微信自动登录并获取昵称是可以通过api接口来获取的也是通过微信开放的接口来实现了,下...
本文实例讲述了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('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; }
更多关于php相关内容感兴趣的读者可查看本站专题:《php微信开发技巧汇总》、《php编码与转码操作技巧汇总》、《php网络编程技巧总结》、《php基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
上一篇: PHP引用返回用法示例