php測試代碼

來源:互聯網
上載者:User

標籤:libxml   media   efi   機器人   post   ret   display   switch   修改   

<?php/*    這是一個自動回複的PHP*/header(‘Content-type:text‘);define("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();if (!isset($_GET[‘echostr‘])) {    $wechatObj->responseMsg();}else{    $wechatObj->valid();}class wechatCallbackapiTest{    //驗證簽名    public function valid()    {        $echoStr = $_GET["echostr"];        $signature = $_GET["signature"];        $timestamp = $_GET["timestamp"];        $nonce = $_GET["nonce"];        $token = TOKEN;        $tmpArr = array($token, $timestamp, $nonce);        sort($tmpArr, SORT_STRING);        $tmpStr = implode($tmpArr);        $tmpStr = sha1($tmpStr);        if($tmpStr == $signature){            echo $echoStr;            exit;        }    }    //響應訊息    public function responseMsg()    {        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];        if (!empty($postStr)){            $this->logger("R \r\n".$postStr);            $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);            $RX_TYPE = trim($postObj->MsgType);                        //訊息類型分離            switch ($RX_TYPE)            {                case "event":                    $result = $this->receiveEvent($postObj);                    break;                case "text":                    $result = $this->receiveText($postObj);                    break;                case "image":                    $result = $this->receiveImage($postObj);                    break;                case "location":                    $result = $this->receiveLocation($postObj);                    break;                case "voice":                    $result = $this->receiveVoice($postObj);                    break;                case "video":                case "shortvideo":                    $result = $this->receiveVideo($postObj);                    break;                case "link":                    $result = $this->receiveLink($postObj);                    break;                default:                    $result = "unknown msg type: ".$RX_TYPE;                    break;            }            $this->logger("T \r\n".$result);            echo $result;        }else {            echo "";            exit;        }    }    //接收事件訊息    private function receiveEvent($object)    {        $content = "";        switch ($object->Event)        {            case "subscribe":                $content = "歡迎使用互連網CAE自動資料機器人\n請回複以下關鍵字:\n文本 表情 單圖文 多圖文 音樂\n請按住說話 或 \n點擊 + 再分別發送以下內容:語音 圖片 小視頻 我的收藏 位置";                //                //$content .= (!empty($object->EventKey))?("\n來自二維碼情境 ".str_replace("qrscene_","",$object->EventKey)):"";                //後面的連結會變成可點擊的連結                $content .= "\n\n".‘<a href="http://www.cnblogs.com/ironwang/p/6376873.html">互連網CAE 伊羅賽創工作室</a>‘;                break;            default:                $content = "receive a new event: ".$object->Event;                break;        }        if(is_array($content)){            $result = $this->transmitNews($object, $content);        }else{            $result = $this->transmitText($object, $content);        }        return $result;    }    //接收簡訊    private function receiveText($object)    {        $keyword = trim($object->Content);        //自動回複模式        if (strstr($keyword, "資料")){            $content = ‘點擊一下資料庫‘."\n".‘<a href="http://www.cnblogs.com/Elosithang/p/6401061.html">互連網CAE 分享資料</a>‘;                    }else if (strstr($keyword, "表情")){                        $content = "微笑:/::)\n乒乓:/:oo\n中國:".$this->bytes_to_emoji(0x1F1E8).$this->bytes_to_emoji(0x1F1F3)."\n仙人掌:".$this->bytes_to_emoji(0x1F335);            //這裡開始添加流體相關的關鍵字            }else if (strstr($keyword, "cfd")){            $content = ‘點擊一下資料庫‘."\n".‘<a href="http://www.cnblogs.com/Elosithang/p/6401061.html">互連網CAE-CFD 分享資料</a>‘;            }else if (strstr($keyword, "流體")){            $content = ‘點擊一下資料庫‘."\n".‘<a href="http://www.cnblogs.com/Elosithang/p/6401061.html">互連網CAE-NVH 分享資料</a>‘;            //外流場            //整車            //機艙            //HVAC            //空調舒適性            //電池包            //涉水            //指令碼            //最新軟體            //            }else if (strstr($keyword, "Star")){            $content = ‘點擊一下資料庫‘."\n".‘<a href="http://www.cnblogs.com/Elosithang/p/6401061.html">互連網CAE-Starccm+ 分享資料</a>‘;            }else if (strstr($keyword, "star")){            $content = ‘點擊一下資料庫‘."\n".‘<a href="http://www.cnblogs.com/Elosithang/p/6401061.html">互連網CAE-Starccm+ 分享資料</a>‘;            }else if (strstr($keyword, "STAR")){            $content = ‘點擊一下資料庫‘."\n".‘<a href="http://www.cnblogs.com/Elosithang/p/6401061.html">互連網CAE-Starccm+ 分享資料</a>‘;                        //流體添加到這裡為止                    }else if (strstr($keyword, "單圖文")){            $content = array();            $content[] = array("Title"=>"單圖文標題",  "Description"=>"單圖文內容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");                    }else if (strstr($keyword, "圖文") || strstr($keyword, "多圖文")){            $content = array();            $content[] = array("Title"=>"多圖文1標題", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");            $content[] = array("Title"=>"多圖文2標題", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");            $content[] = array("Title"=>"多圖文3標題", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");                    }else if (strstr($keyword, "音樂")){            $content = array();            $content = array("Title"=>"最炫民族風", "Description"=>"歌手:鳳凰傳奇", "MusicUrl"=>"http://mascot-music.stor.sinaapp.com/zxmzf.mp3", "HQMusicUrl"=>"http://mascot-music.stor.sinaapp.com/zxmzf.mp3");                     }else{//這裡修改其他內容                        $content = ‘為您提供時間:‘.$this->bytes_to_emoji(0x1F1E8)."\n".date("Y-m-d H:i:s",time())."\n".‘點擊一下資料庫‘."\n".‘<a href="http://www.cnblogs.com/Elosithang/p/6401061.html">互連網CAE 分享資料</a>‘;                    }        if(is_array($content)){            if (isset($content[0])){                $result = $this->transmitNews($object, $content);            }else if (isset($content[‘MusicUrl‘])){                $result = $this->transmitMusic($object, $content);            }        }else{            $result = $this->transmitText($object, $content);        }        return $result;    }    //接收圖片訊息    private function receiveImage($object)    {        $content = array("MediaId"=>$object->MediaId);        $result = $this->transmitImage($object, $content);        return $result;    }    //接收位置訊息    private function receiveLocation($object)    {        $content = "你發送的是位置,經度為:".$object->Location_Y.";緯度為:".$object->Location_X.";縮放層級為:".$object->Scale.";位置為:".$object->Label;        $result = $this->transmitText($object, $content);        return $result;    }    //接收語音訊息    private function receiveVoice($object)    {        if (isset($object->Recognition) && !empty($object->Recognition)){            $content = "你剛才說的是:".$object->Recognition;            $result = $this->transmitText($object, $content);        }else{            $content = array("MediaId"=>$object->MediaId);            $result = $this->transmitVoice($object, $content);        }        return $result;    }    //接收影像訊息    private function receiveVideo($object)    {        $content = "上傳視頻類型:".$object->MsgType;        $result = $this->transmitText($object, $content);        return $result;    }    //接收連結訊息    private function receiveLink($object)    {        $content = "你發送的是連結,標題為:".$object->Title.";內容為:".$object->Description.";連結地址為:".$object->Url;        $result = $this->transmitText($object, $content);        return $result;    }    //回複簡訊    private function transmitText($object, $content)    {        if (!isset($content) || empty($content)){            return "";        }        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[text]]></MsgType>    <Content><![CDATA[%s]]></Content></xml>";        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);        return $result;    }    //回複圖文訊息    private function transmitNews($object, $newsArray)    {        if(!is_array($newsArray)){            return "";        }        $itemTpl = "        <item>            <Title><![CDATA[%s]]></Title>            <Description><![CDATA[%s]]></Description>            <PicUrl><![CDATA[%s]]></PicUrl>            <Url><![CDATA[%s]]></Url>        </item>";        $item_str = "";        foreach ($newsArray as $item){            $item_str .= sprintf($itemTpl, $item[‘Title‘], $item[‘Description‘], $item[‘PicUrl‘], $item[‘Url‘]);        }        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[news]]></MsgType>    <ArticleCount>%s</ArticleCount>    <Articles>$item_str    </Articles></xml>";        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));        return $result;    }    //回複音樂訊息    private function transmitMusic($object, $musicArray)    {        if(!is_array($musicArray)){            return "";        }        $itemTpl = "<Music>        <Title><![CDATA[%s]]></Title>        <Description><![CDATA[%s]]></Description>        <MusicUrl><![CDATA[%s]]></MusicUrl>        <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>    </Music>";        $item_str = sprintf($itemTpl, $musicArray[‘Title‘], $musicArray[‘Description‘], $musicArray[‘MusicUrl‘], $musicArray[‘HQMusicUrl‘]);        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[music]]></MsgType>    $item_str</xml>";        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());        return $result;    }    //回複圖片訊息    private function transmitImage($object, $imageArray)    {        $itemTpl = "<Image>        <MediaId><![CDATA[%s]]></MediaId>    </Image>";        $item_str = sprintf($itemTpl, $imageArray[‘MediaId‘]);        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[image]]></MsgType>    $item_str</xml>";        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());        return $result;    }    //回複語音訊息    private function transmitVoice($object, $voiceArray)    {        $itemTpl = "<Voice>        <MediaId><![CDATA[%s]]></MediaId>    </Voice>";        $item_str = sprintf($itemTpl, $voiceArray[‘MediaId‘]);        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[voice]]></MsgType>    $item_str</xml>";        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());        return $result;    }    //回複影像訊息    private function transmitVideo($object, $videoArray)    {        $itemTpl = "<Video>        <MediaId><![CDATA[%s]]></MediaId>        <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>        <Title><![CDATA[%s]]></Title>        <Description><![CDATA[%s]]></Description>    </Video>";        $item_str = sprintf($itemTpl, $videoArray[‘MediaId‘], $videoArray[‘ThumbMediaId‘], $videoArray[‘Title‘], $videoArray[‘Description‘]);        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[video]]></MsgType>    $item_str</xml>";        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());        return $result;    }    //回複第三方介面訊息    private function relayPart3($url, $rawData)    {        $headers = array("Content-Type: text/xml; charset=utf-8");        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);        $output = curl_exec($ch);        curl_close($ch);        return $output;    }    //位元組轉Emoji表情    function bytes_to_emoji($cp)    {        if ($cp > 0x10000){       # 4 bytes            return chr(0xF0 | (($cp & 0x1C0000) >> 18)).chr(0x80 | (($cp & 0x3F000) >> 12)).chr(0x80 | (($cp & 0xFC0) >> 6)).chr(0x80 | ($cp & 0x3F));        }else if ($cp > 0x800){   # 3 bytes            return chr(0xE0 | (($cp & 0xF000) >> 12)).chr(0x80 | (($cp & 0xFC0) >> 6)).chr(0x80 | ($cp & 0x3F));        }else if ($cp > 0x80){    # 2 bytes            return chr(0xC0 | (($cp & 0x7C0) >> 6)).chr(0x80 | ($cp & 0x3F));        }else{                    # 1 byte            return chr($cp);        }    }    //日誌記錄    private function logger($log_content)    {        if(isset($_SERVER[‘HTTP_APPNAME‘])){   //SAE            sae_set_display_errors(false);            sae_debug($log_content);            sae_set_display_errors(true);        }else if($_SERVER[‘REMOTE_ADDR‘] != "127.0.0.1"){ //LOCAL            $max_size = 1000000;            $log_filename = "log.xml";            if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}            file_put_contents($log_filename, date(‘Y-m-d H:i:s‘)." ".$log_content."\r\n", FILE_APPEND);        }    }}?>

  

php測試代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.