子沫
微信公众号对接网站实现自动回复搜索找电影关注自动回复
04/23
本文最后更新于2021年04月11日,已超过1377天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
<?php
header('Content-type:text');
define("TOKEN", "qingeedy");
$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest{
//验证签名
public function valid(){
$echoStr = $_GET["echostr"];
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
echo $echoStr;
exit;
}
}
//响应消息
public function responseMsg(){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content></xml>";
//$postStr = 'huihui'; //调试用
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$event = $postObj->Event;
$RX_TYPE = trim($postObj->MsgType);
// $RX_TYPE = "text";//gnagcai zhege meiqudiao
//消息类型分离
switch ($RX_TYPE){
case "text":
$result = $this->receiveText($postObj);
break;
case "image":
$result = $this->receiveImage($postObj);
break;
case 'event':
if($event == 'subscribe')
{
$contentStr = "欢迎关注";
$result = $this->receiveText2($postObj);
}
break;
default:
$result = "unknown msg type: ".$RX_TYPE;
break;
}
echo $result;
}else {
echo "this i s failed";
exit;
}
}
//接收文本消息
private function receiveText($object){
$a = ($object->Content);
$keyword = trim($a);//关键字
$arr[] = array(
"Title" =>"为您找到【".$keyword."】的所有搜索结果!",
"Description" => "享受闲暇,不负好时光!点我查看 ☞",
"PicUrl" => "https://app.qingee.net/weixin/img/sousou.png",
"Url" => "http//he-ad.cn/?wd=".$keyword
);
$result="";
if (is_array($arr)) {
if (isset($arr[0])) {
$result = $this->transmitNews($object, $arr);
}
} else {
$result = $this->transmitText($object, $arr);
}
return $result;
}
//接收文本消息
private function receiveText2($object){
$a = ($object->Content);
$keyword = trim($a);//关键字
$arr="欢迎关注我们,橙子网络";
$result="";
if (is_array($arr)) {
if (isset($arr[0])) {
$result = $this->transmitNews($object, $arr);
}
} else {
$result = $this->transmitText($object, $arr);
}
return $result;
}
//接收图片消息
private function receiveImage($object){
$content = array("MediaId"=>$object->MediaId);
$result = $this->transmitImage($object, $content);
return $result;
}
//回复文本消息
private function transmitText($object, $content){
if (!isset($content) || empty($content)){
return "";
}
$xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
//回复图文消息
private function transmitNews($object, $newsArray){
if(!is_array($newsArray)){
return "";
}
$itemTpl = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$item_str = "";
foreach ($newsArray as $item){
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
}
$xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>$item_str</Articles>
</xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
return $result;
}
//回复图片消息
private function transmitImage($object, $imageArray){
$itemTpl = "<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>";
$item_str = sprintf($itemTpl, $imageArray['MediaId']);
$xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
$item_str
</xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
}
?>
版权属于:
Strjson博客-专注于各种精品源码、精品软件、技术教程分享、黑客技术、破解教程(爱你在心口难开、没事写一写)
本文链接:
https://jpgke.com/jishu/112.html(转载时请注明本文出处及文章链接)
作品采用: