php微信公眾號模版訊息傳送(群發)
阿新 • • 發佈:2018-11-25
public function sendall(){
$all_openid=$this->tosendall();
foreach($all_openid as $value){
$this->set_msg($value);
}
}
public function tosendall(){
$access_token=$this->getAccesstoken();
$url="https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid=" ;
$ch=curl_init($url);
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);
return $data['data']['openid'];
}
public function set_msg($openid){
$access_token = $this->getAccesstoken();
$template = array(
"touser" => "$openid",//使用者openid
"template_id" => "-dRb-6prGX6AO6Pt_uTirmH2fztkVHLFG7HHfISwjXg",//模板id
"url" => "",//跳轉連結
"data" => array(
'first' => array('value' => urlencode ("為更好的為您提供服務,原公眾號功能升級,現已升級完畢並更名為惠三秦,特此通知。")),
'keyword1' => array('value' => urlencode("公眾號功能")),
'keyword2' => array('value' => urlencode("全站業務")),
'keyword3' => array('value' => urlencode("2018年3月30日")),
'keyword4' => array('value' => urlencode("現已升級完畢")),
'remark' => array('value' => urlencode("點選這裡,更多優惠!更多驚喜都在等你來發現。")),
)
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
$json = $this->curl_post($url, urldecode($json_template));
$rs = json_decode($json, true);
if ($rs['errcode'] == 0) {echo 'ok';
} else {
echo "no";
print_r($rs);
}
}