微信模板訊息 群發 突破 4條限制
阿新 • • 發佈:2019-01-03
微訊號分為服務號和訂閱號(還有企業號內部用),服務號像qq,對方給你發信息,能馬上看到,訂閱號像郵件,你必須自己開啟對方的訂閱號才能看到即時的資訊。所以服務號更像一個普通微信好友,可以線上溝通。但是微信為了控制廣告效果,服務號每月只能發4次訊息,而訂閱號卻可以每天一次訊息。雖然公眾號也可以用高階群發介面程式設計傳送,但是更厲害的是微信從客戶端限制每使用者只能接收4次公眾號的訊息。所以如果只編程傳送,超過4次,使用者根本不知道有這訊息,微信伺服器就給你攔掉了。還好,微信為企業服務留了個口子,圖文訊息不讓發,但是簡單文字訊息可以發,突破公眾號限制,每天傳送一條訊息的方法就走的這個介面。微信監測這個介面,每日限額10000次的呼叫,我們只有小几千個關注者,夠用了。
儲存成php檔案,設定好<?php $appid = "wx"; $appsecret = "0b97ad645797ee2"; $times= date('y年m月d日 H:i:s',time()); echo $times."</br>"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;//取得token $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $jsoninfo = json_decode($output, true); $access_token = $jsoninfo["access_token"]; // echo $access_token."</br>"; $begin=0; if($begin>=10000) {$url= "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token."&next_openid=".$_GET['next_openid'];} else{$url= "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $array = json_decode($output, true); echo $array["count"]."</br>"; // foreach ($array["data"]["openid"] as $key=>&$value) {//獲取使用者的唯一標識 // echo ($key)."->".$value."</br>" ; // } //群發註釋 $times= date('y年m月d日 H:i:s',time()); $template = array('touser' =>"oC282t60QEDt0El-hcVOShgzgTmQ",//&$value, // // 'template_id' => "gJ_qahsRIMxRtxeWtKuhChzfA6w8HHdZE2xa7_XQ5u8", // 'url' =>"http://eqxiu.com/s/7pWq", 'url' => "http://mp.weixin.qq.com/", 'topcolor' => "#7B68EE", 'data' => array('first' => array('value' => urlencode("新聞標題:"), 'color' => "#743A3A", ), 'keyword1' => array('value' => urlencode("XXXXXX"), 'color' => "#FF0000", ), 'keyword2' => array('value' => urlencode("全體關心XXX"), 'color' => "#C4C400", ), 'keyword3' => array('value' => urlencode($times), 'color' => "#0000FF", ), 'keyword4' => array('value' => urlencode("工作先進集體"), 'color' => "#FF0000", ), 'remark' => array('value' => urlencode("\\n你好。點選檢視“詳情”!"), 'color' => "#008000", ), ) ); // var_dump($template)."</br>"; // echo urldecode(json_encode($template))."</br>"; $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token; $result = https_request($url, urldecode(json_encode($template)));//"";//群發命令 // var_dump($result)."</br>"; // } //群發關閉 function https_request($url,$data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); // echo "</br>"; // echo $output; return $output; }
$appid = "wx";
$appsecret = "0b97ad645797ee2";
改掉幾處註釋,即可以使用,最好先發到自己號上測試一下