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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。