最近公众号的消息推送,由编辑模式过渡到了开发模式,把要推送模板消息的openid存入MySQL,尝试了两种群发模板消息的方法,其中有一种效率属实给力,大家可以看一下
/**
* 发送模板消息 方案一
*/
public function actionTempletsend()
{
$TOKEN=$this->actionToken();
$sql = "select openid from wechat_tempsend";
$data = Yii::app()->db_com->createCommand($sql)->queryAll();
$counts = count($data);
$num = 0;
$param = '';
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$TOKEN";
$tmp ='"template_id":"1QJ18wBgrJAYAWjRkw5pdfcOQEZFFTmyUFaE-uOj8hI",
"url":"https://mp.weixin.qq.com/s?__biz=MzI5MTA1MzgyNA==&mid=500823476&idx=1&sn=4f54b950cca434ab26ccb8e168ec8e21&chksm=743bdddb434c54cdffa35c7c1c619f96cecb7aca9bc1d90650248aa64e77bb52bd3ca39079d6&mpshare=1&scene=1&srcid=0115atv3RrJlzFwgVJM7SGbf#rd",
"data":{
"first": {
"value":"今日指数估值数据已更新!",
"color":"#173177"
},
"keyword1":{
"value":"赚钱数据公布",
"color":"#173177"
},
"keyword2": {
"value":"每周一至周五更新",
"color":"#173177"
},
"remark":{
"value":"点击立即查看",
"color":"#173177"
}
}
}';
$start_time = microtime(true);
foreach ($data as $key => $value) {
$param ='{"touser":"'.$value['openid'].'",';
$param =$param.$tmp;
$res = $this->http_post($url,$param);
$num = $num +1;
// var_dump($res);die;
}
if ($num == $counts) {
echo "";
}
print_r(microtime(true) - $start_time);
}
/**
* 发送模板消息 方案二
*/
public function actionTemplets()
{
// $scene = Yii::app()->request->getParam("scene");
$access_token = $this->actionToken();
$scene = 'test';
//发送模板消息的对象
$sql = "select openid from wechat_tempsend1 where scene = '$scene'";
$data = Yii::app()->db_com->createCommand($sql)->queryAll();
//模板消息参数
$tempsql = " select * from wechat_moban1 where scene = '$scene'";
$temp = Yii::app()->db_com->createCommand($tempsql)->queryRow();
$temp = json_decode($temp['message'],true);
$counts = count($data);
$num = 0;
$param = '';
// $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$TOKEN";
// $tmp ='"template_id":"1QJ18wBgrJAYAWjRkw5pdfcOQEZFFTmyUFaE-uOj8hI",
$tmp ='"template_id":"1ZM5h7riM42a4-ScHOt3v0Vl0uYjQrm0AOuDiBMFmUY",
"url":"'.$temp['url'].'",
"data":{
"first": {
"value":"'.$temp['first'].'",
"color":"#173177"
},
"keyword1":{
"value":"'.$temp['keyword1'].'",
"color":"#173177"
},
"keyword2": {
"value":"'.$temp['keyword2'].'",
"color":"#173177"
},
"remark":{
"value":"'.$temp['remark'].'",
"color":"#173177"
}
}
}';
$start_time = microtime(true);
foreach ($data as $key => $value) {
$param ='{"touser":"'.$value['openid'].'",';
$params =$param.$tmp;
$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);
$num = $num +1;
}
$losttime = microtime(true) - $start_time;
$losttime = substr($losttime,0,4);
$times = date('Y-m-d H:i:s');
$sql2 = " insert into wechat_log(losttime,scene,times) values ('$losttime','$scene','$times')";
Yii::app()->db_com->createCommand($sql2)->execute();
// echo $losttime;die;
if ($num == $counts) {
echo "";
}
// print_r(microtime(true) - $start_time);
}
第一种方式是利用curl模拟http请求,第二种方式是利用fsockopen,其中方案二速度最快,希望能帮到各位老铁