這是示例主要用來獲取公衆號和用戶進行視頻信息傳遞時的解析方法php
<?php define("ToKEN", "weixin"); //定義一個常量 $wechatObj = new wechatCallbackapiTest(); //標準模版 if (isset($_GET['echostr'])) { //echo $_GET['echostr']; $wechatObj->valid(); } else { $wechatObj->responseMsg(); } class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if ($this->checkSignature()) { echo $echoStr; exit; //輸出一個消息而且退出當前腳本 } } //驗證微信簽名 private function checkSignature() { $signature = $_GET["signature"]; //微信加密簽名 $timestamp = $_GET["timestamp"]; //時間戳 $nonce = $_GET["nonce"]; //隨機數 $token = TOKEN; //微信token $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); //對數組進行排序 $tmpStr = implode($tmpArr); //將一個一維數組的值轉化爲字符串 $tmpStr = sha1($tmpStr); //計算字符串的 sha1 散列值 if ($tmpStr == $signature) { return true; } else { return false; } } //發送信息 public function responseMsg() { /** * 基本上$GLOBALS['HTTP_RAW_POST_DATA'] 和 $_POST是同樣的。可是若是post過來的數據不是PHP可以識別的, * 你能夠用 $GLOBALS['HTTP_RAW_POST_DATA']來接收,好比 text/xml 或者 soap 等等 */ $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)) { //檢查一個變量是否爲空 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; //發送方微信號 $toUsername = $postObj->ToUserName; // 開發者微信公衆賬號 $CreateTime = intval($postObj->CreateTime); //消息的建立時間,而且把這個時間轉換成整數。 $formTime = date("Y-m-d H:i:s", $CreateTime); $MsgId = $postObj->MsgId; //消息內容的隨機ID $MsgType = $postObj->MsgType; //消息類型 $MediaId = $postObj->MediaId; //視頻消息媒體ID $ThumbMediaId = $postObj->ThumbMediaId; //視頻信息縮略圖的媒體ID。 //返回給微信服務器的模版 $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>"; $time = time(); $msg = "開發者id: " . $toUsername . "\n"; $msg .= "用戶id: " . $fromUsername . "\n"; $msg .= "視頻消息id: " . $MsgId . "\n"; $msg .= "視頻消息類型: " . $MsgType . "\n"; $msg .= "視頻消息媒體id :" . $MediaId . "\n"; $msg .= "視頻消息縮略圖媒體id :" . $ThumbMediaId . "\n"; $msg .= "視頻信息發送過來的時間戳: " . $CreateTime . "\n"; $contentStr = $msg; $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; exit; } else { exit; } } } ?>