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 }
如果本文章已帮助到您!