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);
openid 昵称 性别 国家 省份 城市 头像 是否关注过 登录时间
本实例源码下载地址:http://www.sucaihuo.com/php/1414.html