微信测试号接入--URL配置

来源:互联网 发布:衡水中学 知乎 编辑:程序博客网 时间:2024/06/05 11:02

一、准备工作

需要一个能提供外网访问的 url 地址

a. 获取方式:去阿里云、腾讯云、新浪云等等平台上购买一台虚拟主机。
b. 注意点:可能有的小伙伴用的是 花生壳动态内网穿透 所提供的外网域名。确实该域名是外网域名,但是该外网域名端口不是固定的 80(http) 或 443(https) 端口,所以配置会失败。

二、代码展示

配置失败的原因:很可能是编码问题,在代码最上层添加 utf-8 编码。
<?php/**  * wechat php test  *///如果配置失败,可能是编码问题,加上这一句header("Content-Type:text/plain;charset=utf-8");//define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();/** 该方法验证成功后,就不要调用了 */$wechatObj->valid();class wechatCallbackapiTest {    /**     * 验证     * @return [type] [description]     */    public function valid() {        $echoStr = $_GET["echostr"];        //valid signature , option        if($this->checkSignature()) {            // ob_clean();            echo $echoStr;            exit;        }    }    /**     * 响应消息     * @return [type] [description]     */    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 = "<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;        }    }    /**     * 校验签名     * @return [type] [description]     */    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;        }    }}

三、结果

原创粉丝点击