微信扫码关注登陆
1 服务器配置
设置与开发-》基本配置
2 获取access_token
//$this->appid 微信appid //$this->AppSecret 微信AppSecret public function get_access_token(){ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->AppSecret; $res = file_get_contents($url); $res=json_decode($res,true); return $res['access_token']; }
3 获取ticket 用于换取微信二维码
$option =[ 'expire_seconds'=>604800, 'action_name'=>'QR_STR_SCENE', 'action_info'=>[ 'scene'=>['scene_str'=>uniqid()] ], ]; //获取微信ticket生成二维码 $res = $this->postUrl('https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token,$option); $imgsrc = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.UrlEncode($res['ticket']); echo '<img src="'.$imgsrc.'">'; debug($res); //curl请求 public function postUrl($url,$data){ $data = json_encode($data); $ch = curl_init(); //初始化CURL句柄 curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST"); //设置请求方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串 $output = curl_exec($ch); curl_close($ch); return json_decode($output,true); }
4 获取用户扫码,微信推送的数据,写入日记查
$xmlData = file_get_contents('php://input'); libxml_disable_entity_loader(true); // 解析该xml字符串,利用simpleXML $xmlToaArr = simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA); //禁止xml实体解析,防止xml注入 $data = json_decode(json_encode($xmlToaArr),true); Log::record($data,'wxpush');
5 微信推送的数据,处理网站的罗辑
微信事件开发文档:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html