图文群发消息流程
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(); } }
我这里写的比较简单,可以参考下面这篇博客,写的比较详细