<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "wssss");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr))
{
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$MsgType = $postObj->MsgType;
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
switch ($MsgType)
{
case ‘event‘:
$event = $postObj -> Event;
if ($event == "subscribe")
{
$contentStr = "哈喽啊~欢迎关注!";
}
# code...
break;
case ‘text‘:
$wenzi = $postObj->Content;
switch ($wenzi) {
case ‘你好‘:
case ‘好‘:
case ‘hello‘:
case ‘HELLO‘:
case ‘你好么‘:
$contentStr= "比你好一点!";
break;
case ‘我‘:
$contentStr="我比你帅一点!";
break;
case ‘王帅‘:
$contentStr="比张琦帅一点!";
break;
case ‘小鸡‘:
$contentStr="是张琦咯咯哒么~";
break;
case ‘累么‘:
$contentStr="莫欺少年穷";
break;
default:
$contentStr="你是猪,我还不知道呢!";
# code...
break;
}
}
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else
{
echo "";
exit;
}
}
}
?>