为了使获取不到图片丶语音丶视频丶音乐缩略图的meidiaId的订阅号,也能自动回复图片消息,语音消息,视频消息(windows phone手机还不知道怎么发视频)。晚上,就做了这个工具。
将自己当粉丝,然后给自己的订阅号发消息。你需要的自动回复什么图片,你就发送什么图片。你需要什么语音,你就录制什么语音发送。当你发送这些消息时,服务端会在服务器上建立一个txt文档,该文档会将这些消息的mediaId和描述信息记录下来。
当你记录完毕后,打开服务端的txt,你就能得到mediaId了。

所要做的是,在开发模式下,修改URL配置到这个工具的URL。当你获取mediaId完毕时,再将URL切换回来。
也许,有人会觉得这个麻烦。那么你也可以将这个功能直接融入到所正常使用的那个代码中。你通过给订阅号,发送秘密语言来启动记录mediaId和停止mediaId.
不过,我觉得这样并不好。影响原来的代码的美观以及逻辑性。

还有一点要说明,自己的订阅号所得到的mediaId只能自己的订阅号使用,别的订阅号使用不到。至于,这个mediaId的生命周期,还不知道会是多少。
好吧。先贴出录制过程的截图:

记录结果

记录结果

最后,贴出完整的php代码:

<?php
/**
  * author vini123
  * blog http://blog1.vini123.com
  * date 2014.02.13 22:30
  */
define("TOKEN", "vini123");

$wechatObj = new wechatCallbackapiTest();
if (isset($_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;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
   
    private function writeInTxt($str)
    {
        $date = date('Y-m-d');
        $open=fopen("log/".$date."_log.txt","a" );
        fwrite($open,$str);
        fclose($open);
    }
    
    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        $contentStr = "";
        $msgType = "";
        if (!empty($postStr))
        {
               $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();
                $contentStr = "";
                $resultStr = "";
                
                  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $creatTime = $postObj->CreateTime;
                $msgType = $postObj->MsgType;
                $msgId = $postObj->MsgId;
                $mediaId = "";
                $tempStr = "";
                $thumbMediaId = "";
                             
                switch($msgType)
                {
                    case "text":
                        $keyword = trim($postObj->Content);
                        $this -> writeInTxt($keyword."\r\n");
                        break;
                    case "image":
                        $mediaId = $postObj->MediaId;
                        $tempStr = "";
                        $tempStr = "toUser: ".$toUsername."\r\n";
                        $tempStr = $tempStr."fromUser: ".$fromUsername."\r\n";
                        $tempStr = $tempStr."msgType: ".$msgType."\r\n";
                        $tempStr = $tempStr."mediaId: ".$mediaId."\r\n";
                        $tempStr = $tempStr."msgId: ".$msgId."\r\n";
                        $tempStr = $tempStr."______________________END_____________________\r\n\r\n";
                        $this -> writeInTxt($tempStr);
                        
                        $msgType = "text";
                        $contentStr = "保存图片完毕!谢谢!";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        echo $resultStr;
                        break;
                    case "voice":
                        $mediaId = $postObj->MediaId;
                        $tempStr = "";
                        $tempStr = "toUser: ".$toUsername."\r\n";
                        $tempStr = $tempStr."fromUser: ".$fromUsername."\r\n";
                        $tempStr = $tempStr."msgType: ".$msgType."\r\n";
                        $tempStr = $tempStr."mediaId: ".$mediaId."\r\n";
                        $tempStr = $tempStr."msgId: ".$msgId."\r\n";
                        $tempStr = $tempStr."______________________END_____________________\r\n\r\n";
                        $this -> writeInTxt($tempStr);
                        
                        $msgType = "text";
                        $contentStr = "保存声音完毕!谢谢!";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        echo $resultStr;
                        break;                        
                    case "video":
                        $mediaId = $postObj->MediaId;
                        $thumbMediaId = $postObj->ThumbMediaId;
                        $tempStr = "";
                        $tempStr = "toUser: ".$toUsername."\r\n";
                        $tempStr = $tempStr."fromUser: ".$fromUsername."\r\n";
                        $tempStr = $tempStr."msgType: ".$msgType."\r\n";
                        $tempStr = $tempStr."mediaId: ".$mediaId."\r\n";
                        $tempStr = $tempStr."thumbMediaId: ".$thumbMediaId."\r\n";
                        $tempStr = $tempStr."msgId: ".$msgId."\r\n";
                        $tempStr = $tempStr."______________________END_____________________\r\n\r\n";
                        $this -> writeInTxt($tempStr);
                        
                        $msgType = "text";
                        $contentStr = "保存视频完毕!谢谢!";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        echo $resultStr;
                        break;
                    default:
                        break;
                }  
             }
             else
             {
                echo "";
                exit;
             }
        }
}
?>

 

欢迎大家加我的微信 vini1024 , 成为我的粉丝。谢谢。。。