微信开发之群发接口

群发视频和图文素材时需要临时上传视频和图文素材,所以有多一步。

上传临时视频或图片

public function up_temporary_media($type, $img_path, $data)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->access_token."&type=".$type;
		/*$file_info=array(
		    'filename'=>'/images/1.png',  //国片相对于网站根目录的路径
		    'content-type'=>'image/png',  //文件类型
		    'filelength'=>'11011'         //图文大小
		);*/
        if($data['filename'] != "")
        {
        	$curlPost['media'] =  '@' . $img_path;
        	$curlPost['form-data'] = $data;
        	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
        	if(isset($res['media_id']))
        	{
        		return $res;
        	}else
        	{
        		return "";
        	}
        }else
        {
        	return "";
        } 
	}
发送视频是获取新的视频media_id

public function up_mpvideo($data)
    {
    	$url = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=".$this->access_token;
    	$curlPost['media_id'] = $data['media_id'];
    	$curlPost['title'] = urlencode($data['title']);
    	$curlPost['description'] = urlencode($data['description']);

    	$curlPost = urldecode(json_encode($curlPost));
    	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
    	if(isset($res['media_id']))
    	{
    		return $res;
    	}else
    	{
    		return "";
    	}
    }


上传临时图文

public function up_mpnews($articles)
    {
    	$url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=".$this->access_token;
    	foreach ($articles as $key => $val) {
    		$articles[$key]['title'] = urlencode($val['title']);
    		$articles[$key]['author'] = urlencode($val['author']);
    		$articles[$key]['digest'] = urlencode($val['digest']);
    		$articles[$key]['content'] = urlencode($val['content']);
    	}
    	$curlPost['articles'] = $articles;
    	$curlPost = urldecode(json_encode($curlPost));
    	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
    	if(isset($res['media_id']))
    	{
    		return $res;
    	}else
    	{
    		return "";
    	}
    }

上传永久图文或语音

public function up_media_towx($type, $img_path, $data)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->access_token."&type=".$type;
		/*$file_info=array(
		    'filename'=>'/images/1.png',  //国片相对于网站根目录的路径
		    'content-type'=>'image/png',  //文件类型
		    'filelength'=>'11011'         //图文大小
		);*/
        if($data['filename'] != "")
        {
        	$curlPost['media'] =  '@' . $img_path;
        	$curlPost['form-data'] = $data;
        	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
        	if(isset($res['media_id']))
        	{
        		return $res;
        	}else
        	{
        		return "";
        	}
        }else
        {
        	return "";
        } 
	}

群发信息

public function send_mass_msg($type, $tag_id, $data)
    {
    	$url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=".$this->access_token;
    	$curlPost['filter']['is_to_all'] = false;
    	$curlPost['filter']['tag_id'] = $tag_id;
    	$curlPost['msgtype'] = $type;

    	if($type == 'text')
    	{
    		$curlPost['text']['content'] = urlencode($data['content']);
    	}elseif($type == 'image')
    	{
    		$curlPost['image']['media_id'] = $data['media_id'];
    	}
    	elseif($type == 'voice')
    	{
    		$curlPost['voice']['media_id'] = $data['media_id'];
    	}
    	elseif($type == 'video')
    	{
    		$curlPost['msgtype'] = "mpvideo"; 
    		$curlPost['mpvideo']['media_id'] = $data['media_id'];
    	}
    	elseif($type == 'news')
    	{
    		$curlPost['msgtype'] = "mpnews"; 
    		$curlPost['mpnews']['media_id'] = $data['media_id'];
    	}
    	$curlPost = urldecode(json_encode($curlPost));
    	
    	$res = json_decode($this->helper->httpPost($curlPost, $url), true);

    	if($res['errcode'] == 0)
    	{
    		return $res;
    	}else
    	{
    		return "";
    	}
    }




$tag_id = $_POST['tag_id'];
    if($tag_id == 0)
    {
        sys_msg('请选择标签');
    }

    $content_type = $_POST['content_type'];
    $authorizer_access_token = get_token();
    $wxClassLib = new wxClassLib($authorizer_access_token);
    $upres = "";
    if($content_type == "image")
    {
        $media_id = intval($_POST['media_id']);
        $content = '';
        $wxmedia_id = get_wxmedia_id($media_id);
        $data['media_id'] = $wxmedia_id;
        $upres = $wxClassLib->send_mass_msg('image', $tag_id, $data);
    }elseif($content_type == "voice")
    {
        $media_id = intval($_POST['media_id']);
        $content = '';
        $wxmedia_id = get_wxmedia_id($media_id);
        $data['media_id'] = $wxmedia_id;
        $upres = $wxClassLib->send_mass_msg('voice', $tag_id, $data);
    }elseif($content_type == "video")
    {
        $media_id = intval($_POST['media_id']);
        $content = '';
        $video = $GLOBALS['db']->getRow("SELECT title, digest, file, size, file_name, file_type  FROM ".$GLOBALS['ecs']->table('')." WHERE id=".$media_id);
        //上传临时素材获取media_id
        $temporary_data['filename'] = $video['file_name'];
        $temporary_data['content-type'] = $video['file_type'];
        $temporary_data['filelength'] = $video['size'];
        $video_path = ROOT_PATH . $video['file'];
        $temporary_media_id = $wxClassLib->up_temporary_media("video", $video_path, $temporary_data);
        //获取特殊media_id
        if($temporary_media_id != "")
        {
            $mpvideo['media_id'] = $temporary_media_id['media_id'];
            $mpvideo['title'] = $video['title'];
            $mpvideo['description'] = $video['digest'];
            $mpmedia_id = $wxClassLib->up_mpvideo($mpvideo);
            if($mpmedia_id != "")
            {
                $content = $mpmedia_id['media_id'];
                $mpdata['media_id'] = $mpmedia_id['media_id'];
                $upres = $wxClassLib->send_mass_msg('video', $tag_id, $mpdata);
            }    
        }
          
    }
    elseif($content_type == "news")
    {
        $media_id = intval($_POST['media_id']);
        $content = '';
        $sql = "SELECT * FROM ".$ecs->table('wechat_auther_media')." WHERE id=".$media_id;
        $article = $db->getRow($sql);
        //上传图文消息封面图片
        $articles_arr = array();
        if($article['size'] < 2048000 )
        {                   
            $updata['filename'] = $article['file_name'];
            $updata['content-type'] = $article['file_type'];
            $updata['filelength'] = $article['size'];
            $real_file = ROOT_PATH . $article['file'];
            $mpnews = $wxClassLib->up_temporary_media('image', $real_file, $updata);
            if($mpnews != "")
            {
                $articles_arr[0]['thumb_media_id'] = $mpnews['media_id'];
                $articles_arr[0]['title'] = $article['title'];
                $articles_arr[0]['digest'] = $article['digest'];
                $articles_arr[0]['author'] = $article['author'];
                $articles_arr[0]['content_source_url'] = empty($article['link']) ? "http://www.qq.com/mobile/index.php?m=default&c=article&a=info&aid=".$article['id'] : $article['link'];
                $articles_arr[0]['content'] = "test";
                $articles_arr[0]['show_cover_pic'] = $article['is_show'];
            }
        }else
        {
            sys_msg("图片过大,请上传2m以内图片");
        }

        if($article['article_id'])
        {
            $article_id_arr = explode(",", $article['article_id']);
            $k = 1;
            foreach ($article_id_arr as $v) {
                if($v)
                {
                    $sql = "SELECT * FROM ".$ecs->table('wechat_auther_media')." WHERE id=".$v;
                    $other_article = $db->getRow($sql);
                    if($other_article['size'] < 2048000 )
                    {
                        $updata['filename'] = $other_article['file_name'];
                        $updata['content-type'] = $other_article['file_type'];
                        $updata['filelength'] = $other_article['size'];
                        $real_file = ROOT_PATH . $other_article['file'];
                        $mpnews = $wxClassLib->up_temporary_media('image', $real_file, $updata);
                        if($mpnews != "")
                        {
                            $articles_arr[$k]['thumb_media_id'] = $mpnews['media_id'];
                            $articles_arr[$k]['title'] = $other_article['title'];
                            $articles_arr[$k]['digest'] = $other_article['digest'];
                            $articles_arr[$k]['author'] = $other_article['author'];
                            $articles_arr[$k]['content_source_url'] = empty($other_article['link']) ? "http://www.qq.com/mobile/index.php?m=default&c=article&a=info&aid=".$other_article['id'] : $other_article['link'];
                            $articles_arr[$k]['content'] = "";
                            $articles_arr[$k]['show_cover_pic'] = $other_article['is_show'];
                            $k++;
                        }

                    }else
                    {
                        sys_msg("图片过大,请上传2m以内图片");
                    }
                }
                
            }
        }
        $mpmedia_id = $wxClassLib->up_mpnews($articles_arr);
        if($mpmedia_id != "")
        {
            $content = $mpmedia_id['media_id'];
            $mpdata['media_id'] = $mpmedia_id['media_id'];
            $upres = $wxClassLib->send_mass_msg('news', $tag_id, $mpdata);
        }
    }else
    {
        $media_id = 0;
        $wxmedia_id = "";
        $content = $_POST['content'];
        $data['content'] = $content;
        $upres = $wxClassLib->send_mass_msg('text', $tag_id, $data);
    }


发送图文信息时如果图文详情里有图片,还需要获取图片url,以为微信图文信息只认微信内部的图片地址,微信文档说道:

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

请注意,本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下。

接口调用请求说明

http请求方式: POST
https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN
调用示例(使用curl命令,用FORM表单方式上传一个图片):
curl -F media=@test.jpg "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN"

参数说明

参数 是否必须 说明
access_token调用接口凭证
mediaform-data中媒体文件标识,有filename、filelength、content-type等信息

返回说明 正常情况下的返回结果为:

{
    "url":  "http://mmbiz.qpic.cn/mmbiz/gLO17UPS6FS2xsypf378iaNhWacZ1G1UplZYWEYfwvuU6Ont96b1roYs CNFwaRrSaKTPCUdBK9DgEHicsKwWCBRQ/0"
}

其中url就是上传图片的URL,可用于后续群发中,放置到图文消息中。

这里再提一下,微信文档中

视频

请注意,此处视频的media_id需通过POST请求到下述接口特别地得到:https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=ACCESS_TOKEN POST数据如下(此处media_id需通过基础支持中的上传下载多媒体文件来得到):

{
  "media_id": "rF4UdIMfYK3efUfyoddYRMU50zMiRmmt_l0kszupYh_SzrcW5Gaheq05p_lHuOTQ",
  "title": "TITLE",
  "description": "Description"
}

返回将为

{
  "type":"video",
  "media_id":"IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc",
  "created_at":1398848981
}
https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=ACCESS_TOKEN  POST需要去掉前面的file.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值