PHP微信模版消息有时收不到_THINKPHP5微信模板消息推送跨坑记录。

https://blog..net/qd55815634/article/details/79043430

上面用的是CURL推送,推送少还可以,但是推送多了。就直接超时了。

获取所以的关注用户的函数(不懂的参数可以查看微信文档的说明):

function get_allopenid($access_token,$next_openid = ''){

$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid=".$next_openid;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);

$data = curl_exec($ch);

$data = json_decode($data,true);

if ($data["count"] == 10000){

$newdata = $this->get_allopenid($access_token,$data["next_openid"]);

$data["data"]["openid"] = array_merge_recursive($date["data"]["openid"], $newdata["data"]["openid"]);

}

return $data;

}

模版信息组装函数:

function json_tempalte($openid,$data,$template_id = 'DtlkLjhNf5p5P7kYorbm-6kop3zlSyct1wL70fuJ8ME'){

$template=array(

'touser'=>$openid,//openID

'template_id'=>$template_id,//模版id

'url'=>$data['url'],

'topcolor'=>"#7B68EE",

'data'=>array(

'first'=>array('value'=>$data['first'],'color'=>"#000"),

'keyword1'=>array('value'=>$data['keyword1'],'color'=>'#F70997'),

'keyword2'=>array('value'=>$data['keyword2'],'color'=>'#248d24'),

'keyword3'=>array('value'=>date("Y-m-d H:i:s"),'color'=>'#000'),

'remark' =>array('value'=>$data['remark'],'color'=>'#1784e8'), )

);//各个参数不明白的就去看文档,很详细。

return $template;

}

主要部分(写在你的访问方法里面的):

$data = input('post.');//前端传过来的值,一般是模版消息的各个内容。

$value = db('wx_config')->where([ 'key' => 'SHOPWCHAT'])->value('value');

$value = json_decode($value,true);

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$value['appid']."&secret=".$value['appsecret'];

if (cookie('access_token')){

$access_token=cookie('access_token');

}else{

$cont = json_decode($this->getToken($url));

$access_token=$cont->access_token;

setcookie('access_token',$access_token,7200);

}//缓存assesstoken

$all = $this->get_allopenid($access_token);

$count = $all['total'];

$openids = $all['data']['openid'];

foreach ($openids as $key => $value) {

if (db('users')->where('openid',$value)->value('unsub') == '1'){

unset($openids[$key]);

}

}

$openids = array_values($openids);//这部分主要是拒绝推送的用户匹配一下重组数据。

foreach ($openids as $key => $value) {//循环推送,一条一条的。我这里500条左右花了 21秒。

$params = json_encode($this->json_tempalte($value,$data),JSON_UNESCAPED_UNICODE);

$fp = fsockopen('api.weixin.qq.com', 80, $error, $errstr, 1);

$http = "POST /cgi-bin/message/template/send?access_token={$access_token} HTTP/1.1\r\nHost: api.weixin.qq.com\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";

fwrite($fp, $http);

fclose($fp);

}

return json(['code' => 1, 'msg' => '推送完成!', 'url' => url('pnotive/send')]);

以上。

异步推送请看下一篇。后来学会的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值