您好,登錄后才能下訂單哦!
PHP微信掃碼登錄看起來簡單,但做起來有點復雜,微信接口的坑有點多,稍不注意,就費很多時間。PHP判斷是否首次關注公眾號,掃碼關注公眾號獲取微信用戶頭像、openid和省市等信息源碼。
$sql = "INSERT INTO `qrcode` (`addtime`) VALUES ('" . time() . "')"; mysql_query($sql); $scene_id = mysql_insert_id(); $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret; $access_token_array = json_decode(curlGet($url), true); $access_token = $access_token_array['access_token']; //echo $access_token;exit;http://www.sucaihuo.com/project/wxvalid/index.php $qrcode_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . $access_token; $post_data = array(); $post_data['expire_seconds'] = 3600 * 24; //有效時間 $post_data['action_name'] = 'QR_SCENE'; $post_data['action_info']['scene']['scene_id'] = $scene_id; //傳參用戶uid,微信端可獲取 $json = curlPost($qrcode_url, json_encode($post_data)); if (!$json['errcode']) { $ticket = $json['ticket']; $ticket_img = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($ticket); } else { echo '發生錯誤:錯誤代碼 ' . $json['errcode'] . ',微信返回錯誤信息:' . $json['errmsg']; exit; }
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $scene_id = str_replace("qrscene_", "", $postObj->EventKey); $openid = $postObj->FromUserName; //openid $ToUserName = $postObj->ToUserName; //轉換角色 $Event = strtolower($postObj->Event); if ($Event == 'subscribe') {//首次關注 $is_first = 0; } elseif ($Event == 'scan') {//已關注 $is_first = 1; } $access_token = $this->getAccessToken(); $userinfo = $this->getUserinfo($openid, $access_token); $sql = "UPDATE `qrcode` SET `openid` = '" . $openid . "',logintime='" . time() . "',is_first=" . $is_first . ",nickname='" . $userinfo['nickname'] . "'" . ",avatar='" . $userinfo['avatar'] . "',sex='" . $userinfo['sex'] . "',province='" . $userinfo['province'] . "',city='" . $userinfo['city'] . "',country='" . $userinfo['country'] . "' WHERE `id` =" . $scene_id . ""; mysql_query($sql);
<script> check_login(); function check_login() { $.post("check_login.php", {scene_id: <?php echo $scene_id; ?>}, function(data) { var attention = '首次關注'; if (data.is_first == 1) { attention = '已關注過'; } if (data.openid != null) { alert("掃碼成功,openid為" + data.openid + "nickname:" + data.nickname); location.reload(); } else { setTimeout("check_login()", 2000); } }, "json"); } </script>
<tr> <th>openid</th> <th>昵稱</th> <th>性別</th> <th>國家</th> <th>省份</th> <th>城市</th> <th>頭像</th> <th>是否關注過</th> <th>登錄時間</th> </tr>
本實例源碼下載地址:http://www.sucaihuo.com/php/1414.html
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。