这就比较好玩了
有个公众号不知道 拿来斗图吧、
第一步、微信配置接受消息的服务地址
https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=1886184966&lang=zh_CN
第二步、服务器验证token
//检测token 每次修改后台配置的时候会用到
function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$echostr = $_GET["echostr"];
$token = "duoduo888";
// 1、将token、timestamp、nonce三个参数进行字典序排序
$tmpArr = array($nonce, $token, $timestamp);
sort($tmpArr, SORT_STRING);
// 2、将三个参数字符串拼接成一个字符串进行sha1加密
$str = implode($tmpArr);
$sign = sha1($str);
// 3、开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
if ($sign == $signature) {
return $echostr;
}
return "";
}
//点击后台配置走这里
if ($_GET["echostr"]) {
return checkSignature();
}
第三步、拿到消息处理数据
include_once "XMLParse.php";
include_once "simple_html_dom.php";
include_once "WXBizMsgCrypt.php";
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$msg = '';
$pc = new WXBizMsgCrypt("duoduo888", "EDOO5bhPnGcpHU3Zt6vH42XWIYqhhlbF7E", "wx8b98ce915a6e3aa8");
//微信调取该接口时候携带的参数 demo中的解密方法 得到msg
$errCode = $pc->decryptMsg($_GET["msg_signature"], $_GET["timestamp"], $_GET["nonce"], $postStr, $msg);
//解出msg的内容
$xml_tree = new DOMDocument();
$xml_tree->loadXML($msg);
$fromUser = $xml_tree->getElementsByTagName('FromUserName')->item(0)->nodeValue;
$toUser = $xml_tree->getElementsByTagName('ToUserName')->item(0)->nodeValue;
$createTime = $xml_tree->getElementsByTagName('CreateTime')->item(0)->nodeValue;
$msgType = $xml_tree->getElementsByTagName('MsgType')->item(0)->nodeValue;
$content = $xml_tree->getElementsByTagName('Content')->item(0)->nodeValue;
$msgId = $xml_tree->getElementsByTagName('MsgId')->item(0)->nodeValue;
$time = time();
$access_token = "";
$textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content></xml>";
$imgTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Image> <MediaId><![CDATA[%s]]></MediaId></Image>
</xml>";
if ($msgType == "text") {
if ($content == "【收到不支持的消息类型,暂无法显示】") {//自定义表情
$resultStr = sprintf($textTpl, $fromUser, $toUser, $time, "text", "我看不懂这些" . $msgType);
} else if (substr($content, 0, 1) == "#") {//斗图
$content = substr($content, 1, strlen($content));
//搜一张图片
$searchImg = "http://www.adoutu.com/search?keyword=" . urlencode($content);
//爬虫爬出来一张图片
$html = new simple_html_dom();
$html->load(file_get_contents($searchImg));
$rnd = rand(0, 15);
$ret = $html->find(".col-sm-3 img", $rnd);
//保存到本地
$path = getimg($ret->src, $rnd);
//获取token todo 存本地
$result = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx8b98ce915a6e3aa8&secret=6fc27aee4dbfff2f3413c6342exxxxx");
$access_token = json_decode($result, true)["access_token"];
$file_info = array(
'filename' => $path, //图片相对于网站根目录的路径
);
//上传文件到临时素材
$r = add_material($file_info, $access_token);
if ($r != false) {
$arr = json_decode($r, true);
$mediaId = $arr["media_id"];//获取到素材id
$resultStr = sprintf($imgTpl, $fromUser, $toUser, $time, "image", $mediaId);//封装图片消息格式
} else {
//素材获取失败的情况 调用茉莉机器人
$url = "http://i.itpk.cn/api.php?question=$content&limit=10&api_key=47c1ebe1ff89ba88f6ed1cbad0bdc314&api_secret=7kpv8of4zap3";
$contentStr = file_get_contents($url);
$resultStr = sprintf($textTpl, $fromUser, $toUser, $time, "text", $contentStr);
}
} else {
//调用茉莉机器人
$url = "http://i.itpk.cn/api.php?question=$content&limit=10&api_key=47c1ebe1ff89ba88f6ed1cxxxxxdc314&api_secret=7kpv8ofxxxxp3";
$contentStr = file_get_contents($url);
//保存下用户 给与提示
$fp2 = file_get_contents("D:\Wnmp\html\weixin_gongzhonghao\\user.txt");
if (strpos($fp2, $fromUser) !== 0) {
$contentStr .= "\n---加#前缀 能斗图哦~";
}
$resultStr = sprintf($textTpl, $fromUser, $toUser, $time, "text", $contentStr);//封装文字消息格式
}
} else {//其他 接受到图片位置等消息的时候暂不处理
$resultStr = sprintf($textTpl, $fromUser, $toUser, $time, "text", "能发一点我能看懂的东西吗?");
}
//记录日志
$requestInformation = date('Y-m-d h:i:s') . "\n" . $_SERVER['REMOTE_ADDR'] . ', ' . $_SERVER['HTTP_USER_AGENT'] .
', http://' . $_SERVER['HTTP_HOST'] . htmlentities($_SERVER['PHP_SELF']) . '?' . $_SERVER['QUERY_STRING'] . "\n" .
"发送者:" . $fromUser . "\n" .
"时间:" . $createTime . "\n" .
"消息类型:" . $msgType . "\n" .
"收到内容:" . $content . "\n" .
"回复内容--->" . "$contentStr\n" .
"-----------------------------------------------------------------------------------------------------------------------\n";
writeFile("D:\Wnmp\html\weixin_gongzhonghao\a.txt", $requestInformation, 'a');
echo $resultStr;//打印消息 即发送给用户