现在的位置: 首页 > 综合 > 正文

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

2012年12月01日 ⁄ 综合 ⁄ 共 3205字 ⁄ 字号 评论关闭
<?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;
		}
	}
}
?>

抱歉!评论已关闭.