1. 程式人生 > >微信公眾號開發之群發消息

微信公眾號開發之群發消息

dia inf 詳細 media ans urn xxx pat 出了

圖文群發消息流程

1、上傳圖片縮略圖獲取media_id

2、上傳圖文素材獲取圖文素材的media_id

3、群發消息

這裏給出了圖文群發的預覽接口和群發接口

//圖文群發
public function tuwenqunfa(){
        $accessTokenInfo = file_get_contents("access_token.log");
    if($accessTokenInfo){
    $tokenArr = json_decode($accessTokenInfo,true);
    //var_dump($tokenArr);
    if
((time() + $tokenArr[‘expires_in‘] -200) >time()){//保證不過期,減去200秒,做緩沖 $access_token = $tokenArr[‘access_token‘]; $url="https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=".$access_token; $media_id=$this->getmedia_id(); //var_dump($media_id); $data=‘{ "articles": [ { "thumb_media_id":"
‘.$media_id.‘", "author":"李強", "title":"圖文消息1", "content_source_url":"www.baidu.com", "content":"這是內容吧", "digest":"digest", "show_cover_pic":1, "need_open_comment":1, "only_fans_can_comment":1 }, { "thumb_media_id":"
‘.$media_id.‘", "author":"xxx", "title":"圖文消息2", "content_source_url":"www.qq.com", "content":"這是內容", "digest":"digest", "show_cover_pic":0, "need_open_comment":1, "only_fans_can_comment":1 } ] }; //上傳圖文素材 $res=$this->http_request($url,$data); //var_dump($res); $res=json_decode($res); //預覽接口 $url2 = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=".$access_token; //群發接口 //$url2="https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=".$access_token; $sucai_id=$res->media_id; //預覽發布 $data2=‘{ "touser":"用戶openid", "mpnews":{ "media_id":"‘.$sucai_id.‘" }, "msgtype":"mpnews" }; //群發圖文素材 // $data2=‘{ // "filter":{ // "is_to_all":true // }, // "mpnews":{ // "media_id":"‘.$sucai_id.‘" // }, // "msgtype":"mpnews" // }‘; $res2=$this->http_request($url2,$data2); var_dump($res2); } }else{ //重新獲取accesstoken $this->getaccesstoken(); } } //上傳圖片縮略圖獲取media_id public function getmedia_id(){ $accessTokenInfo = file_get_contents("access_token.log"); if($accessTokenInfo){ $tokenArr = json_decode($accessTokenInfo,true); //var_dump($tokenArr); if((time() + $tokenArr[‘expires_in‘] -200) >time()){//保證不過期,減去200秒,做緩沖 $access_token = $tokenArr[‘access_token‘]; $url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$access_token."&type=thumb"; $file_path = dirname(__FILE__)."/1.jpg"; //var_dump($file_path); //這裏聲明文件的路徑,使用絕對路徑 $file_data = array(‘media‘ => ‘@‘.$file_path); $res=$this->http_request($url,$file_data); $res=json_decode($res); //var_dump($res); return $res->media_id; } }else{ //重新獲取accesstoken $this->getaccesstoken(); } }

純文本的比較簡單

//文本群發功能
public function wenbenqunfa(){
    $accessTokenInfo = file_get_contents("access_token.log");
    if($accessTokenInfo){
    $tokenArr = json_decode($accessTokenInfo,true);
    //var_dump($tokenArr);
    if((time() + $tokenArr[‘expires_in‘] -200) >time()){//保證不過期,減去200秒,做緩沖
        $access_token = $tokenArr[‘access_token‘];
        $url="https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=".$access_token;
        $data=‘{
"filter":{
"is_to_all":true
},
"text":{
"content":"測試群發消息"
},
"msgtype":"text"
};

    $res=$this->http_request($url,$data);
    //var_dump($res);
    }
}else{
    //重新獲取accesstoken
    $this->getaccesstoken();
}
}

我這裏寫的比較簡單,可以參考下面這篇博客,寫的比較詳細

https://blog.csdn.net/qq_18976087/article/details/79061855

微信公眾號開發之群發消息