PHP利用模板訊息無限制向使用者推送訊息

來源:互聯網
上載者:User

標籤: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利用模板訊息無限制向使用者推送訊息

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.