/**
* @Author: itwo
* @Date: 2016-07-18 11:04:35
* @Last Modified by: itwo
* @Last Modified time: 2016-07-28 11:55:54
*/
// 接收用户信息
// 微信公众账号接收用户的消息类型判断
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if(isset($_GET["echostr"])){
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
/**
* 微信开发入口
*/
class wechatCallbackapiTest
{
/**
* 验证消息的确来自微信服务器
* @return [bool] [ description ]
* 转载:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319&token=&lang=zh_CN
*/
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 valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; // 理解-> http://www.cnblogs.com/xwblog/archive/2011/12/23/2299672.html
if(!empty($postStr)){
$postStr = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
$RX_TYPE = trim($postStr->MsgType);
//用户发送的消息类型判断
switch ($RX_TYPE) {
case ‘text‘: // 文本消息
$result = $this->receiveText($postStr);
break;
case ‘image‘:
$result = $this->receiveImage($postStr);
break;
case ‘voice‘:
$result = $this->receiveVoice($postStr);
break;
case ‘video‘:
$result = $this->receiveVideo($postStr);
break;
case ‘shortvideo‘:
$result = $this->receiveVideo($postStr);
break;
case ‘location‘:
$result = $this->receiveLocation($postStr);
break;
case ‘link‘:
$result = $this->receiveLink($postStr);
break;
case ‘event‘:
$result = $this->receiveEvent($postStr);
break;
default:
$result = $this->receiveMsgType($postStr);
break;
}
echo $result;
}else{
echo "";
exit;
}
}
/**
* 接收用户发送各种消息类型,提取用户发送的消息特征值,然后反馈给开发者
*/
private function receiveText($object)
{
$content = " 你发送的是文本,内容为: ".$object->Content;
$result = $this->transmitText($object, $content);
$keyword = trim($object->Content);
if ($keyword == "文本") {
$content = " 你发送的是文本,内容为: ".$object->FromUserName;
$result = $this->transmitText($object, $content);
}
else if($keyword == "图文" || $keyword == "单图文"){
//回复单图文消息
$content = array();
$content[] = array("Title"=>"单图文标题", "Description"=>"单图文简述", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url"=>"http://m.cnblogs.com/?u=txw1958");
$result = $this->transmitNews($object, $content);
}
else if($keyword == "多图文"){
//回复单图文消息
$content = array();
$content[] = array("Title"=>"图文标题1", "Description"=>"图文简述1", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url"=>"http://m.cnblogs.com/?u=txw1958");
$content[] = array("Title"=>"图文标题2", "Description"=>"图文简述2", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url"=>"http://m.cnblogs.com/?u=txw1958");
$content[] = array("Title"=>"图文标题3", "Description"=>"图文简述3", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url"=>"http://m.cnblogs.com/?u=txw1958");
$result = $this->transmitNews($object, $content);
}
else if ($keyword == "音乐") {
$content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇","MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3","HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");
$result = $this->transmitMusic($object, $content);
}
return $result;
}
private function receiveImage($object)
{
$content = array("MediaId"=>$object->MediaId);
$result = $this->transmitImage($object, $content);
return $result;
}
private function receiveVoice($object)
{
$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 receiveLocation($object)
{
$content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
$result = $this->transmitText($object, $content);
return $result;
}
private function receiveLink($object)
{
$content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
$result = $this->transmitText($object, $content);
return $result;
}
private function receiveEvent($object)
{
$content = "";
switch ($object->Event) {
case ‘subscribe‘:
$content = "欢迎关注itwo123,科学上网";
break;
case ‘unsubscribe‘:
$content = "";
break;
default:
$content = "";
break;
}
$result = $this->transmitText($object, $content);
return $result;
}
private function receiveMsgType($object)
{
$content = "抱歉,系统故障。"."\n故障代码:".$object->MsgType;
$result = $this->transmitText($object, $content);
return $result;
}
/**
* 文本形式回复用户发送的消息
*/
private function transmitText($object, $content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime><![CDATA[%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><![CDATA[%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><![CDATA[%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><![CDATA[%s]]></CreateTime>
<MsgType><![CDATA[video]]></MsgType>
$item_str
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
/**
* 回复图文信息
*/
private function transmitNews($object, $arr_item)
{
if(!is_array($arr_item)){
return;
}
$itemTpl = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$item_str = "";
foreach ($arr_item 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($arr_item));
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;
}
}
时间: 2024-10-10 00:26:28