最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php實現(xiàn)微信公眾號無限群發(fā)

 更新時間:2015年10月11日 15:19:02   投稿:hebedich  
本文給大家分享的是php實現(xiàn)的利用微信的客服接口進行各類消息的無限群發(fā),思路非常巧妙,有需要的小伙伴可以參考下

利用微信客服接口進行各類消息的無限群發(fā)

sendAllMsg.php

<?php
  /*
    Author:yf
    使用說明:微信公眾號無限群發(fā)接口,使用實例:   
    $test = new SendAllMsg("你的appId","你的appSecret");
    $test->sendMsgToAll(); //調用群發(fā)方法
    注:1.使用條件:認證號或測試號
      2.群發(fā)消息內容可為圖文、文本、音樂等,$data具體內容參照微信開發(fā)文檔/客服接口
      3.若用戶量過萬,需修改getUserInfo(),具體參照信開發(fā)文檔/獲取關注者列表
       
    新手上路,大神們多多指點,謝謝
  */
  interface iSendAllMsg{
    function getData($url); //curl 發(fā)送get請求
    function postData($url,$data); //curl 發(fā)送post請求
    function getAccessToken();  //在構造方法中已調用該方法來獲取access_token,注意它在wx服務器的保存時間7200s
    function sendMsgToAll(); //群發(fā)消息方法,發(fā)送的消息$data 可自行修改
  }
  class SendAllMsg implements iSendAllMsg{
    private $appId; 
    private $appSecret;
    private $access_token;
    //
    public function __construct($appId, $appSecret) {
      $this->appId = $appId;
      $this->appSecret = $appSecret;
      $this->access_token = $this->getAccessToken();
    }
    //
    function getData($url){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
      curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;
    }
    //
    function postData($url,$data){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $tmpInfo = curl_exec($ch);
      if (curl_errno($ch)) {
        return curl_error($ch);
      }
      curl_close($ch);
      return $tmpInfo;
    }
    //
    function getAccessToken(){
      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
      $res = $this->getData($url);
      $jres = json_decode($res,true);
      $access_token = $jres['access_token'];
      return $access_token;
    }
    //
    private function getUserInfo(){
      $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token;
      $res = $this->getData($url);
      $jres = json_decode($res,true);
      //print_r($jres);
      $userInfoList = $jres['data']['openid'];
      return $userInfoList;
    }
    function sendMsgToAll(){
      $userInfoList = $this->getUserInfo();
      $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
      foreach($userInfoList as $val){
        $data = '{
              "touser":"'.$val.'",
              "msgtype":"text",
              "text":
              {
                "content":"測試一下,抱歉打擾各位"
              }
            }';
        $this->postData($url,$data);
      }
    }
  }
  $test = new SendAllMsg("YOURappId","YOURappSecret");
  $test->sendMsgToall();
   
?>

以上就是本文的全部內容了,希望大家能夠喜歡。

相關文章

最新評論

潞西市| 东山县| 邮箱| 西盟| 邵阳市| 长兴县| 南投市| 浮梁县| 安岳县| 互助| 汉寿县| 淮北市| 太保市| 洛浦县| 临清市| 桐梓县| 城市| 江源县| 广宗县| 怀仁县| 海阳市| 合江县| 双流县| 德兴市| 聊城市| 克东县| 营口市| 申扎县| 亳州市| 巴马| 岑溪市| 新蔡县| 商丘市| 梁山县| 辽源市| 西宁市| 阳新县| 嘉黎县| 任丘市| 大渡口区| 扎鲁特旗|