weiphp微信多客服系统插件
发布时间:2016-04-14, 17:02:37 分类:PHP | 编辑 off 网址 | 辅助
图集1/3
正文 1139字数 1,218,354阅读
使用方法:第一步:安装附件中插件
第二步:增加返回多客服系统函数
在
Application/Home/Model/WeixinModel.class.php
Run code
Cut to clipboard
/*
* 调用多客服系统
*/
public function replyCustomerService() {
$msg = array();
$this->_replyData ( $msg, 'transfer_customer_service' );
}
Run code
Cut to clipboard
第三部:没有匹配到关键词就调用多客服系统
将
Application/Home/Controller/WeixinController.class.php
Run code
Cut to clipboard
// 最终也无法定位到插件,终止操作
if (! isset ( $addons [$key] ) || ! file_exists ( ONETHINK_ADDON_PATH . $addons [$key] . '/Model/WeixinAddonModel.class.php' )) {
return false;
}
Run code
Cut to clipboard
修改为
// 最终也无法定位到插件,终止操作
if (! isset ( $addons [$key] ) || ! file_exists ( ONETHINK_ADDON_PATH . $addons [$key] . '/Model/WeixinAddonModel.class.php' )) {
$addons [$key] = 'CustomerService';
}
Run code
Cut to clipboard
注意:本插件只适用于认证的服务号!
附件:upload/attach/tbs-I0fiZSKKHT.rar
http://bbs.weiphp.cn/forum.php?mod=viewthread&tid=1269&page=1&_dsign=80135400
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 6 条评论 »
$res = $this->replyNews ( $articles ); } elseif ($keywordArr ['extra_text'] == 'custom_reply_news') { // 单条图文回复 $info = M ( 'custom_reply_news' )->where ( $map )->find (); // 组装微信需要的图文数据,格式是固定的 $articles [0] = array ( 'Title' => $info ['title'], 'Description' => $info ['intro'], 'PicUrl' => get_cover_url ( $info ['cover'] ), 'Url' => $this->_getNewsUrl ( $info, $param ) ); $res = $this->replyNews ( $articles ); } else { // 增加积分 add_credit ( 'custom_reply', 300 ); // 文本回复 $info = M ( 'custom_reply_text' )->where ( $map )->find (); $contetn = replace_url ( htmlspecialchars_decode ( $info ['content'] ) ); //2017.09.06 //'asdf'. if($keywordArr ['keyword']=='wxcustomservice123'){ $nowkeeff=$this->kfzzxx(); //$contetn = htmlspecialchars_decode($nowkeeff['kf_list'][0]['kf_account']); $contentnew='当前客服:'."\n\r"; foreach($nowkeeff['kf_list'] as $nk=>$nv){ $contentnew.= ($nk+1).'、'.$nv['kf_nick']."\n\r"; } if(!(count($nowkeeff['kf_list']))) $contentnew.='暂无客服人员接入。'; else $contentnew.='正在为您转接人工客服'; $contetn = ($contentnew);//htmlspecialchars_decode /*$articles [0] = array ( 'Title' => '当前客服:', 'Description' => $contentnew, 'PicUrl' => get_cover_url ( $info ['cover'] ), 'Url' => $this->_getNewsUrl ( $info, $param ) ); $res = $this->replyNews ( $articles ); exit;*/ //$articles [0]['Title']='fads';//.$keywordArr ['keyword'];//$nowkeeff['kf_list'][0]['kf_account']; } //end $this->replyText ( $contetn ); } } //获取在线客服 function kfzzxx(){ //exit; $access_token=get_access_token(); $url='https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token='.$access_token; $content = file_get_contents($url); //echo $sss=$this->get_web_page($url); return $sss=json_decode($content, true); //dump($content); //header('Location:'.$url); //exit; }
/Addons/CustomReply/Model/WeixinAddonModel.class.php
php 中去除中文的换行,str_replace(PHP_EOL,'',($res['share_content']));
<?php $jsapi_ticket=get_jsapi_ticket(); //$access_token=get_access_token(); $appinfo=get_token_appinfo(); require_once "jssdk.php"; $jssdk = new JSSDK($appinfo['appid'],$appinfo['secret'],$access_token=0,$jsapi_ticket); $signPackage = $jssdk->GetSignPackage(); //dump($signPackage); ?> <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script> /* * 注意: * 1. 所有的JS接口只能在公众号绑定的域名下调用,公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。 * 2. 如果发现在 Android 不能分享自定义内容,请到官网下载最新的包覆盖安装,Android 自定义分享接口需升级至 6.0.2.58 版本及以上。 * 3. 常见问题及完整 JS-SDK 文档地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html * * 开发中遇到问题详见文档“附录5-常见错误及解决办法”解决,如仍未能解决可通过以下渠道反馈: * 邮箱地址:weixin-open@qq.com * 邮件主题:【微信JS-SDK反馈】具体问题 * 邮件内容说明:用简明的语言描述问题所在,并交代清楚遇到该问题的场景,可附上截屏图片,微信团队会尽快处理你的反馈。 */ //console.log('fdasfdas'); wx.config({ /*debug: true,*/ appId: '<?php echo $signPackage["appId"];?>', timestamp: <?php echo $signPackage["timestamp"];?>, nonceStr: '<?php echo $signPackage["nonceStr"];?>', signature: '<?php echo $signPackage["signature"];?>', jsApiList: [ // 所有要调用的 API 都要加到这个列表中 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone' ] }); wx.ready(function () { // 在这里调用 API wx.onMenuShareTimeline({ imgUrl: 'http://***lizhenqiu.com/222.jpg' }); wx.onMenuShareAppMessage({ desc: '地址:<?php echo $config['xl_dz'];?>\n电话:<?php echo $config['xl_man'];?>', // 分享描述 imgUrl: 'http://***lizhenqiu.com/222.jpg' }); wx.onMenuShareQQ({ desc: '地址:<?php echo $config['xl_dz'];?>\n电话:<?php echo $config['xl_man'];?>', // 分享描述 imgUrl: 'http://***lizhenqiu.com/222.jpg' }); wx.onMenuShareWeibo({ desc: '地址:<?php echo $config['xl_dz'];?>\n电话:<?php echo $config['xl_man'];?>', // 分享描述 imgUrl: 'http://***lizhenqiu.com/222.jpg' }); wx.onMenuShareQZone({ desc: '地址:<?php echo $config['xl_dz'];?>\n电话:<?php echo $config['xl_man'];?>', // 分享描述 imgUrl: 'http://***lizhenqiu.com/222.jpg' }); }); </script>
// 判断公众号的类型:是订阅号还是服务号 function get_token_type($token = '') { $info = get_token_appinfo ( $token ); return intval ( $info ['type'] ); } // 获取jsapi_ticket,自动带缓存功能 function get_jsapi_ticket($token = '') { empty ( $token ) && $token = get_token (); $key = 'jsapi_ticket_' . $token; $res = S ( $key ); if ($res !== false) return $res; $info = get_token_appinfo ( $token ); if (empty ( $info ['appid'] ) || empty ( $info ['secret'] )) { S ( $key, 0, 7200 ); return 0; } $accessToken=get_access_token(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $tempArr = json_decode ( file_get_contents ( $url ), true ); //dump($tempArr);exit; if (@array_key_exists ( 'ticket', $tempArr )) { S ( $key, $tempArr ['ticket'], 7200 ); return $tempArr ['ticket']; } else { return 0; } } // 获取access_token,自动带缓存功能 function get_access_token($token = '') { empty ( $token ) && $token = get_token (); $key = 'access_token_' . $token; $res = S ( $key ); if ($res !== false) return $res; $info = get_token_appinfo ( $token ); if (empty ( $info ['appid'] ) || empty ( $info ['secret'] )) { S ( $key, 0, 7200 ); return 0; } $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $info ['appid'] . '&secret=' . $info ['secret']; // 如果是企业号用以下URL获取access_token //if(intval ( $info ['type'] )==1) $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' . $info ['appid'] . '&corpsecret=' . $info ['secret']; $tempArr = json_decode ( file_get_contents ( $url ), true ); if (@array_key_exists ( 'access_token', $tempArr )) { S ( $key, $tempArr ['access_token'], 7200 ); return $tempArr ['access_token']; } else { return 0; } }
<?php class JSSDK { private $appId; private $appSecret; private $access_token; private $jsapi_ticket; public function __construct($appId, $appSecret,$access_token,$jsapi_ticket) { $this->appId = $appId; $this->appSecret = $appSecret; $this->access_token = $access_token; $this->jsapi_ticket = $jsapi_ticket; } public function getSignPackage() { $jsapiTicket = $this->jsapi_ticket;//$this->getJsApiTicket(); // 注意 URL 一定要动态获取,不能 hardcode. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = $this->createNonceStr(); // 这里参数的顺序要按照 key 值 ASCII 码升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } private function getJsApiTicket() { return $this->jsapi_ticket; // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 $data = json_decode($this->get_php_file("jsapi_ticket.php")); if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); // 如果是企业号用以下 URL 获取 ticket $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; //$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; $this->set_php_file("jsapi_ticket.php", json_encode($data)); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } private function getAccessToken() { return $this->access_token; // 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=$this->appId&secret=$this->appSecret"; $res = json_decode($this->httpGet($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 httpGet($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, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } 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, "<?php exit();?>" . $content); fclose($fp); } }
\r回到行首 \n换行
\n是换行,英文是New line,表示使光标到行首
\r是回车,英文是Carriage return,表示使光标下移一格
\r\n表示回车换行
我们在平时使用电脑时,已经习惯了回车和换行一次搞定,敲一个回车键,即是回车,又是换行。
1、\n 软回车:
在Windows 中表示换行且回到下一行的最开始位置。相当于Mac OS 里的 \r 的效果。
在Linux、unix 中只表示换行,但不会回到下一行的开始位置。
2、\r 软空格:
在Linux、unix 中表示返回到当行的最开始位置。
在Mac OS 中表示换行且返回到下一行的最开始位置,相当于Windows 里的 \n 的效果。
3、\t 跳格(移至下一列)。
它们在双引号或定界符表示的字符串中有效,在单引号表示的字符串中无效。
\r\n 一般一起用,用来表示键盘上的回车键,也可只用 \n。
\t表示键盘上的“TAB”键。
就像你使用 enter和shift+enter的区别
4、文件中的换行符号:
linux,unix: \r\n windows : \n Mac OS : \r
5、常用转义符号的意义:
\n LF或ASCII中的0x0A(10) \r CR或ASCII中的0x0D(13) \t 水平制表符-HT或ASCII中的0x09(9) \\ 反斜杠 \$ 美圆符 \" 双引号 \' 单引号
6、"\r\n"与"</br>"的区别
\r\n是输出的HTML代码换行,客户看到的效果没有换行。
如果是输出给浏览器,就用<br/>
\r\n,好比你在DreamWeaver里做一个网页,在源代码里按一下回车,是给源代码回车换行。
是输出一个(HTML标记性语言),在HTML中解释为输入的字换行。 \r\n的作用是给源代码回车换行,输入后,查看html源代码,就看会到有换行,而HTML解释器(浏览器)认为它是没用的东西,用不用都一样显示。
$info = M ( 'custom_reply_text' )->where ( $map )->find ();
$contetn = replace_url ( htmlspecialchars_decode ( $info ['content'] ) );
//if(!$content) $content='暂无客服人员接入。';
//2017.09.06
//'asdf'.
if($keywordArr ['keyword']=='wxcustomservice123' || !$content){
// 文本回复 $info = M ( 'custom_reply_text' )->where ( $map )->find (); $contetn = replace_url ( htmlspecialchars_decode ( $info ['content'] ) ); //if(!$content) $content='暂无客服人员接入。'; //2017.09.06 //'asdf'. if($keywordArr ['keyword']=='wxcustomservice123' || !$content){
微信上传永久素材图文,一直报{"errcode":44003,"errmsg":"empty news data"}。
看了接口参数,使用post,就是出现44003
最后发现原因,是数据错误导致
数据参考格式: $jsonArr = array( "articles"=> array( array(// 我就是少了这层array 才会报empty news data 错误 "title"=> 'dingdingdemo', "thumb_media_id"=> '填写素材id', "author"=> 'martin', "digest"=> 'digest', "show_cover_pic"=> 0, "content"=> $content, "content_source_url"=> 'https://www.baidu.com/', ), array( "title"=> 'dingdingdemo2', "thumb_media_id"=> '填写素材id', "author"=> 'martin', "digest"=> 'digest', "show_cover_pic"=> 0, "content"=> "content", "content_source_url"=> 'https://www.baidu.com/', ) ), ); $json3=json_encode($jsonArr,JSON_UNESCAPED_UNICODE); 进行传输
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); echo"<br/>"; var_dump(json_decode($json, true)); ?>
数组$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';被json_decode()解码,转换回来的数据是对象,var_dump(json_decode($json))得到的是一个对象,如下:
object(stdClass)#1 (5) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) }
那么,要怎么样才能把json数组转换为php格式的数组呢,采用以下方式:
json_decode($json, true)
这样得到的数据就是php的数组了:
var_dump(json_decode($json, true));
效果如下:
array(5) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) }