杜旭 翰云 13:39:46
这是认证和自动回复的
MZ2 13:39:53
哦……
main在哪
server启动在哪
杜旭 翰云 13:40:59
这是应答微信服务器发送过来的请求的
你没法单独使用
MZ2 13:41:25
它应该是一直运行着的吧
杜旭 翰云 13:42:07
这个文件是放在服务器指定目录下的
MZ2 13:42:18
哦。。
有别的地方调它?
杜旭 翰云 13:42:42
在微信服务器中指定了这个目录
MZ2 13:42:52
哦。。。
<?php
define("TOKEN", "Cloudwave123!");
$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest
{
//验证消息
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
//检查签名
private function checkSignature()
{
$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){
return true;
}else{
return false;
}
}
//响应消息
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$this->logger("R ".$postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
case "text":
$result = $this->receiveText($postObj);
break;
case "image":
$result = $this->receiveImage($postObj);
break;
case "location":
$result = $this->receiveLocation($postObj);
break;
case "voice":
$result = $this->receiveVoice($postObj);
break;
case "video":
$result = $this->receiveVideo($postObj);
break;
case "link":
$result = $this->receiveLink($postObj);
break;
default:
$result = "unknown msg type: ".$RX_TYPE;
break;
}
$this->logger("T ".$result);
echo $result;
}else {
echo "";
exit;
}
}
//接收事件消息
private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe":
$content = "恭喜您关注翰云大数据,美图、最新报道、搞笑的胡说大数据上线了,让您的生活无限乐趣!感谢您的关注,您的关注是我们不竭的动力。";
$content .= (!empty($object->EventKey))?("\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):"";
break;
case "unsubscribe":
$content = "取消关注";
break;
case "SCAN":
$content = "扫描场景 ".$object->EventKey;
break;
case "CLICK":
switch ($object->EventKey)
{
case "V1001_aboutus":
$content = array();
$content[] = array("Title"=>"翰云", "Description"=>"翰云数据库", "PicUrl"=>"http://www.cloudwave.cn/wx/images/1.jpg", "Url" =>"http://www.cloudwave.cn");
break;
default:
$content = "点击菜单:".$object->EventKey;
break;
}
break;
case "LOCATION":
$content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;
break;
case "VIEW":
$content = "跳转链接 ".$object->EventKey;
break;
default:
$content = "receive a new event: ".$object->Event;
break;
}
if(is_array($content)){
if (isset($content[0]['PicUrl'])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($object, $content);
}
}else{
$result = $this->transmitText($object, $content);
}
return $result;
}
//接收文本消息
private function receiveText($object)
{
switch ($object->Content)
{
case "新闻":
$content = array();
$content[] = array("Title"=>"新闻", "Description"=>"行业新闻资讯", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn/xwzxlb.jsp");
break;
case "访谈":
$content = array();
$content[] = array("Title"=>"访谈", "Description"=>"访谈", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn/zxly.jsp");
break;
case "知识":
$content = array();
$content[] = array("Title"=>"知识", "Description"=>"知识库", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn/aboutus.jsp");
break;
case "案例":
case "方案":
$content = array();
$content[] = array("Title"=>"案例", "Description"=>"典型案例", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn/dxal.jsp");
break;
case "技术":
case "服务":
case "售前":
case "售后":
$content = array();
$content[] = array("Title"=>"技术服务", "Description"=>"技术服务", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn/product.jsp");
break;
case "大数据":
$content = array();
$content[] = array("Title"=>"大数据", "Description"=>"胡说大数据", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn");
break;
case "数据库":
$content = array();
$content[] = array("Title"=>"数据库", "Description"=>"翰云数据库", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn/product.jsp");
break;
case "留言":
$content = array();
$content[] = array("Title"=>"留言", "Description"=>"联系我们", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn/zxly.jsp");
break;
case "文化":
$content = array();
$content[] = array("Title"=>"文化", "Description"=>"企业文化", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn/ygfc.jsp");
break;
case "活动":
case "中奖":
$content = array();
$content[] = array("Title"=>"活动", "Description"=>"最新活动", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn");
break;
case "你好":
$content = "时间都去哪儿,都去大数据!";
break;
case "图文":
case "单图文":
$content = array();
$content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "PicUrl"=>"http://www.cloudwave.cn/images/banner.jpg", "Url" =>"http://www.cloudwave.cn");
break;
case "多图文":
$content = array();
$content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://www.cloudwave.cn/wx/images/1.jpg", "Url" =>"http://www.cloudwave.cn");
$content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://www.cloudwave.cn");
$content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://www.cloudwave.cn");
break;
// case "音乐":
// $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");
// break;
default:
$content = "没找到二师兄,请大师兄在”大数据“”新闻“”知识“”文化“”案例“”技术“”活动“”留言“房间休息片刻,一会就回来。";
break;
}
if(is_array($content)){
if (isset($content[0]['PicUrl'])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($object, $content);
}
}else{
$result = $this->transmitText($object, $content);
}
return $result;
}
//接收图片消息
private function receiveImage($object)
{
$content = array("MediaId"=>$object->MediaId);
$result = $this->transmitImage($object, $content);
return $result;
}
//接收位置消息
private function receiveLocation($object)
{
$content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
$result = $this->transmitText($object, $content);
return $result;
}
//接收语音消息
private function receiveVoice($object)
{
if (isset($object->Recognition) && !empty($object->Recognition)){
$content = "你刚才说的是:".$object->Recognition;
$result = $this->transmitText($object, $content);
}else{
$content = array("MediaId"=>$object->MediaId);
$result = $this->transmitVoice($object, $content);
}
return $result;
}
//接收视频消息
private function receiveVideo($object)
{
$content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>"");
$result = $this->transmitVideo($object, $content);
return $result;
}
//接收链接消息
private function receiveLink($object)
{
$content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
$result = $this->transmitText($object, $content);
return $result;
}
//回复文本消息
private function transmitText($object, $content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
//回复图片消息
private function transmitImage($object, $imageArray)
{
$itemTpl = "<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>";
$item_str = sprintf($itemTpl, $imageArray['MediaId']);
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
$item_str
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
//回复语音消息
private function transmitVoice($object, $voiceArray)
{
$itemTpl = "<Voice>
<MediaId><![CDATA[%s]]></MediaId>
</Voice>";
$item_str = sprintf($itemTpl, $voiceArray['MediaId']);
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
$item_str
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
//回复视频消息
private function transmitVideo($object, $videoArray)
{
$itemTpl = "<Video>
<MediaId><![CDATA[%s]]></MediaId>
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
</Video>";
$item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']);
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
$item_str
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
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']);
}
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";
$result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
return $result;
}
//回复音乐消息
private function transmitMusic($object, $musicArray)
{
$itemTpl = "<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>";
$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
$item_str
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
//日志记录
private function logger($log_content)
{
if(isset($_SERVER['HTTP_APPNAME'])){ //SAE
sae_set_display_errors(false);
sae_debug($log_content);
sae_set_display_errors(true);
}else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
$max_size = 10000;
$log_filename = "log.xml";
if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);
}
}
}
?>
微信开发示例参考
原创
©著作权归作者所有:来自51CTO博客作者TechOnly的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
微信现金红包开发
微信现金红包
API xml 数据 -
微信小程序-向商户付款界面示例
微信小程序-输入金额向商家付款界面,带金额校验,应用于商家收款码收款场景
小程序 支付 向商家付款 -
微信小程序开发流程及示例Demo
微信小程序开发流程及示例Demo ,了解开发流程,快速入门
小程序 小程序开发 微信小程序 -
微信小程序组件化开发教程、微信小程序组件化开发示例代码
微信小程序中的组件化开发可以使用 template 和 Component 两种方式来实现
微信小程序 组件化 xml html -
微信公众号开发参考
签名验证通过,再加载菜单 //加载菜单 XMLFile.xml 参考:https://www.cnblogs.com/JeffController/p/5875563.html
微信 xml json 加载 javascript