微信公众平台信息处理(PHP) Posted on 2015-04-03 | In PHP | 后台是 PHP 处理的,主要是获取接收到的信息类型还有组织发出的信息。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287<?phpclass MsgProcess{ private $fromUserName; private $toUserName; private $createTime; private $msgId; // media message private $mediaId; // text message private $content; // image message private $picUrl; // voice message private $format; // video or short video message private $thumbMediaId; // location message private $location_X; private $location_Y; private $scale; private $label; // link message private $title; private $description; private $url; // set variables from php://input public function __construct() { $postStr = file_get_contents("php://input"); if (empty($postStr)) { exit; } $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $this->fromUserName = $postObj->FromUserName; $this->toUserName = $postObj->ToUserName; $this->msgType = $postObj->MsgType; $this->createTime = $postObj->CreateTime; $this->msgId = $postObj->MsgId;// $this->fromUserName = $_POST['FromUserName'];// $this->toUserName = $_POST['ToUserName'];// $this->msgType = $_POST['MsgType'];// $this->createTime = $_POST['CreateTime'];// $this->msgId = $_POST['MsgId']; switch ($this->msgType) { case "text": $this->content = trim($postObj->Content);// $this->content = trim($_POST['Content']); break; case "image": $this->picUrl = $postObj->PicUrl; $this->mediaId = $postObj->MediaId;// $this->picUrl = $_POST['PicUrl'];// $this->mediaId = $_POST['MediaId']; break; case "voice": $this->mediaId = $postObj->MediaId; $this->format = $postObj->Format;// $this->mediaId = $_POST['MediaId'];// $this->format = $_POST['Format']; break; case "video": case "shortvideo": $this->mediaId = $postObj->MediaId; $this->thumbMediaId = $postObj->ThumbMediaId;// $this->mediaId = $_POST['MediaId'];// $this->thumbMediaId = $_POST['ThumbMediaId']; break; case "link": $this->title = $postObj->Title; $this->description = $postObj->Description; $this->url = $postObj->Url;// $this->title = $_POST['Title'];// $this->description = $_POST['Description'];// $this->url = $_POST['Url']; break; case "location": $this->location_X = $postObj->Location_X; $this->location_Y = $postObj->Location_Y; $this->scale = $postObj->Scale; $this->label = $postObj->Label; break; } } // get message type public function getMsgType() { return $this->msgType; } // get message public function getMsg() { switch ($this->msgType) { case "text": return $this->content; case "image": return array( 'PicUrl' => $this->picUrl, 'MediaId' => $this->mediaId, ); case "voice": return array( 'MediaId' => $this->mediaId, 'Format' => $this->format, ); case "video": case "shortvideo": return array( 'MediaId' => $this->mediaId, 'ThumbMediaId' => $this->thumbMediaId, ); case 'location': return array( 'Location_X' => $this->location_X, 'Location_Y' => $this->location_Y, 'Scale' => $this->scale, 'Label' => $this->label, ); case "link": return array( 'Title' => $this->title, 'Description' => $this->description, 'Url' => $this->url, ); } } // response in text public function resText($contentStr) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $time = time(); $msgType = "text"; $res = sprintf($textTpl, $this->fromUserName, $this->toUserName, $time, $msgType, $contentStr); echo $res; } // response in image public function resImage($mediaId) { $imageTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Image> <MediaId><![CDATA[%s]]></MediaId> </Image> </xml>"; $time = time(); $msgType = "image"; $res = sprintf($imageTpl, $this->fromUserName, $this->toUserName, $time, $msgType, $mediaId); echo $res; } public function resVoice($mediaId) { $voiceTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Voice> <MediaId><![CDATA[%s]]></MediaId> </Voice> </xml>"; $time = time(); $msgType = "voice"; $res = sprintf($voiceTpl, $this->fromUserName, $this->toUserName, $time, $msgType, $mediaId); echo $res; } public function resVideo($mediaId, $title, $description) { $videoTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Video> <MediaId><![CDATA[%s]]></MediaId> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> </Video> </xml>"; $time = time(); $msgType = "video"; $res = sprintf($videoTpl, $this->fromUserName, $this->toUserName, $time, $msgType, $mediaId, $title, $description); echo $res; } public function resMusic($title, $description, $musicUrl, $HQMusicUrl, $thumbMediaId) { $musictpl = "<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> <ThumbMediaId><![CDATA[%s]]></ThumbMediaId> </Music> </xml>"; $time = time(); $msgType = "music"; $res = sprintf($musictpl, $this->fromUserName, $this->toUserName, $time, $msgType, $title, $description, $musicUrl, $HQMusicUrl, $thumbMediaId); echo $res; } /* * $newsArr = array( array( 'Title' => '', 'Description' => '', 'PicUrl' => '', 'Url' => '', ), array( 'Title' => '', 'Description' => '', 'PicUrl' => '', 'Url' => '', ), ); */ public function resArticle($newsArr) { if (sizeof($newsArr) > 10) { echo "must be less than 10 articles!"; exit; } $res = ""; $newsCount = 0; $resHeadTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles>"; $resItemTpl = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; $resFooterTpl = "</Articles> </xml>"; $res = $res . $resHeadTpl; foreach ($newsArr as $item) { $resItem = sprintf($resItemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); $res = $res . $resItem; $newsCount++; } $res = $res . $resFooterTpl; $time = time(); $msgType = 'news'; $res = sprintf($res, $this->fromUserName, $this->toUserName, $time, $msgType, $newsCount); echo $res; }}