zoukankan      html  css  js  c++  java
  • 微信群发图文消息

    官方文档

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

     

  • 相关阅读:
    Add Two Numbers
    Reverse Linked List II
    Reverse Linked List
    Remove Duplicates from Sorted List
    Remove Duplicates from Sorted List II
    Partition List
    Intersection of Two Linked Lists
    4Sum
    3Sum
    2Sum
  • 原文地址:https://www.cnblogs.com/eeds-wangwei/p/8203382.html
Copyright © 2011-2022 走看看