1、生成二维码:
01、生成accessToken并保存到文件中
/* * 封住curl请求方法 * @param { (string) $url } 请求的url * @param { (array) $post_data } 请求的参数 * @param { (boolean) $ispost } post/get 默认post * @param {(boolean) $isJson} 是否发送json数据 */ function send_request($url, $post_data = array(), $ispost = true,$isJson=false) { if (empty($url) || empty($post_data)) { return false; } if($isJson) { $post_data=json_encode($post_data); }else $o = http_build_query($post_data); if (!$ispost) { $post_data =$o; if(!strpos($url,'?')) $url =trim($url . '?' . $post_data); else { $url =trim($url.$post_data); } } //初始化curl $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); //设置header curl_setopt($ch, CURLOPT_HEADER, 0); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($ispost) { //post提交方式 curl_setopt($ch, CURLOPT_POST, 1); // $post_data为array类型时multipart/formdata请求,string类型时application/x-www-form-urlencoded curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($ch);//运行curl curl_close($ch); return $data; } /*得到微信accessToken并写入文件*/ function writeAccessToken($appid,$secret){ $url ='https://api.weixin.qq.com/cgi-bin/token'; $res = send_request($url,array('grant_type'=>'client_credential','appid'=>$appid,'secret'=>$secret),false); $res = json_decode($res,1); if(isset($res['errcode'])) throw new Exception($res['errmsg']); file_put_contents('token.php','<?php exit;?>'.$res['access_token'].','.time()); return $res['access_token']; } /*得到微信accessToken*/ function getAccessToken(){ $file=''; if(file_exists('token.php')) $file=file_get_contents('token.php'); if($file!='') { $content=str_replace('<?php exit;?>','',$file); $arr=explode(',',$content); if(time()-$arr[1]>3000) { return writeAccessToken('wx5f0163b88','a003ed7f659e'); } return $arr[0]; }else { return writeAccessToken('wx5f01641a','a003edcc043bb059e'); } }
02、通过accessToken生成二维码
参考文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433542
/*生成二维码*/ function generrate() { $qrcode_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.getAccessToken(); $post_data = array(); $post_data['expire_seconds'] = 3600 * 24; //有效时间 $post_data['action_name'] = 'QR_SCENE'; $post_data['action_info']['scene']['scene_id'] = 123; //可以用户uid,微信端可获取,用户推广使用 $json =json_decode(send_request($qrcode_url, $post_data,true,true),true); // echo var_dump($json); if (!$json['errcode']) { $ticket = $json['ticket']; echo '<img src=\'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($ticket).'\' width=\'260\' height=\'260\'/>'; } else { echo '发生错误:错误代码 ' . $json['errcode'] . ',微信返回错误信息:' . $json['errmsg']; exit; } }
2、微信公众号配置
参考文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319
配置成功后记得点击启用配置
3、处理推送事件
参考文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140454
代码:
/*处理推送事件*/ function reponseMsg() { $content=file_get_contents("php://input"); if(!empty($content)) { $postObj = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA); file_put_contents('log.txt',date('Y-m-d H:i:s').'\n'.$content); $openid = $postObj->FromUserName; //用户openid $Event=strtolower($postObj->Event); /*当等于subscribe为已关注用户*/ if ($Event == 'subscribe') {//首次关注 $is_first = 0; } elseif ($Event == 'scan') {//已关注 $is_first = 1; } } else { file_put_contents('log.txt',date('Y-m-d H:i:s').'\n无法获取到内容'); exit; } } /*处理微信推送过的信息:注意只有当用户点击关注公众号了或者已经关注了才会推送信息*/ $signature=$_GET['signature']; if($signature!='') { $getInfo=getSHA1('NfzAVfnFD1fv63f4xVtVRNFtRmF1RyRT',$_GET['timestamp'],$_GET['nonce']); $echostr=$_GET['echostr']; if($getInfo[1]!= $signature) { echo '签名失败'; exit; } if( $echostr){ //第一次接入weixin api接口的时候 echo $echostr; exit; }else{ //echo 'adss'; reponseMsg(); } }