博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信消息自动回复 json版
阅读量:4945 次
发布时间:2019-06-11

本文共 4969 字,大约阅读时间需要 16 分钟。

1 
Content) ? '' : $postSql->Content; //内容 35 36 $openid = $postSql->FromUserName; //用户openID 37 $MsgId = $postSql->MsgId; //消息ID 38 if ($postSql->MsgType == 'event'){ //触发消息 //消息类型 39 if (!isset($postSql->SessionFrom)) exit(); 40 $info = json_decode($postSql->SessionFrom,true); 41 $type = $postSql->MsgType; 42 $data = array( 43 "touser" => $openid, 44 "msgtype" => "link", 45 "link" => array( 46 "title" => '标题', 47 "description" => '内容描述', 48 "url" => "https://www.baidu.com", 49 "thumb_url" => "https://www.baidu.com/icon.png" 50 ) 51 ); 52 $postData = [ 53 'user' => $info['openid'], 54 'qu' => $info['serverUrl'], 55 'time' => $postSql->CreateTime, 56 'data' => json_encode($data), 57 ]; 58 $tb = dns('kefu'); 59 if (!$tb->insertOne($postData)->isAcknowledged()){ 60 writeLog('存库失败'); 61 $tb->insertOne($postData); 62 } 63 64 $sendData = array( 65 "touser" => $openid, 66 "msgtype" => "text", 67 "text" => ['content' => '回复内容'], 68 ); 69 send($sendData); 70 } 71 elseif ($postSql->MsgType == 'text'){ 72 if (isset($postSql->Content) && $postSql->Content === '1'){ 73 $tb = dns('kefu'); 74 $data = $tb->findOne(['user'=>$openid],['sort'=>['time'=>-1]]); 75 if (empty($data) || empty($data['data'])) exit(); 76 send(json_decode($data['data'],true)); 77 if (!$tb->deleteMany(['user'=>$openid]) ) writeLog('删除失败'); 78 } 79 } 80 echo 'success'; 81 } else { 82 // CLog::writeLog('获取token4',$token); 83 echo "123"; 84 exit; 85 } 86 87 } 88 89 /** 90 * 日志记录 91 * @param $ct string 要记录的字符串 92 * @return null 无 93 */ 94 function writeLog($ct,$is = true) //是否强行记录 95 { 96 if (!DEBUG && !$is) return null; 97 $myfile = fopen("logs.txt", "a+"); 98 if (!$myfile) return null; 99 $content = '时间:' . date("Y-m-d H:i:s") . "\r\n------------------------\r\n";100 $content .= $ct . "\r\n";101 $content .= "================================\r\n\r\n";102 fwrite($myfile, $content);103 fclose($myfile);104 return null;105 }106 107 /**108 * 获取token109 * @return bool|string 失败|token110 */111 function access_token()112 {113 $appid = 'wx0000000000000000';114 $secret = '00000000000000000000000000000000';115 $tb = dns('token');116 $token = $tb->findOne(['_id' => 'all']);117 if (empty($token) || $token['time'] < time()) {118 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret"; //获取token地址119 $data = httpGet($url); //发送请求120 $t = json_decode($data, true); //解析数据121 if (empty($t)) {122 writeLog('获取token失败' . json_encode($t));123 return false; //没有数据 返回请求失败124 }125 $tb->updateOne(['_id' => 'all'], ['$set' => ['token' => $t['access_token'], 'time' => time() + 7000]], ['upsert' => true])->isAcknowledged();126 return $t['access_token'];127 }128 return $token['token'];129 }130 131 //get请求132 function httpGet($url)133 {134 return file_get_contents($url);135 }136 137 //post请求138 function httpPost($url, $data = [], $head = [], $outtime = 30) //请求地址139 {140 $ch = curl_init();141 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查142 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在143 curl_setopt($ch, CURLOPT_URL, $url); //设置请求地址144 curl_setopt($ch, CURLOPT_HTTPHEADER, $head); //头信息145 curl_setopt($ch, CURLOPT_POST, true); //post请求146 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data,JSON_UNESCAPED_UNICODE)); //post请求参数147 // curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //post请求参数148 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //149 curl_setopt($ch, CURLOPT_TIMEOUT, $outtime); //允许 cURL 函数执行的最长秒数150 return curl_exec($ch);151 }152 //发送消息153 function send($data){154 $token = access_token();155 if (!$token){156 exit('错误');157 }158 $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={
$token}";159 $head = ["Content-type: text/html; charset=utf-8"];160 $result = httpPost($url, $data, $head);161 writeLog(json_encode($result));162 return null;163 }164 //连接数据库165 function dns($tb){166 //连接数据库167 $dbs = new MongoDB\Client("mongodb://127.0.0.1:27017");168 return $dbs->token->$tb;169 }

 参考地址:

转载于:https://www.cnblogs.com/fanhuo/p/9611215.html

你可能感兴趣的文章
ORA-3136
查看>>
算法笔记_145:拓扑排序的应用(Java)
查看>>
JS获取农历日期
查看>>
PHP中的HTTP协议
查看>>
CSS给文字描边实现发光文字
查看>>
Java WebService入门实例
查看>>
css样式之补充
查看>>
结构与联合
查看>>
BUPT复试专题—众数(2014)
查看>>
20145316 《信息安全系统设计基础》第十四周学习总结
查看>>
Liferay7 BPM门户开发之18: 理解ServiceContext
查看>>
Intel Galileo development documentation
查看>>
EV: Workaround to Allow Only One Instance or Window of outlook
查看>>
数据校验,
查看>>
IntelliJ IDEA完美解决tomcat8+乱码问题
查看>>
破解电信光猫华为HG8120C关闭路由功能方法
查看>>
在Qt示例项目的C ++ / QML源中的//! [0]的含义是什么?
查看>>
【智能家居篇】wifi网络接入原理(上)——扫描Scanning
查看>>
操作引入xml文件的书包(定位到指定节点)
查看>>
操作系统学习笔记系列(一)- 导论
查看>>