微信群发图文消息

官方文档

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

2、上传图文消息素材

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

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

执行上传图文消息,这里的 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

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);   }

成功返回

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了! 实际都是根据文档去实现,所有步骤大同小异,如有疑问,请留言!

原文地址:https://www.cnblogs.com/eeds-wangwei/p/8203382.html

时间: 2025-01-18 05:11:49

微信群发图文消息的相关文章

微信接口图文消息群发,预览

上来就直接贴代码了,具体问题放到最后说,这个类我将方法全都封装在里面了,基本上拿去修改一下就可以用了 using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text.RegularExpressions;using Newtonsoft.Json;using System.Web.SessionState;using Website_CS;using mgtArt

微信回复图文消息

一级POJO只能包含一级子元素,如果是多级子元素,就包含多级对象就可以了: @XmlRootElement(name="xml") @XmlAccessorType(XmlAccessType.FIELD) public class ScanUrl { private Integer id; @XmlElement(name="ToUserName") private String toUserName; @XmlElement(name="FromUse

[实例]JAVA调用微信接口发送图文消息,不用跳到详情页

package com.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.junit.Test; import net.sf.json.JSONArray;

java微信接口之五—消息分组群发

一.微信消息分组群发接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN   其中ACCESS_TOKEN是我们动态获取的.   发送的数据:(这里使用图文消息示例) { "filter":{ "group_id":"2" }, "mpnews":{ "me

微信公众平台开发(97) 图文消息

关键字:微信公众平台 开发模式 图文消息作者:方倍工作室原文:http://www.cnblogs.com/txw1958/p/weixin-97-news.html 在这篇微信公众平台开发教程中,我们将介绍如何灵活的使用图文消息功能.我们将只介绍被动回复消息中的图文消息,发送客服消息及高级群发消息接口的发送的图文消息与本文介绍的图文消息的各情况基本一致. 本文分为以下四个部分: 图文消息的定义 图文消息的实现 图文消息的类型 图文消息的回复 一.图文消息的定义 在微信公众平台消息中,发送被动响

后悔药来了?发出去的微信图文消息删除后不再显示

之前微信公众平台上群发的图文消息一旦发出去后就不能再改了,假使发送成功后发现有错别字你马上到后台修改了,订阅用户看到的还是那个错别字,即使改了也没用,ytkah就吃过这个亏.现在公众平台貌似有更新了,发出去的微信图文消息你删除后不再显示,提示“该内容已被发布者删除”. 后悔药来了?微信公众平台图文消息群发成功后删除订阅用户看不到内容.修改文字和段落是否成功还没测试,如果哪位公众号运营者已完成过的,欢迎发送截图给我们哈,不胜感激!

微信公众号 发送图文消息

介绍被动回复消息中的图文消息,发送客服消息及高级群发消息接口的发送的图文消息与本文介绍的图文消息的各情况基本一致. 本文分为以下四个部分: 图文消息的定义 图文消息的实现 图文消息的类型 图文消息的回复 一.图文消息的定义 在微信公众平台消息中,发送被动响应消息中的图文消息的XML结构如下所示. <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[

C# 调用微信接口上传素材和发送图文消息

using Common;using Newtonsoft.Json.Linq;using System;using System.IO;using System.Net;using System.Text; /// <summary> /// 调用微信接口凭证access_token /// </summary> private static string test_access_token { get { return "XXXXXXXXXXXX"; } }

asp实现微信客服消息群发,asp代码写的

最近一个客户的需求找到我,他想利用公众号给他的粉丝群发客服消息,想发带图文的客服消息,或是带链接的消息,花几分钟研究了一下,比较简单,上代码: 微信客服消息发送asp代码如下: <% url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="&Application("wx_token")(0) body="{""touser"