新增媒体素材和群发
首先上传素材(媒体素材)到微信服务器
public function uploadMedia($type="image"){
$access_token = $this->get_access_token();
//$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=$access_token";//上传微信推文内容的图片返回url
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token&type=$type";//普通素材(会过期)要跟普通图文匹配
//$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=$type";//永久素材(有容量限制,5000张)要跟永久图文匹配
$filename = $_SERVER['DOCUMENT_ROOT'].'/Public/img/lo.jpg';//要上传的图片素材
if( !file_exists($filename) ) {
die('图片不存在');
}
$data=array("media"=>'@'.$filename);
$res=$this->http_curl( $url ,'post', 'json', $data);//上传
dump($res);
} 接着新增图文素材(就说群发时候的图文列表,一组一组的),这里的thumb_media_id是上一步返回得到,可选择普通图文和永久图文,链接可以写外链,也可以用微信后台的文章链接
public function uploadTw(){
$access_token = $this->get_access_token();
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=$access_token";//普通素材图文
//$url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=$access_token";//永久素材图文
$array = array(
"articles" => array(
array(
"thumb_media_id" => "CUrNAWWIjhSBPD3E6YonGLo-SS1NyjJj9qEv3C3sfaM",
"author"=>"2016-12-04",
"title"=>"详情",
"content_source_url"=>"http://www.guitutu.com",
"content"=>"这里是页面的内容",
"digest"=>"描述信息",
"show_cover_pic"=>1
),
array(
"thumb_media_id" => "CUrNAWWIjhSBPD3E6YonGLo-SS1NyjJj9qEv3C3sfaM",
"author"=>"测试测试",
"title"=>"abcd",
"content_source_url"=>"http://www.guitutu.com",
"content"=>"这里是页面的内容2",
"digest"=>"描述信息2",
"show_cover_pic"=>1
),
)
);
$postJson = json_encode($array);
$res = $this->http_curl($url,'post','json',$postJson);
dump($res);
} 可以这样来查看后台的历史发文还能取得历史文章的内容和链接,方便调用
public function countmedel($start=0,$count=20){
$access_token = $this->get_access_token();
$url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=".$access_token;
$array = array(
"type" => "news",
"offset" => $start,
"count" => $count
);
$postJson = json_encode($array);
$res = $this->http_curl($url,'post','json',$postJson);
dump($res);
} 素材添加完毕,接下来可以群发消息了(为了不打扰别人,在测试的时候最好设置一下只发给自己)
public function sendMsgAll(){
//1、获取全局access_token
$access_token=$this->get_access_token();
$url = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token='.$access_token;
//2、组装群发接口
//单文本
// $array = array(
// 'touser'=>'oY1k9wWRtQgMk1z_snM2v3N4rykc',//接收消息用户对应该公众号的openid
// 'text' => array('content'=>'hello'),
// 'msgtype'=>'text'//消息类型
// );
//单图文
$array = array(
'touser'=>'oY1k9wWRtQgMk1z_snM2v3N4rykc',//接收消息用户对应该公众号的openid,这里方便测试用,群发给自己
'mpnews' => array('media_id'=>'CUrNAWWIjhSBPD3E6YonGITDQZmaCSENmkR8NzYBs6g'),//图文素材返回的id
'msgtype'=>'mpnews'//消息类型
);
//3、将数组转成json
$postJson = json_encode($array);
//4、调用curl
$res = $this->http_curl($url,'post','json',$postJson);
dump($res);
} 如果上面步骤都没问题的话,现在就已经成功群发了一条消息
然后发送模板消息,这个要去微信后台申请模板,通过后需要拿到模板ID,对应好模板的格式(这里我用自己来测试)
public function sendTemplMsg(){
$access_token = $this->get_access_token();
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
//组装数组
$array = array(
"touser"=>"oY1k9wWRtQgMk1z_snM2v3N4rykc",
"template_id"=>"pY569gYGkV38n-eV3H5-I1NMoeqb-5veeZf4RRGCKF0",
"url"=>"http://www.guitutu.com",
"data"=>array(
"first"=>array(
"value"=>"尊敬的小简,您尾号为1234的资金账户交易信息。",
"color"=>"#173177"
),
"stock_Code"=>array(
"value"=>"11654811",
"color"=>"#173177"
),
"stock_Name"=>array(
"value"=>"中国钢铁",
"color"=>"#173177"
),
"direction"=>array(
"value"=>"买入",
"color"=>"#173177"
),
"vol"=>array(
"value"=>"1000股",
"color"=>"#173177"
),
"amount"=>array(
"value"=>"12000元",
"color"=>"#173177"
),
"time"=>array(
"value"=>time('Y-m-d H:i:s'),
"color"=>"#173177"
),
"remark"=>array(
"value"=>"如有疑问,请拨打海通证券客服热线95553。",
"color"=>"#173177"
),
)
);
$postJson = json_encode($array);
$res = $this->http_curl($url,'post','json',$postJson);
dump($res);
}