您的位置:首页 > 移动开发 > 微信开发

微信接口 图灵自动回复

2016-07-28 10:09 369 查看
define("TOKEN", "*******");/*输入自己的微信token*/

$wechatObj = new wechatCallbackapiTest();

$wechatObj->valid();

class wechatCallbackapiTest

{

    public function valid()

    {

        $echoStr = $_GET["echostr"];

        //valid signature , option

        if($this->checkSignature()){

            echo $echoStr;

            echo $this->responseMsg();

            exit;

        }

    }

    public function responseMsg()

    {

        //get post data, May be due to the different environments

        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

          //extract post data

        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);

                  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

                $fromUsername = $postObj->FromUserName;

                $toUsername = $postObj->ToUserName;

                $keyword = trim($postObj->Content);

                $time = time();

                //显示文本模板

                $textTpl_text= "<xml>

                            <ToUserName><![CDATA[%s]]></ToUserName>

                            <FromUserName><![CDATA[%s]]></FromUserName>

                            <CreateTime>%s</CreateTime>

                            <MsgType><![CDATA[%s]]></MsgType>

                            <Content><![CDATA[%s]]></Content>

                            <FuncFlag>0</FuncFlag>
                            </xml>";      

                //  判断关注与输入

                if(empty( $keyword ))

                {

                    $msgType = "text";

                    $contentStr = "Welcome to wechat world!";

                    $resultStr = sprintf($textTpl_text, $fromUsername, $toUsername, $time, $msgType, $contentStr);

                    echo $resultStr;
                }else{

                    //回复类型

                    $msgType = "text";

                    //调用图灵机器人回复信息

                    $url="http://www.tuling123.com/openapi/api?key=5af470f42ac9f4be4ad43a951fa3a339&info=".$keyword;

                    $text = file_get_contents($url);

                    $arr = json_decode($text,true);

                    $contentStr = $arr['text'];

                    $resultStr = sprintf($textTpl_text, $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 rule

        sort($tmpArr, SORT_STRING);

        $tmpStr = implode( $tmpArr );

        $tmpStr = sha1( $tmpStr );

        

        if( $tmpStr == $signature ){

            return true;

        }else{

            return false;

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: