微信小程序第三方平台开发【全网发布及全网发布接入检测】

折腾了几个小时,终于通过了全网发布接入检测,进入审核阶段,半个小时之后审核通过。


https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318611&lang=zh_CN

这个是官方链接,不知是我看走眼了,还是官方写的模模糊糊,总之网上查阅了不少资料才明白了思路。

好了,进入正题……

1、为了不必要的麻烦,我直接把官方给定的测试公众号、测试小程序的原始ID加入了白名单里。



2、由于我是开发小程序,这里创建小程序第三方平台时勾选的权限集,根据官方的意思,如果勾选了消息这类权限,就要多弄一些检测。


3、根据官方文档步骤,我最终只实现了两步便成功通过全网发布接入检测。

并且所有检测代码都在“消息与事件接收URL”的URL中完成。顺便说下小程序业务域名是指在web-view组件中承载网页的外链。


第一步:采用自动回复文本的方式

第二步:使用客服消息接口发送消息


全部代码如下:

    /**
     * 消息与事件接收URL
     */
    public function receiveEvent()
    {
        // 每个授权小程序的appid,在第三方平台的消息与事件接收URL中设置了 $APPID$ 
        $authorizer_appid = I('param.appid/s'); 
        // 每个授权小程序传来的加密消息
        $postStr = file_get_contents("php://input");
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

            $toUserName = trim($postObj->ToUserName);
            $encrypt = trim($postObj->Encrypt);

            $format = "<xml><ToUserName><![CDATA[{$toUserName}]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
            $from_xml = sprintf($format, $encrypt);

            $inputs = array(
                'encrypt_type' => '',
                'timestamp' => '',
                'nonce' => '',
                'msg_signature' => '',
                'signature' => ''
            );
            foreach ($inputs as $key => $value) {
                $tmp = $_REQUEST[$key];
                if (!empty($tmp)){
                    $inputs[$key] = $tmp;
                }
            }

            // 第三方收到公众号平台发送的消息
            $msg = '';
            $timeStamp = $inputs['timestamp'];
            $msg_sign = $inputs['msg_signature'];
            $nonce = $inputs['nonce'];
            $token = 'xxxxxxxxxxx';
            $encodingAesKey = 'xxxxxxxxxxxxxxxx';
            $appid = 'xxxxxxxx';
            $appsecret = 'xxxxxxxxxxxxxx';
            vendor('minicrypto.wxBizMsgCrypt');
            $pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appid);
            $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
            if ($errCode == 0) {
                $msgObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
                $content = trim($msgObj->Content);

                //第三方平台全网发布检测普通文本消息测试 
                if (strtolower($msgObj->MsgType) == 'text' && $content == 'TESTCOMPONENT_MSG_TYPE_TEXT') {
                    $toUsername = trim($msgObj->ToUserName);
                    if ($toUsername == 'gh_3c884a361561') { 
                        $content = 'TESTCOMPONENT_MSG_TYPE_TEXT_callback'; 
                        echo $this->responseText($msgObj, $content);
                    }
                }
                //第三方平台全网发布检测返回api文本消息测试 
                if (strpos($content, 'QUERY_AUTH_CODE') !== false) { 
                    $toUsername = trim($msgObj->ToUserName);
                    if ($toUsername == 'gh_3c884a361561') { 
                        $query_auth_code = str_replace('QUERY_AUTH_CODE:', '', $content);
                        $params = $this->dedeLogic->api_query_auth($query_auth_code);
                        $authorizer_access_token = $params['authorization_info']['authorizer_access_token']; 
                        $content = "{$query_auth_code}_from_api"; 
                        $this->sendServiceText($msgObj, $content, $authorizer_access_token);
                    }
                }
                // file_put_contents ( ROOT_PATH."/log.txt", date ( "Y-m-d H:i:s" ) . "  " . var_export($msgObj,true) . "\r\n", FILE_APPEND );
            }
        }
        echo "success";
    }

    /**
     * 自动回复文本
     */
    public function responseText($object = '', $content = '')
    {
        if (!isset($content) || empty($content)){
            return "";
        }

        $xmlTpl =   "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[text]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                    </xml>";
        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);

        return $result;
    }

    /**
     * 发送文本消息
     */
    public function sendServiceText($object = '', $content = '', $access_token = '')
    {
        /* 获得openId值 */
        $openid = (string)$object->FromUserName;
        $post_data = array(
            'touser'    => $openid,
            'msgtype'   => 'text',
            'text'      => array(
                            'content'   => $content
                        )
        );
        $this->sendMessages($post_data, $access_token);
    } 
    
    /**
     * 发送消息-客服消息
     */
    public function sendMessages($post_data = array(), $access_token = '')
    {
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
        httpRequest($url, 'POST', json_encode($post_data, JSON_UNESCAPED_UNICODE));
    }   

    /**
     * CURL请求
     * @param $url 请求url地址
     * @param $method 请求方法 get post
     * @param null $postfields post数据数组
     * @param array $headers 请求header信息
     * @param bool|false $debug  调试开启 默认false
     * @return mixed
     */
    function httpRequest($url, $method="GET", $postfields = null, $headers = array(), $debug = false) {
        $method = strtoupper($method);
        $ci = curl_init();
        /* Curl settings */
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
        curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
        switch ($method) {
            case "POST":
                curl_setopt($ci, CURLOPT_POST, true);
                if (!empty($postfields)) {
                    $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
                }
                break;
            default:
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
                break;
        }
        $ssl = preg_match('/^https:\/\//i',$url) ? TRUE : FALSE;
        curl_setopt($ci, CURLOPT_URL, $url);
        if($ssl){
            curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
            curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
        }
        //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
        curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, true);
        /*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
        $response = curl_exec($ci);
        $requestinfo = curl_getinfo($ci);
        $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
        if ($debug) {
            echo "=====post data======\r\n";
            var_dump($postfields);
            echo "=====info===== \r\n";
            print_r($requestinfo);
            echo "=====response=====\r\n";
            print_r($response);
        }
        curl_close($ci);
        return $response;
        //return array($http_code, $response,$requestinfo);
    }


猜你喜欢

转载自blog.csdn.net/xiao_hu520/article/details/79727933