发布时间:2023-03-11
1199
0
验证完后,取消验证,进行相关事件处理
<?php responseMsg(); /* 用于第一次验证我们网站url合法性 */ function firstValid(){ // echo(1); //检验签名的合法性 if(_checkSignature()){ //签名合法,告知微信公众平台服务器 echo $_GET['echostr']; } } /** * 验证签名 * @return bool */ function _checkSignature(){ //获得由微信公众平台请求的验证数据 $signature = $_GET['signature']; $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; //将时间戳,随机字符串,token按照字母顺序排序,病并连接 $tmp_arr = array('这是token',$timestamp,$nonce); sort($tmp_arr,SORT_STRING);//字典顺序 $tmp_str = implode($tmp_arr);//连接 $tmp_str = sha1($tmp_str);//sha1加密 if($signature==$tmp_str){ return true; }else{ return false; } } function responseMsg(){ //读取数据 $xml = file_get_contents('php://input'); if(!empty($xml)){ //将xml数据转换成对象 $postObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA); $toUser = $postObj->FromUserName; $formUser = $postObj->ToUserName; $time = time(); //判断用户传入的类型 (文本 图文) if($postObj->MsgType == 'text' && $postObj->Content == '自定义的关键词回复才会触发'){ $unionid = unionid($toUser); $url = '学员api'.$unionid; $res = getJson($url); if ($res['data']['member_info'] != null ) { $realname = $res['data']['member_info']['realname']; // 名称 $grade_name = $res['data']['member_info']['grade_name']; // 年级 $major_name = $res['data']['member_info']['major_name']; // 专业 // 查询数据库 $db=new mysqli('xxxxxx','xxxxxxxx','xxxxxxxxx','xxxxxxxxx'); if(mysqli_connect_error()){ putMark('Could not connect to database.'); exit; } $sql = "SELECT picurl FROM zf_kefu WHERE nianji = '".$grade_name."' AND zhuanye LIKE '%".$major_name."%' AND type = '客服' LIMIT 1"; $sqlis = "SELECT picurl FROM zf_kefu WHERE nianji = '".$grade_name."' AND zhuanye LIKE '%".$major_name."%' AND type = '客服' AND ispost = 0 limit 1"; $result = $db->query($sql); $row=$result->fetch_row(); if ($row) { $result = $db->query($sqlis); $row=$result->fetch_row(); if ($row) { $picurl = implode('',$row); } else { $db->query("update zf_kefu set ispost = 0 where type = '客服'"); $result = $db->query($sqlis); $row=$result->fetch_row(); $picurl = implode('',$row); } }else{ // 这里可以返回一个随意的客服 putMark('数据库获取该学员专属客服失败'.$realname.'-'.$grade_name.'-'.$major_name); } $title1 = '标题'; $description1 = '内容~'; $url = $picurl; imgtxt($toUser,$formUser,$time,$title1,$description1,$picurl,$url); $db->query("update zf_kefu set ispost = 1 where picurl = '".$picurl."'"); }else{ putMark('学信查询api无unionid'.$unionid); } // $content = 'xxxxxxxxx-xxxxxxxxx'; // $content_tezt = 'xxxxxxxxxxxx'; // text($toUser,$formUser,$time,$content_tezt); // img($toUser,$formUser,$time,$content); } } } function unionid($toUser){ $accessToken = accessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$accessToken.'&openid='.$toUser.'&lang=zh_CN'; $res = getJson($url); $unionid = $res['unionid']; if($unionid) { return $unionid; } } function text($toUser,$formUser,$time,$content) { $templade = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; echo sprintf($templade,$toUser,$formUser,$time,$content); } function img($toUser,$formUser,$time,$content) { $templade = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>"; echo sprintf($templade,$toUser,$formUser,$time,$content); } function imgtxt($toUser,$formUser,$time,$title1,$description1,$picurl,$url){ $templade = " <xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>1</ArticleCount> <Articles> <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> </Articles> </xml>"; echo sprintf($templade,$toUser,$formUser,$time,$title1,$description1,$picurl,$url); } function putMark($params){ # 记录入参 $time = date('Y-m-d h:i:s',time()); // $paramsStr = json_encode($params,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT); $data = $time.'---'.$params; $address = 'log.log'; file_put_contents($address,$data."\r\n",FILE_APPEND); } function accessToken() { $tokenFile = "access_token.txt";//緩存文件名 $data = json_decode(file_get_contents($tokenFile)); if ($data->expires_in < time() == 1 or !$data->expires_in) { $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxxxxxxxxxx'; $res = getJson($url); $access_token = $res['access_token']; if($access_token) { $data = []; $data['expires_in'] = time() + 7200; $data['access_token'] = $access_token; $fp = fopen($tokenFile, "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } function getJson($url){ $headerArray =array("Content-type:application/json;","Accept:application/json"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } function postJson($url,$data){ $ch = curl_init(); $headers = array( "Content-type: application/json;charset='utf-8'", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache", ); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $ret = curl_exec($ch); curl_close($ch); $ret = json_decode($ret,true); return $ret; }
发表评论
留言: