//获取access_tokenprivate static function get_access_token($app_id){$getAuthorizerInfo = wx_auth::getAuthorizerInfo($app_id);$access_token = wx_auth::getAuthorizerAccessToken($app_id, $getAuthorizerInfo['authorization_info']['authorizer_refresh_token']);return $access_token;}//客服回复用户信息public static function reply_customer($open_id, $content){$app_id = WxUser::getWxUserInfoByOpenId($open_id)['app_id'];$data = '{"touser":"' . $open_id . '","msgtype":"text","text":{"content":"' . $content . '"}}';$access_token = self::get_access_token($app_id);$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;$result = wx_tools::postCurl($url, $data);return json_decode($result, true);}//获取所有客服账号public static function get_customer_account_list($app_id){$access_token = self::get_access_token($app_id);$url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" . $access_token;$result = wx_tools::getCurl($url);return json_decode($result, true);}//邀请微信号到客服public static function invite_customer_account($kf_account, $invite_wx, $app_id){$access_token = self::get_access_token($app_id);$data = '{"kf_account":"' . $kf_account . '","invite_wx":"' . $invite_wx . '"}';$url = "https://api.weixin.qq.com/customservice/kfaccount/inviteworker?access_token=" . $access_token;$result = wx_tools::postCurl($url, $data);return json_decode($result, true);}//添加客服账号public static function add_customer_account($kf_account, $nickname, $password, $app_id){$access_token = self::get_access_token($app_id);$data = '{"kf_account":"' . $kf_account . '","nickname":"' . $nickname . '","text":"' . $password . '"}';$url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" . $access_token;$result = wx_tools::postCurl($url, $data);return json_decode($result, true);}//设置微信头像public static function upload_head_img($app_id, $kf_account, $file){$access_token = self::get_access_token($app_id);$url = 'https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=' . $access_token . '&kf_account=' . $kf_account;$tmp_name = $file['tmp_name'];$type = $file['type'];$path = $file['name'];$result = wx_tools::curl_post_file($url, $tmp_name, $type, $path);return $result;}//修改客服账号public static function modify_customer_account($kf_account, $nickname, $password, $app_id){$access_token = self::get_access_token($app_id);$data = '{"kf_account":"' . $kf_account . '","nickname":"' . $nickname . '","text":"' . $password . '"}';$url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=" . $access_token;$result = wx_tools::postCurl($url, $data);return json_decode($result, true);}//删除客服帐号public static function remove_customer_account($kf_account, $app_id){$access_token = self::get_access_token($app_id);$data = '{"kf_account":"' . $kf_account . '"}';$url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=" . $access_token;$result = wx_tools::postCurl($url, $data);return json_decode($result, true);}//获取用户与客服之间的聊天记录public static function get_customer_service_chat_record($starttime, $endtime, $msgid, $number, $app_id){$access_token = self::get_access_token($app_id);$data = '{"starttime":"' . $starttime . '","endtime":"' . $endtime . '","msgid":"' . $msgid . '","number":"' . $number . '"}';$url = "https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=" . $access_token;$result = wx_tools::postCurl($url, $data);return json_decode($result, true);}

我自己的wx_tools 文件

 /*** 以post方式提交xml到对应的接口url* @param string $url 提交地址* @param string $param 需要post的xml数据* @param bool $file 是否上传文件* @param bool|array $cert 是否需要证书,默认不需要 如果是数组代表有证书地址 请按以下格式 array('cert' => 'cert.pem', 'key' => 'key.pem', 'rootca' => 'rootca.pem');* @param int $second* @return mixed*/public static function postCurl($url, $param, $file = false, $cert = false, $second = 30){$curl = curl_init();//设置超时curl_setopt($curl, CURLOPT_TIMEOUT, $second);if (stripos($url, "https://") !== FALSE) {curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {$is_file = true;} else {$is_file = false;if (defined('CURLOPT_SAFE_UPLOAD')) {curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);}}if (is_string($param)) {$str_post = $param;} elseif ($file) {if ($is_file) {foreach ($param as $key => $val) {if (substr($val, 0, 1) == '@') {$param[$key] = new \CURLFile(realpath(substr($val, 1)));}}}$str_post = $param;} else {$post = array();foreach ($param as $key => $val) {$post[] = $key . "=" . urlencode($val);}$str_post = join("&", $post);}//设置证书 todo 未验证if (is_array($cert)) {//请确保您的libcurl版本是否支持双向认证,版本高于7.20.1 使用证书:cert 与 key 分别属于两个.pem文件curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');curl_setopt($curl, CURLOPT_SSLCERT, $cert['cert']);curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');curl_setopt($curl, CURLOPT_SSLKEY, $cert['key']);//红包使用if (empty($cert['rootca'])) {curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');curl_setopt($curl, CURLOPT_CAINFO, $cert['rootca']);}}curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_POST, true);curl_setopt($curl, CURLOPT_POSTFIELDS, $str_post);$content = curl_exec($curl);$status = curl_getinfo($curl);if (intval($status["http_code"]) == 200) {curl_close($curl);
//            ApiLog::setMessage(\Yii::$app->session->get('request_base_api_log_id'),['url' => $url, 'message'=> $content], 1);return $content;} else {$error = curl_errno($curl);curl_close($curl);
//            $this->err_code = $error;
//            $this->err_msg = $this->curl_error[$error];
//            ApiLog::setMessage(\Yii::$app->session->get('request_base_api_log_id'),['url' => $url, 'message'=> $content], 0);return false;}}/*** CURL GET 请求* @param $url* @return bool|mixed*/public static function getCurl($url){$curl = curl_init();if (stripos($url, "https://") !== FALSE) {curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$content = curl_exec($curl);$status = curl_getinfo($curl);if (intval($status["http_code"]) == 200) {curl_close($curl);
//            ApiLog::setMessage(\Yii::$app->session->get('request_base_api_log_id'),['url' => $url, 'message'=> $content], 1);return $content;} else {$error = curl_errno($curl);curl_close($curl);file_put_contents('../web/logs/notify/error' . date('YmdHi') . '.txt', $error);
//            ApiLog::setMessage(\Yii::$app->session->get('request_base_api_log_id'),['url' => $url, 'message'=> $content], 0);
//            $this->err_code = $error;
//            $this->err_msg = $this->curl_error[$error];return false;}}/*** 微信api不支持中文转义的json结构* @param $arr* @return string*/public static function jsonEncode($arr){if (count($arr) == 0) return "[]";$parts = array();$is_list = false;//Find out if the given array is a numerical array$keys = array_keys($arr);$max_length = count($arr) - 1;if (($keys [0] === 0) && ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1$is_list = true;for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its positionif ($i != $keys [$i]) { //A key fails at position check.$is_list = false; //It is an associative array.break;}}}foreach ($arr as $key => $value) {if (is_array($value)) { //Custom handling for arraysif ($is_list)$parts [] = self::jsonEncode($value); /* :RECURSION: */else$parts [] = '"' . $key . '":' . self::jsonEncode($value); /* :RECURSION: */} else {$str = '';if (!$is_list)$str = '"' . $key . '":';//Custom handling for multiple data typesif (!is_string($value) && is_numeric($value) && $value < 2000000000)$str .= $value; //Numberselseif ($value === false)$str .= 'false'; //The booleanselseif ($value === true)$str .= 'true';else$str .= '"' . addslashes($value) . '"'; //All other things// :TODO: Is there any more datatype we should be in the lookout for? (Object?)$parts [] = $str;}}$json = implode(',', $parts);if ($is_list)return '[' . $json . ']'; //Return numerical JSONreturn '{' . $json . '}'; //Return associative JSON
    }/*** 数据解析* @param $data* @return bool|mixed*/public static function parseData($data){$data = json_decode($data, true);return $data;}/*** 使用curl 文件上传 版本大于5.5* @param $url* @param $tmp_name* @param $type* @param $path* @return int|mixed*/public static function curl_post_file($url, $tmp_name, $type, $path){$curl = curl_init();curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);$data = ['file' => new \CURLFile($tmp_name, $type, $path)];curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, $data);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_USERAGENT, "TEST");$result = curl_exec($curl);$status = curl_getinfo($curl);if (intval($status["http_code"]) == 200) {curl_close($curl);return $result;}$error = curl_errno($curl);curl_close($curl);return $error;}

转载于:https://www.cnblogs.com/lt-com/p/8073771.html

PHP 微信公众号之客服完整讲解相关推荐

  1. 微信公众号 智能客服

    前言 微信公众号的开发,园子里有很多资料,这里简述. 虽说是智能,现在是仿佛智障,很多是hard code逻辑,日后将逐步加入LUIS,现在一些常用的打招呼(你好,您好,hi,hey,hello,ho ...

  2. 如何找到微众银行的微信公众号在线客服

    如何找到微众银行的微信公众号在线客服 微众银行是国内首家互联网银行,当我们不知道怎么开通使用或者别的问题时,需要找微众银行在线客服咨询.今天小编就给大家介绍一下咨询微众银行在线客服的步骤吧.微众客服 ...

  3. java微信公众号多客服_微信公众号多客服功能怎么实现?

    原标题:微信公众号多客服功能怎么实现? 微信公众号多客服功能怎么实现?为了方便更好地接待公众号的用户,很多企业想要为公众号接入多客服,以此提升公众号服务.公众号多客服功能可以通过接入米多客公众号客服软 ...

  4. 微信公众号多客服开发介绍

    非开发者模式:如果公众号没有处于开发者模式,则只需要单纯的在 微信公众号后台进行设置就可以启用多客服功能 开发者模式: 用户发送消息时的2种情况:          普通微信用户向公众号发消息时,微信 ...

  5. java微信公众号多客服_WPF 实现微信公众号多客服(效果实现篇)

    简介: 这是利用WPF作为前端技术,实现桌面版微信多客服系统.项目采用Prism作为前端框架,采用MVVM模式极好的对UI和逻辑代码分离,使用MefBootstrapper集成的MEF IOC容器,解 ...

  6. 微信公众号多客服系统自动分组系统

    学习交流: 之前给别人做过很多的微信公众号扫描带参数二维码实现自动分组的系统,系统使用客户超过20多家,其中有几家公司的 客户量比较大,多个2-5w的,还有一个20W+的,这样就需要一个比较完善的多刻 ...

  7. 微信客服消息html链接,微信公众号利用客服消息和模板消息实现微信群发

    1.关于群发接口和消息接口 关于群发接口 1.订阅号每天可以群发消息一条,服务号每月(自然月)四条的群发权限.开发者模式下,可以通过高级群发接口,实现更灵活的群发能力. 2.注意 ● 对于认证订阅号, ...

  8. 微信公众号在线客服接入发方法和功能详解

    微信公众号可以设置在线客服功能,相信很多朋友都知道 ,但是具体怎么设置能让客服功能使用起来呢?我给大家说下流程. 点击添加插件,找到客服功能,点击添加, 添加后在左边功能菜单就有了客服功能这一选项,点 ...

  9. 微信公众号利用客服接口主动给用户发送消息的方法

    目前微信并没有放出主动给用户发送消息的接口,但是我们可以使用其多客服接口来达到主动给用户发送消息的目的. 当用户和公众号产生特定动作的交互时(具体动作列表请见下方说明),微信将会把消息数据推送给开发者 ...

最新文章

  1. IIS+PHP+MySQL+Zend Optimizer+GD库+phpMyAdmin安装配置[完整修正实用版]
  2. Windows组策略屏蔽U盘有妙法
  3. 5.QT5中的connect的实现
  4. SPSS学习笔记之——两独立样本的非参数检验(Mann-Whitney U )
  5. 或许每条喵咪上辈子都是陨落的码农
  6. 苹果6怎样打开html,苹果iPhone的Safari浏览器使用技巧图解
  7. php取结果集,php获取数据库结果集方法(推荐)
  8. Java高级语法笔记-语法支持的异常
  9. 基本线程同步(五)使用Lock同步代码块
  10. Java消息中间件--JMS规范
  11. 正确关闭线程池:shutdown 和 shutdownNow 的区别
  12. tf 矩阵行和列交换_tf.transpose函数的用法讲解
  13. Heritrix 3.1.0 源码解析(十四)
  14. .NET应用架构设计—表模块模式与事务脚本模式的代码编写
  15. 重读领域驱动设计——如何说好一门通用语言
  16. php 密码加密方法
  17. PMBOK(第五版)学习笔记 —— 3 项目管理过程
  18. Vue_路由_query参数_params参数_命名路由_props配置_编程式路由导航_缓存路由组件_新的生命周期钩子_全局、独享、组件内路由守卫_路由的两种工作模式
  19. java预研项目_缓存java框架技术预研3:JAVA缓存技术介绍
  20. PCF应用管理平台介绍(PCF Apps Manager)

热门文章

  1. php soap 下载文件,允许下载SOAP API响应(PHP)中的PDF文件get(作为附件)
  2. vector 插入_Java学习五分钟系列:对比Vector、ArrayList、LinkedList
  3. EXCEL 将选中列改为只读
  4. 总结nodejs的优缺点
  5. sqlserver2008r2 还原bak文件
  6. oracle关于时间的处理,如计算间隔天数、获取本年第一天、上月第一天、上月最后一天
  7. zk的数据一致性问题
  8. vb中空操作(等待)的指令、延时方法
  9. 抄底公式---预测黑马
  10. Redis实现计数器---接口防刷---升级版(Redis+Lua)