標籤:function www. 推送 etop api each sage ESS get
<?php //擷取access_token function getaccess_token(){ //appid與appsecret改成你自己的 $appid = ‘自己的appid‘; $appsecret = ‘自己的appsecret‘; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}"; $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); curl_close($ch); $data = json_decode($data,true); return $data[‘access_token‘]; } //設定與發送模板資訊 function set_msg($openid){ //擷取access_token $access_token = getaccess_token(); //這裡是在模板裡修改相應的變數 $formwork = ‘{ "touser":"‘.$openid.‘", "template_id":"oasLSlzdPXF-4U21JRE0lFYLsZWVVFcxY20SC6EAxx4", "url":"http://www.wangwenxiao.com", "data":{ "title": { "value":"這裡是自己定義的標題", "color":"#173177" }, "content":{ "value":"這裡是自訂內容", "color":"#173177" }, "time": { "value":"這裡填寫時間", "color":"#173177" } } }‘; $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork); $data = curl_exec($ch); curl_close($ch); return $data;}function sendall(){ //擷取access_token $access_token = getaccess_token(); $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&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); return $data[‘data‘][‘openid‘];}//‘{"total":3,"count":3,"data":{"openid":["oC8xks2kb67MogD8ubzkViHf88x4","oC8xksxbsJMf1FKpr4YEfGsqKHtg","oC8xks4COCfqox1-dXTCsZF_1vIo"]},"next_openid":"oC8xks4COCfqox1-dXTCsZF_1vIo"}‘// array(3) {// [0]=>// string(28) "oC8xks2kb67MogD8ubzkViHf88x4"// [1]=>// string(28) "oC8xksxbsJMf1FKpr4YEfGsqKHtg"// [2]=>// string(28) "oC8xks4COCfqox1-dXTCsZF_1vIo"// }$all_openid = sendall();//把上面設定的資訊迴圈發送到所有的公眾號關注的使用者手裡foreach ($all_openid as $value) { set_msg($value);}echo ‘執行完畢‘;
PHP利用模板訊息無限制向使用者推送訊息