您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關利用php怎么實現一個自動登錄微信并獲取昵稱的功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
/** * 獲取當前頁面完整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/oauth3/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/oauth3/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怎么實現一個自動登錄微信并獲取昵稱的功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。