微信小程序> 微信小程序客服自动回复——PHP实现

微信小程序客服自动回复——PHP实现

浏览量:862 时间: 来源:@hardy

先说怎么用,然后使用成功了自己研究代码,驱动式学习多好。

  1. 首先将这个PHP程序的文件放在你 自己的服务器里面
    注意php程序里的APP_ID的值改成你自己的appid。还有APP_SECRET 的值改成你自己的appSecret。还有记好配置的token令牌,等会配置用到。

  2. 然后打开消息推送
    在小程序后台“开发”->“开发设置”->下拉有一个消息推送,打开。
    服务器地址填写这个PHP文件的位置https://**********.php
    TOKEN(令牌) ----这里随便写,反正与PHP程序里的开头那里我 的是dertertsdf
    define(“TOKEN”,“dertertsdf”);//自己设置的Token
    密钥随机生成
    然后模式选兼容模式
    接受类型选“XML”
    这是配置,然后如果你急着提交会发现“taken(令牌) 失效”。。。。。就对了

  3. 验证
    在250行左右有一个代码,这是验证用的,因为当你打开消息推送的时候腾讯的服务器会给你写的地址发送一个请求进行验证只有你返回正确的信息才能验证成功,提交才不会出现“taken(令牌) 失效”字样。然后才会提交成功,所以当提交时不要注释

PS:建议把绑定的客服都解绑,因为我发现有时候会出现bug,会把用户联系 客服的消息转发给客服了不发送给服务器也就是自动回复那里收不到。

//注意:第一步验证时打开,验证完成之后就可以注释了//$wechatObj->isValid();
  1. 修改
    顺利完成了上面的步骤后你打开客服应该会有自动打招呼了。
    然后就自己看代码发挥了,对应封装一个$data变量调用官方文档说的发送接口就可以了
    如果想发送图片,需要先把图片发送给腾讯服务器,然后腾讯服务器会返回一个媒体的ID,用这个ID调用发送接口就可以了。程序里我也写好了输入上传图片地址返回ID的函数了。
    不过我偷懒直接调用系统指令curl,所以需要PHP的shell_exec()这个函数。为了安全起见这个函数一般默认禁用的,所以需要在php配置文件里该一下,百度就可以。over
    下面时所有代码。可以直接复制粘贴用【手动滑稽】
<?php define("TOKEN","dertertsdf");//自己设置的Tokenclass wechatAPI{    const APP_ID = 'wx***********'; //你自己的appid    const APP_SECRET = '017ec3*****************';//你自己生成的appSecret        //用于小程序第一步验证返回    public function isValid(){        $echoStr = $_GET["echostr"];        if ($this->checkSignature()) {            echo $echoStr;            exit;        }    }        public function checkSignature(){        $signature = $_GET["signature"];        $timestamp = $_GET["timestamp"];        $nonce = $_GET["nonce"];        $token = TOKEN;        $tmpArr = array($token, $timestamp, $nonce);        sort($tmpArr, SORT_STRING);        $tmpStr = implode( $tmpArr );        $tmpStr = sha1( $tmpStr );        if( $tmpStr == $signature ){            return true;        }else{            return false;        }    }       public function send($data){        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->getAccessToken();        $data = urldecode(json_encode($data));        $this->curl_post($url,$data);    }        //xml数据转数组    public function xml2Array($contents = NULL, $encoding = 'UTF-8', $get_attributes = 1, $priority = 'tag'){        if (!$contents)         {            return array();        }        if (!function_exists('xml_parser_create'))        {            return array ();        }        $parser = xml_parser_create('');        xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $encoding);        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);        xml_parse_into_struct($parser, trim($contents), $xml_values);        xml_parser_free($parser);        if (!$xml_values)            return array();         $xml_array = array ();        $parents = array ();        $opened_tags = array ();        $arr = array ();        $current = & $xml_array;        $repeated_tag_index = array ();         foreach ($xml_values as $data)        {            unset ($attributes, $value);            extract($data);            $result = array ();            $attributes_data = array ();            if (isset ($value))            {                if ($priority == 'tag')                    $result = trim($value);                else                    $result['value'] = trim($value);            }            if (isset ($attributes) && $get_attributes) {                foreach ($attributes as $attr => $val)                {                    if ($priority == 'tag')                        $attributes_data[$attr] = $val;                    else                        $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'                }            }            if ($type == "open")            {                 $parent[$level -1] = & $current;                if (!is_array($current) || (!in_array($tag, array_keys($current)))) {                    $current[$tag] = $result;                    if ($attributes_data)                        $current[$tag . '_attr'] = $attributes_data;                    $repeated_tag_index[$tag . '_' . $level] = 1;                    if (isset($tag) && $tag && isset($current[$tag])) {                        $current = & $current[$tag];                    }                }                else                {                    if (isset ($current[$tag][0]))                    {                        $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;                        $repeated_tag_index[$tag . '_' . $level]++;                    }                    else                    {                         $current[$tag] = array (                            $current[$tag],                            $result                        );                         $repeated_tag_index[$tag . '_' . $level] = 2;                        if (isset ($current[$tag . '_attr']))                        {                            $current[$tag]['0_attr'] = $current[$tag . '_attr'];                            unset ($current[$tag . '_attr']);                        }                    }                    $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;                    $current = & $current[$tag][$last_item_index];                }            }            elseif ($type == "complete")            {                if (!isset ($current[$tag]))                {                    $current[$tag] = $result;                    $repeated_tag_index[$tag . '_' . $level] = 1;                    if ($priority == 'tag' && $attributes_data) {
            
            

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎