define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
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)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; //发送消息放ID
$toUsername = $postObj->ToUserName; //接受信息放ID
$form_MsgType = $postObj->MsgType; //消息类型
$form_Content = $postObj->Content; //消息内容
$keyword = trim($postObj->Content); //关键字
$form_Event = $postObj->Event; //获取事件类型
$form_Key = $postObj->EventKey; //获取菜单Key值
$j=$postObj->Location_X;
$w=$postObj->Location_Y;
$time = time(); //时间
$form_CreateTime = $postObj->CreateTime; //发送时间
$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>";
$newsTpl = " <xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
<FuncFlag>1</FuncFlag>
</xml>";
if($form_MsgType=="text"){
if($keyword == "今日活动")
{
$contentStr = file_get_contents('http://paiangotcwap.e-dar.cn/wx_content.php?cx=1');
$contentStr = json_decode($contentStr,true);
$count = count($contentStr);
$newsTplHead = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>$count</ArticleCount>
<Articles>";
$header = sprintf($newsTplHead, $fromUsername, $toUsername, time());
$newsTplBody = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
foreach($contentStr as $newsContent){
$body .= sprintf($newsTplBody, $newsContent['title'], $newsContent['Description'], $newsContent['PicUrl'], $newsContent['url']);
}
$newsTplFoot = "</Articles>
<FuncFlag>0</FuncFlag>
</xml>";
$FuncFlag = 0;
$footer = sprintf($newsTplFoot, $FuncFlag);
echo $header.$body.$footer; exit;
}
}
if($form_MsgType =='link'){
$msgType = "news";
$contentStr = file_get_contents($Url);
$contentStr = json_decode($contentStr,true);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
if($form_MsgType=="event") //判断消息内容
{
if($form_Event=="subscribe") //判断事件类型
{
//回复欢迎文字消息
$msgType = "text";
$contentStr = "欢迎关注陕药派昂OTC大家庭";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
if($form_Event == "CLICK"){
if($form_Key =="今日活动")
{
$contentStr = file_get_contents('http://paiangotcwap.e-dar.cn/wx_content.php?cx=1');
$contentStr = json_decode($contentStr,true);
$count = count($contentStr);
$newsTplHead = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>$count</ArticleCount>
<Articles>";
$header = sprintf($newsTplHead, $fromUsername, $toUsername, time());
$newsTplBody = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
foreach($contentStr as $newsContent){
$body .= sprintf($newsTplBody, $newsContent['title'], $newsContent['Description'], $newsContent['PicUrl'], $newsContent['url']);
}
$newsTplFoot = "</Articles>
<FuncFlag>0</FuncFlag>
</xml>";
$FuncFlag = 0;
$footer = sprintf($newsTplFoot, $FuncFlag);
echo $header.$body.$footer; exit;
}
}
QQ: 123581702