php微信開發01

來源:互聯網
上載者:User
valid();$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 dataif (!empty($postStr)){                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,                   the best way is to check the validity of xml by yourself */                libxml_disable_entity_loader(true);              $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);                $fromUsername = $postObj->FromUserName;                $toUsername = $postObj->ToUserName;                $keyword = trim($postObj->Content);                $time = time();                         $type=$postObj->MsgType;$customevent=$postObj->Event;                            $latitude=$postObj->Location_X;    $l                            $textTpl = "%s%s%s%s%s0";             //發送位置回複            if($type=="location"){                $c                $msgType = "text";                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                echo $resultStr;                }                                                                //關鍵字回複            if(!empty( $keyword ))                {              $msgType = "text";                    if($keyword=="1") {                                               $contentStr = "感謝關注泛IT!";}                    if($keyword=="2") {                        $contentStr = "聯絡QQ:,歡迎投稿以及提出寶貴意見";}                    if($keyword=="3") {                                               $contentStr = "你的圖片不錯,還是輸入文字吧!";}                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                echo $resultStr;                }else{                echo "Input something...";                }                                     //關注公眾號後回複               if($type="event" and $customevent="subscribe"){                $c                $msgType = "text";                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                echo $resultStr;                }                                      //圖片回複                if($type="image"){                $c                $msgType = "text";                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                echo $resultStr;                }  
//switch語句最佳化
 /*            switch($type){                            //處理文本                        case "text":                        //關鍵字回複                        if(!empty( $keyword ))                        {                             //$msgType = "text";                            if($keyword=="1") {                                                   $contentStr = "回複1查看使用說明\n回複2查看連絡方式\n回複3使用翻譯字典\n回複4使用機器人聊天功能";}                            if($keyword=="2") {                            $contentStr = "聯絡QQ:,歡迎投稿以及提出寶貴意見";}                         }else{                $c }                        break;                         //處理圖片資訊                         case "image":$c                        break;                        //處理位置資訊                         case "location":$cbreak;                        //處理關注事件                         case "event":if($customevent=="subscribe"){$cbreak;                            default:$c                                           }            $msgType = "text";                        $resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);echo $resultStr;                                              */

}else { echo ""; exit; }
 }
 private function checkSignature()
{ // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN;$tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rulesort($tmpArr, SORT_STRING);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;}else{return false;}}}?>

以上就介紹了php開發01,包括了方面的內容,希望對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.