图文消息html,微信群发图文消息(示例代码)

官方文档

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1481187827_i0l21

根据文档要求

1 上传图文消息内的图片获取URL【订阅号与服务号认证后均可用】

2 上传图文消息素材【订阅号与服务号认证后均可用】

3 根据标签进行群发【订阅号与服务号认证后均可用】

4 根据OpenID列表群发【订阅号不可用,服务号认证后可用】

5 删除群发【订阅号与服务号认证后均可用】

6 预览接口【订阅号与服务号认证后均可用】

7 查询群发消息发送状态【订阅号与服务号认证后均可用】

8 事件推送群发结果

9 使用 clientmsgid 参数,避免重复推送

10 控制群发速度

1、上传图文消息内的图片获取URL

//上传图片 jpg 格式

public function test()

{

$accessToken = $this->getToken($this->appid, $this->appsecret);

$url="https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$accessToken;

$data = array("file" => "@" . dirname(__FILE__) . "\\a.jpg");

$result = $this->request_post($url, $data);

return json_decode($result,true);

}

//获取access_token

public function getToken($appid, $appsecret)

{

if (S($appid)) {

$access_token = S($appid);

} else {

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;

$json = file_get_contents($url);

$result = json_decode($json);

$access_token = $result->access_token;

S($appid, $access_token, 7200);

}

return $access_token;

}

//发送POST请求

function request_post($url = \'\', $param = \'\')

{

if (empty($url) || empty($param)) {

return false;

}

$postUrl = $url;

$curlPost = $param;

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($curl);

curl_close($curl);

return $data;

}

成功返回 URL

12bb5a98ec4a46cfa2f0b6f426b0a86a.jpg

2、上传图文消息素材

可以直接用微信的debug上传图片,返回的 图片media_id 直接用作图文消息  thumb_media_id(缩略图media_id)

https://mp.weixin.qq.com/debug

4b78ce29af2848f8b2ef5030286fb972.jpg

40a7e3ce19ff48e996848f1dbcc58da2.jpg

执行上传图文消息,这里的 thumb_media_id  就是上一步得到的图片 media_id

public function test(){

$accessToken = $this->getToken($this->appid, $this->appsecret);

$url="https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=".$accessToken;

$data=\'{

"articles": [

{

"thumb_media_id":"WfKJPCNZy8seDitzd8_LiWa_EUGy9nqaPwW0Gp4wWZV4Q6KyfmH7-KWx3QZ13BUR",

"author":"作者",

"title":"标题",

"content_source_url":"http://www.coolrong.com",

"content":"具体内容",

"digest":"简短简绍",

"show_cover_pic":1

},

{

"thumb_media_id":"WfKJPCNZy8seDitzd8_LiWa_EUGy9nqaPwW0Gp4wWZV4Q6KyfmH7-KWx3QZ13BUR",

"author":"xxx",

"title":"Happy Day",

"content_source_url":"www.baidu.com",

"content":"content",

"digest":"digest",

"show_cover_pic":0

}]

}\';

$result = $this->request_post($url, $data);

return json_decode($result,true);

}

返回结果,注意这个 media_id 是图文消息的 media_id

99fca12e58ea4d1a94808a8c52a7765a.jpg

3、因为测试群发次数有限,我们使用预览测试一下

注意:这里的 madia_id 就是上一步获取的 madia_id

public function test(){

$accessToken = $this->getToken($this->appid, $this->appsecret);

$url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=".$accessToken;

$data=\'{

"touser":"测试微信的 openID",

"mpnews":

{

"media_id":"VtbL-3wOv1_jn78u9S6VAyvkhbWjMznWuKap7hOCRNCgrtSfXApnWTLMpx......"

},

"msgtype":"mpnews"

}\';

$result = $this->request_post($url, $data);

return json_decode($result,true);

}

成功返回

95bdfba39f1d40a291ae443ab5d42e70.jpg

4、群发

注意:这里的 madia_id 就是与预览中的相同,tag_id为标签,给全部用户推送可以不填写

public function test(){

$accessToken = $this->getToken($this->appid, $this->appsecret);

$url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token==".$accessToken;

$data=\'{

"filter":{

"is_to_all":false,

"tag_id":2

},

"mpnews":{

"media_id":"VtbL-3wOv1_jn78u9S6VAyvkhbWjMznWuKap7hOCRNCgrtSfXApnWTLMpx......"

},

"msgtype":"mpnews",

"send_ignore_reprint":0

}\';

$result = $this->request_post($url, $data);

return json_decode($result,true);

}

这样,就OK了! 实际都是根据文档去实现,所有步骤大同小异,如有疑问,请留言!

08822be5f8f4459c9bd6841dc8e98dea.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值