Web_PHP_微信_接口验证、简单文本响应;

来源:互联网 发布:win10安装java没反应 编辑:程序博客网 时间:2024/06/07 03:58

Web_PHP_微信_接口验证、简单文本响应;


<?php/**  * wechat php test  * 微信客户端 <-> 微信服务器 <-> 开发服务器;  *//// define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$wechatObj->valid();/** * 微信回调接口测试 * @author 2WR3_43 */class wechatCallbackapiTest{    /**     * 服务器地址验证:若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。     * @author 2WR3_43     */public function valid()    {        // 子串返回        $echoStr = $_GET["echostr"];        /// valid: valid signature;        if($this->checkSignature()){        echo $echoStr;        exit;        }    }    /**     * 文本信息响应     * @author 2WR3_43     */    public function responseMsg()    {// tips: 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);                                /// SimpleXML处理:SimpleXML 函数允许您把 XML 转换为对象。通过普通的属性选择器或数组迭代器,可以处理这个对象,就像处理任何其他对象一样。                // 获取对象:从 XML 字符串获取 SimpleXMLElement 对象;              $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);                $fromUsername = $postObj->FromUserName;                $toUsername = $postObj->ToUserName;                $keyword = trim($postObj->Content);                $time = time();                $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>";             if(!empty( $keyword ))                {              $msgType = "text";                $contentStr = "Welcome to wechat world!";                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                echo $resultStr;                }else{                echo "Input something;";                }        }else {        echo "";        exit;        }    }    /**     * 签名验证     * 详情参考:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319&token=&lang=zh_CN ;     * 摘要说明:开发者通过检验signature对请求进行校验(开发者校验,下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。     * @author 2WR3_43     */private function checkSignature(){        /// TOKEN:you must define TOKEN by yourself;        if (!defined("TOKEN")) {            throw new Exception('Exception: TOKEN is not defined;');        }                /// 请求参数        // signature参数:微信加密签名,signature结合开发者填写的token参数和请求中的timestamp参数、nonce参数。        $signature = $_GET["signature"];        // timestamp参数:时间戳;        $timestamp = $_GET["timestamp"];        // nonce参数:随机数;        $nonce = $_GET["nonce"];        // token参数$token = TOKEN;/// 加密/校验流程步骤//步骤1/ 将token、timestamp、nonce三个参数 进行 字典序排序(SORT_STRING);$tmpArr = array($token, $timestamp, $nonce);        sort($tmpArr, SORT_STRING); //use SORT_STRING rule;        //步骤2/ 将三个参数字符串拼接成一个字符串进行sha1加密$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );//步骤3/ 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信;if( $tmpStr == $signature ){return true;}else{return false;}}}?>


1 0
原创粉丝点击