小程序客服接入

2019-10-18 15:01 来源:未知

1.统计接入留存说明  更详细的说明,可以查看官网的文档 https://weixin.hotapp.cn/document

32450新蒲京网站 ,统计接入流程只需要4步
32450新蒲京网站 1

public function check_server(){//校验服务器地址URL

    if (isset($_GET['echostr'])) {

$this->valid();

    }else{

$this->responseMsg();

    }

}

public function valid()

{

$echoStr =$_GET["echostr"];

    if($this->checkSignature()){

header('content-type:text');

        echo $echoStr;

        exit;

    }else{

echo $echoStr.'+++'.'pksfc';

        exit;

    }

}

public function responseMsg()

{

$postStr =$GLOBALS["HTTP_RAW_POST_DATA"];

    if (!empty($postStr) && is_string($postStr)){

//禁止引用外部xml实体

//libxml_disable_entity_loader(true);

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

        $postArr =json_decode($postStr,true);

        if(!empty($postArr['MsgType']) &&$postArr['MsgType'] =='text'){//文本消息

            $fromUsername =$postArr['FromUserName'];  //发送者openid

            $toUserName =$postArr['ToUserName'];      //小程序id

            $textTpl =array(

"ToUserName"=>$fromUsername,

                "FromUserName"=>$toUserName,

                "CreateTime"=>time(),

                "MsgType"=>"transfer_customer_service",

            );

            exit(json_encode($textTpl));

        }elseif(!empty($postArr['MsgType']) &&$postArr['MsgType'] =='image'){//图文消息

            $fromUsername =$postArr['FromUserName'];  //发送者openid

            $toUserName =$postArr['ToUserName'];      //小程序id

            $textTpl =array(

"ToUserName"=>$fromUsername,

                "FromUserName"=>$toUserName,

                "CreateTime"=>time(),

                "MsgType"=>"transfer_customer_service",

            );

            exit(json_encode($textTpl));

        }elseif($postArr['MsgType'] =='event' &&$postArr['Event']=='user_enter_tempsession'){//进入客服动作

            $fromUsername =$postArr['FromUserName'];  //发送者openid

            $content ='您好,有什么能帮助你?';

            $data=array(

"touser"=>$fromUsername,

                "msgtype"=>"text",

                "text"=>array("content"=>$content)

);

            $json =$this->json_encode($data);  //php5.4+

            $access_token =$this->getAccessToken();

            /*

* POST发送https请求客服接口api

*/

            $url ="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;

            //以'json'格式发送post的https请求

            $curl = curl_init();

            curl_setopt($curl, CURLOPT_URL, $url);

            curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求

            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

            if (!empty($json)){

curl_setopt($curl, CURLOPT_POSTFIELDS,$json);

            }

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

            //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );

            $output = curl_exec($curl);

            if (curl_errno($curl)) {

echo 'Errno'.curl_error($curl);//捕抓异常

            }

curl_close($curl);

            if($output ==0){

echo 'success';exit;

            }

}else{

exit('aaa');

        }

}else{

echo "";

        exit;

    }

}

private function checkSignature()//官方的验证函数

{

$signature =$_GET["signature"];

    $timestamp =$_GET["timestamp"];

    $nonce =$_GET["nonce"];

    $token ='pksfc';

    $tmpArr =array($token, $timestamp, $nonce);

    sort($tmpArr, SORT_STRING);

    $tmpStr = implode($tmpArr );

    $tmpStr = sha1($tmpStr );

    if($tmpStr ==$signature ){

return true;

    }else{

return false;

    }

}

private function getAccessToken() {

// access_token 应该全局存储与更新,以下代码以写入到文件中做示例

    $data =json_decode($this->get_php_file("access_token.php"));

    if ($data->expire_time < time()) {

// 如果是企业号用以下URL获取access_token

// $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";

        $url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxeb81a27d58c415af&secret&secret=53a886e372afeadc0f9e86f3b1a4549c";

        $res =json_decode($this->curl_https($url));

        $access_token =$res->access_token;

        if ($access_token) {

$data->expire_time = time() +7000;

            $data->access_token =$access_token;

            $this->set_php_file("access_token.php", json_encode($data));

        }

}else {

$access_token =$data->access_token;

    }

return $access_token;

}

private function get_php_file($filename) {

return trim(substr(file_get_contents($filename), 15));

}

private function set_php_file($filename, $content) {

$fp = fopen($filename, "w");

    fwrite($fp, "" .$content);

    fclose($fp);

}

function curl_https($url){

$curl = curl_init();

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_TIMEOUT, 500);

    // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。

// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);

    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);

    curl_close($curl);

    return $res;

}

function json_encode($array)

{

if(version_compare(PHP_VERSION,'5.4.0','<')){

$str =json_encode($array);

        $str = preg_replace_callback("#\u([0-9a-f]{4})#i",function($matchs){

return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));

        },$str);

        return $str;

    }else{

return json_encode($array, JSON_UNESCAPED_UNICODE);

    }

}
(1)注册账号
  1. 打开http://weixin.hotapp.cn 点击注册
(2)下载js sdk 并接入

点击下载 hotapp js sdk下载

在app.js中接入统计

 var hotapp = require('util/hotapp.js')
    //在app.js中的onLaunch方法中调用hotapp.init("HotAPP的Key")
    App({
      onLaunch: function() {
         hotapp.init("此处填写您在HotApp应用后台中显示的AppKey")
      }
    })

 

(3)request 合法域名设置

微信的request 的请求必须有合法域名,必须把HotApp的后台 https://wxapi.hotapp.cn 设置为您的请求合法域名。

32450新蒲京网站 2

(4)接入完成

到这里统计就接入完成了,request安全域名保存并提交后,记住在开发工具里要刷新本地合法域名的缓存,然后就可以进入后台查看统计是否接入成功

 

TAG标签:
版权声明:本文由32450新蒲京网站发布于葡萄游戏厅_棋牌游戏,转载请注明出处:小程序客服接入