php 微信客服信息推送失败 微信重复推送客服消息 40001 45047
生活随笔
收集整理的這篇文章主要介紹了
php 微信客服信息推送失败 微信重复推送客服消息 40001 45047
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 /***
2 * 微信客服發送信息
3 * 微信客服信息推送失敗 微信重復推送客服消息 40001 45047
4 * 遞歸提交到微信 直到提交成功
5 * @param $openid
6 * @param int $is_repeat 是否遞歸
7 * @return bool
8 */
9 function wx_kf_sendh($openid, $is_repeat = 0)
10 {
11 $result = M('Reply')->where("is_open=1")->order('listorder desc')->select();
12
13 if(!$is_repeat) {
14 $logarr = M('wx_kf_sendlog')->where("openid='".$openid."'")->order('createtime desc')->find();
15 if($logarr && $logarr['createtime'] > time()-10) {
16 return false;
17 }
18 $dddata['openid'] = $openid;
19 $dddata['createtime'] = time();
20 $dddata['text'] = 0; //1 發送成功 0失敗
21 $dddata['image'] = 0; //1 發送成功 0失敗
22 $dddata['diy'] = 0; //1 發送成功 0失敗
23 $dddata['number'] = 0;
24 //$dddata['name'] = getVip($openid, 'nickname');
25 M('wx_kf_sendlog')->add($dddata);
26 }
27
28 foreach ($result as $k => $v) {
29
30 if (empty($v['type'])) {
31 continue;
32 }
33
34 if ($v['type'] == 'text') {
35 $logarr = M('wx_kf_sendlog')->where("openid='".$openid."'")->order('createtime desc')->find();
36 if($logarr['text'] == '1') {
37 continue;
38 }
39 $str = str_replace("<br/>", '\n', $v['description']);
40 $str = str_replace(' ', '\r', $str);
41 $str = str_replace('href="', "href='", $str);
42 $str = str_replace('"', "'", $str);
43 $data = '{
44 "touser":"' . $openid . '",
45 "msgtype":"text",
46 "text":
47 {
48 "content":"' . $str . '"
49 }
50 }';
51
52 //發送信息
53 $appid = getids('config', 56, 'value');
54 $appsecret = getids('config', 57, 'value');
55 $accesstokenarr = getAccessToken($appid, $appsecret);
56 $access_token = $accesstokenarr['access_token'];
57 //通過基礎消息接口發送的數據是XML格式的,但是調用客服接口發送的數據是json數據格式,更易傳輸。
58 $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;
59
60 $rest = https_request($url, $data);
61 $jsonsarr = json_decode($rest, true);
62 if($jsonsarr['errcode'] != '0') {
63
64 M('wx_kf_sendlog')->where("id=".$logarr['id'])->save(array('text'=>0, 'number'=>$logarr['number']+1,'logtime'=>$logarr['logtime'].'|text:'.date('Y-m-d H:i:s')));
65 wx_kf_send($openid,1);exit;
66 } else {
67 if($logarr['text'] == '0') {
68 M('wx_kf_sendlog')->where("id=".$logarr['id'])->save(array('text'=>1));
69 }
70 }
71
72 } elseif ($v['type'] == 'image') {
73
74 $logarr = M('wx_kf_sendlog')->where("openid='".$openid."'")->order('createtime desc')->find();
75 if($logarr['image'] == '1') {
76 continue;
77 }
78
79 $data = '{
80 "touser":"' . $openid . '",
81 "msgtype":"image",
82 "image":
83 {
84 "media_id":"' . $v['media_id'] . '"
85 }
86 }';
87
88 //發送信息
89 $appid = getids('config', 56, 'value');
90 $appsecret = getids('config', 57, 'value');
91 $accesstokenarr = getAccessToken($appid, $appsecret);
92 $access_token = $accesstokenarr['access_token'];
93 //通過基礎消息接口發送的數據是XML格式的,但是調用客服接口發送的數據是json數據格式,更易傳輸。
94 $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;
95
96
97 $rest = https_request($url, $data);
98 $jsonsarr = json_decode($rest, true);
99 $logarr = M('wx_kf_sendlog')->where("openid='".$openid."'")->order('createtime desc')->find();
100 if($jsonsarr['errcode'] != '0') {
101
102 M('wx_kf_sendlog')->where("id=".$logarr['id'])->save(array('image'=>0, 'number'=>$logarr['number']+1,'logtime'=>$logarr['logtime'].'|image:'.date('Y-m-d H:i:s')));
103 wx_kf_send($openid,1);exit;
104 } else {
105 if($logarr['text'] == '0') {
106 M('wx_kf_sendlog')->where("id=".$logarr['id'])->save(array('text'=>1));
107 }
108 }
109
110 } elseif ($v['type'] == 'diy') {
111 $logarr = M('wx_kf_sendlog')->where("openid='".$openid."'")->order('createtime desc')->find();
112 if($logarr['diy'] == '1') {
113 continue;
114 }
115 $data = '{
116 "touser":"' . $openid . '",
117 "msgtype":"image",
118 "image":
119 {
120 "media_id":"' . down_share_img($openid) . '"
121 }
122 }';
123
124 //發送信息
125 $appid = getids('config', 56, 'value');
126 $appsecret = getids('config', 57, 'value');
127 $accesstokenarr = getAccessToken($appid, $appsecret);
128 $access_token = $accesstokenarr['access_token'];
129 //通過基礎消息接口發送的數據是XML格式的,但是調用客服接口發送的數據是json數據格式,更易傳輸。
130 $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;
131
132 $rest = https_request($url, $data);
133 $jsonsarr = json_decode($rest, true);
134 if($jsonsarr['errcode'] != '0') {
135 M('wx_kf_sendlog')->where("id=".$logarr['id'])->save(array('text'=>0, 'number'=>$logarr['number']+1,'logtime'=>$logarr['logtime'].'|diy:'.date('Y-m-d H:i:s')));
136 wx_kf_send($openid,1);exit;
137 } else {
138 if($logarr['diy'] == '0') {
139 M('wx_kf_sendlog')->where("id=".$logarr['id'])->save(array('diy'=>1));
140 }
141 }
142
143 }
144 }
145 }
?
轉載于:https://www.cnblogs.com/handle/p/9529641.html
總結
以上是生活随笔為你收集整理的php 微信客服信息推送失败 微信重复推送客服消息 40001 45047的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【GitHub】github相关问题和解
- 下一篇: 【学习笔记】Sass入门指南