国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 WeChat interface class

WeChat interface class

Aug 22, 2016 am 10:13 AM


valid();
//開啟自動回復功能
$wechatObj->responseMsg();


//定義類文件
class wechatCallbackapiTest
{
    //實現(xiàn)valid驗證方法:實現(xiàn)對接微信公眾平臺
	public function valid()
    {
        //接受隨機字符串
        $echoStr = $_GET["echostr"];

        //valid signature , option
        //進行用戶數(shù)字簽名驗證
        if($this->checkSignature()){
            //如果成功,則返回接受到的隨機字符串
        	echo $echoStr;
            //退出
        	exit;
        }
    }
    //定義自動回復功能
    public function responseMsg()
    {
		//get post data, May be due to the different environments
        //接受用戶端發(fā)送過來的xml數(shù)據(jù)
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

      	//extract post data
        //判斷xml數(shù)據(jù)是否為空
		if (!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);
                //通過simplexml進行xml解析
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                //接受微信的手機端
                $fromUsername = $postObj->FromUserName;
                //微信公眾平臺
                $toUsername = $postObj->ToUserName;
                //接受用戶發(fā)送的關鍵詞
                $keyword = trim($postObj->Content);
                //1.接受用戶消息類型
                $msgType = $postObj -> MsgType;
                //時間戳
                $time = time();
                //文本發(fā)送模板
                $textTpl = "%s0"; 
                //////////////////////////////////////////////////////////////////////////////////
                //如果用戶發(fā)送的是文本類型文件,執(zhí)行以下
                if($msgType == 'text'){
                    if(!empty( $keyword ))
                    {
                        /*這是一個實例
                            //如果發(fā)送文本信息
                            $msgType = "text";
                            //回復內(nèi)容
                            if($keyword == "李楠"){
                                $contentStr = "叫我干嘛";
                            }else{
                                $contentStr = "叫我干嘛";
                            }
                            //格式化xml模板,參數(shù)與上面的模板是一一對應的.fromUsername和頭Username是相反的,只寫帶%s的
                            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

                            //將xml信息返回給客戶端
                            echo $resultStr;
                        */
                       if($keyword == "?" || $keyword == "?"){
                            $msgType = "text";
                            $contentStr = "1.特種服務號碼\n2.通訊服務號碼";
                            $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
                            echo $resultStr;
                       }elseif($keyword == 1){
                            $msgType = "text";
                            $contentStr = "1.匪警:110\n2.火警:119\n3.急救:120";
                            $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
                            echo $resultStr;
                       }elseif($keyword == 2){
                            $msgType = "text";
                            $contentStr = "1.中國移動:10086\n2.中國聯(lián)通:10010";
                            $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
                            echo $resultStr;
                       }
                    }else{
                        echo "不能不說話";
                    }
                }
                ////////////////////////////////////////////////////////////////////////////////////
                //接受圖片信息
                if($msgType == "image"){
                        //如果發(fā)送文本信息
                        $msgType = "text";
                        //回復內(nèi)容
                        $contentStr = "你發(fā)送的是圖片文件";
                        //格式化字符串
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        //將xml信息返回給客戶端
                        echo $resultStr;
                }
                ////////////////////////////////////////////////////////////////////////////////////
                if($msgType == "voice"){
                        //如果發(fā)送文本信息
                        $msgType = "text";
                        //回復內(nèi)容
                        $contentStr = "你發(fā)送的是語音文件";
                        //格式化字符串
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        //將xml信息返回給客戶端
                        echo $resultStr;
                }
                ////////////////////////////////////////////////////////////////////////////////////
                if($msgType == "video"){
                        //如果發(fā)送文本信息
                        $msgType = "text";
                        //回復內(nèi)容
                        $contentStr = "你發(fā)送的是視頻文件";
                        //格式化字符串
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        //將xml信息返回給客戶端
                        echo $resultStr;
                }
                ////////////////////////////////////////////////////////////////////////////////////
                if($msgType == "shortvideo"){
                        //如果發(fā)送文本信息
                        $msgType = "text";
                        //回復內(nèi)容
                        $contentStr = "你發(fā)送的是小視頻文件";
                        //格式化字符串
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        //將xml信息返回給客戶端
                        echo $resultStr;
                }
                ////////////////////////////////////////////////////////////////////////////////////
                if($msgType == "location"){
                        //如果發(fā)送文本信息
                        $msgType = "text";
                        //回復內(nèi)容
                        $contentStr = "你發(fā)送的是地理位置文件";
                        //格式化字符串
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        //將xml信息返回給客戶端
                        echo $resultStr;
                }
                ////////////////////////////////////////////////////////////////////////////////////
                if($msgType == "link"){
                        //如果發(fā)送文本信息
                        $msgType = "text";
                        //回復內(nèi)容
                        $contentStr = "你發(fā)送的是連接文件";
                        //格式化字符串
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        //將xml信息返回給客戶端
                        echo $resultStr;
                }
                ////////////////////////////////////////////////////////////////////////////////////
                /*
                //判斷用戶發(fā)送關鍵詞是否為空           
				
                if(!empty( $keyword ))
                {
                    //如果發(fā)送文本信息
              		$msgType = "text";
                    //回復內(nèi)容
                	$contentStr = "大家好,我是hero";
                    //格式化字符串
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    //將xml信息返回給客戶端
                	echo $resultStr;
                }else{
                	echo "Input something...";
                }
                */
        }else {
        	echo "";
        	exit;
        }
    }
		
	private function checkSignature()
	{
        // you must define TOKEN by yourself
        //判斷是否定義了TOKEN,如果沒有就拋出一個異常
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        
        $signature = $_GET["signature"];//接受微信加密簽名
        $timestamp = $_GET["timestamp"];//接受時間戳
        $nonce = $_GET["nonce"];//接受隨機數(shù)
        		
		$token = TOKEN;//把TOKEN常量賦值給$token
        //把相關參數(shù)組裝成數(shù)組
		$tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        //排序
		sort($tmpArr, SORT_STRING);
        //把排序后的數(shù)組轉換成字符串
		$tmpStr = implode( $tmpArr );
        //通過哈希算法加密
		$tmpStr = sha1( $tmpStr );
		//與加密簽名進行對比
		if( $tmpStr == $signature ){
            //相同返回true
			return true;
		}else{
            //不同返回false
			return false;
		}
	}
}

?>


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276