您的位置:首页 > 移动开发 > 微信开发

微信公众平台 开放平台 自定义回复和事件推送代码

2013-06-14 16:20 513 查看
<?php

/**
* wechat php test
*/

//define your token
define("TOKEN", "xxxxx");
$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;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$Event = trim($postObj->Event);
$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>";
//测试事件推送
if($Event=="subscribe"){
$contentStr = "欢迎订阅果晶晶网络科技有限公司公众平台!";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
//test
if($keyword==1){//文本
$contentStr = $keyword . "测试发送文本!";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else if($keyword==2){//音乐
$textTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>
<FuncFlag>0</FuncFlag>
</xml>";

$msgType = "music";
$msgtitle="致青春";
$Description="电影《致我们终将逝去的青春》 主题曲";
$MusicUrl="http://music.baidu.com/song/40153340#";
$HQMusicUrl="http://music.baidu.com/song/40153340#";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $msgtitle,$Description,$MusicUrl,$HQMusicUrl);
echo $resultStr;
}else if($keyword==3){//图文
$textTpl="<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> ";

$msgType = "news";
$title1="致青春";
$Description="电影《致我们终将逝去的青春》 主题曲";
$PicUrl="http://img10.360buyimg.com/da/g13/M05/01/02/rBEhU1G6iA8IAAAAAADmPyFSPhUAAANiwAxLNcAAOZX575.jpg";
$Url="http://sale.jd.com/act/qYHkylDS0LVOg.html?sid=2&cid=601&aid=3678&bid=661&unit=36617&advid=60724&guv=";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $title1,$Description,$PicUrl,$Url);
echo $resultStr;
}
//
if (!empty ($keyword)) {

$contentStr = $keyword . "Welcome to wechat world!";

$msgType = "text";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
} else {
echo "Input something...";
}

} else {
echo "";
exit;
}
}

private function checkSignature() {
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

$token = TOKEN;
$tmpArr = array (
$token,
$timestamp,
$nonce
);
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);

if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐