微信开发之获取素材列表并保存

既然自己开发,就需要获取到数据,并给出编号;

这个也很简单,根据api能很容易拿到数据,但是封面图片的地址我在写这个记录的时候还是没有拿到,不知道是我的方法不对,还是微信没有这个接口,因为发送图文信息需要一个图片地址链接;因为我要个文章编号,所以我需要一条一条获取,微信可以一次获取20条数据。

1、获取token;

2、获取文章数量

3、获取文章列表;


获取token

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
参数是否必须说明
grant_type获取access_token填写client_credential
appid第三方用户唯一凭证
secret第三方用户唯一凭证密钥,即appsecret

wchat.php


/**
	* 获取access_token
	*grant_type 获取access_token填写client_credential
	*appid 第三方用户唯一凭证
	*secret 第三方用户唯一凭证密钥,即appsecret
	*/
	public function get_access_token()
	{
		//https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".SECRET;
		$response = file_get_contents($url);
		//返回数据格式:{"access_token":"ACCESS_TOKEN","expires_in":7200}
		//expires_in:凭证有效时间
		$res = json_decode($response, true);
		return $res['access_token'];
	}
获取这个是get就可以,所以可以直接用file_get_contents


获取文章数量

wchat.php


https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=ACCESS_TOKEN
{
  "voice_count":COUNT,
  "video_count":COUNT,
  "image_count":COUNT,
  "news_count":COUNT
}

/*
	*获取文章数量
	*/
	public function get_article_count($access_token)
	{
		//https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=ACCESS_TOKEN
		$url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=".$access_token;
		$response = file_get_contents($url);
		$res = json_decode($response, true);
		return $res;
	}


获取文章列表

https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN
参数是否必须说明
type素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
offset从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
count返回素材的数量,取值在1到20之间
{
   "total_count": TOTAL_COUNT,
   "item_count": ITEM_COUNT,
   "item": [{
       "media_id": MEDIA_ID,
       "content": {
           "news_item": [{
               "title": TITLE,
               "thumb_media_id": THUMB_MEDIA_ID,
               "show_cover_pic": SHOW_COVER_PIC(0 / 1),
               "author": AUTHOR,
               "digest": DIGEST,
               "content": CONTENT,
               "url": URL,
               "content_source_url": CONTETN_SOURCE_URL
           },
           //多图文消息会在此处有多篇文章
           ]
        },
        "update_time": UPDATE_TIME
    },
    //可能有多个图文消息item结构
  ]
}

/*
	*获取文章列表
	*type 素材的类型:图片(image)、视频(video)、语音 (voice)、图文(news)
	*offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
	*count 返回素材的数量,取值在1到20之间
	*/
	public function get_article_list($type, $offset, $count, $access_token)
	{
		//https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN
	
		$url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=".$access_token;
        $data = '{"type":"'.$type.'","offset":"'.$offset.'","count":"'.$count.'"}';
		//返回的数据
		$response = $this->get_response_post($url, $data);
		//echo strip_tags($response);
		$res = json_decode($response, true);
		return $res;
	}

public function get_response_post($url, $data)
	{
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_HEADER, 0);//过滤头部
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回,而不是直接输出。
		curl_setopt($curl,CURLOPT_POST,true); // post传输数据
		curl_setopt($curl,CURLOPT_POSTFIELDS,$data);// post传输数据
		$responseText = curl_exec($curl);
		curl_close($curl);
	
	    return $responseText;
	}



插入数据


/*插入数据库*/
	public function import_news($data)
	{
		
		$sql = "INSERT INTO xxx (media_id,cat_id,keywords,news_sn,title,thumb_media_id,show_cover_pic,author,digest,content,url,content_source_url,update_time, msg_type)".
		       " VALUES('".$data['media_id']."', 0, '', '', '".$data['title']."', '".$data['thumb_media_id']."','".$data['show_cover_pic']."', '".$data['author']."','".$data['digest']."','".$data['content']."','".$data['url']."','".$data['content_source_url']."',".$data['update_time'].", 'news')";
		$res = $this->dbhc->qry($sql);
		$last_id = mysql_insert_id();
		$format_count = sprintf("%05u", $last_id);
		$sql = "UPDATE xxx SET news_sn='".$format_count."' WHERE id=".$last_id;
		$this->dbhc->qry($sql);
		return $res;
	}

因为编辑有时候可能忘了,有时候有记起来了,所以我弄了填写数量插入每天更新一条

if($act == "importone")
{
	
	$access_token = $hcwechat->get_access_token();
	$list = $hcwechat->get_article_list('news', 0, 1, $access_token);
    $res = import_opearte($list);
    foreach ($res as $key => $val) {
    	$count = $op->get_news_media_id($val['media_id']);

    	if($count<=0)
		{
			$op->import_news($val['list']);
		}
    }    
    header("Location:list.php");
}

elseif($act == "import")
{
	
	$num = intval($_POST['num']);
	$hcwechat = new hcWechat();
	$access_token = $hcwechat->get_access_token();
    $count = $hcwechat->get_article_count($access_token);
    $news_count = $count['news_count'];
    if($num>$news_count)
    {
    	$num = $news_count;
    }
    for ($i=1; $i <= $num; $i++) 
    { 
    	$start = $num-$i;
    	$list = $hcwechat->get_article_list('news', $start, 1, $access_token);
    	$res = import_opearte($list);
	    foreach ($res as $key => $val) {
	    	$count = $op->get_news_media_id($val['media_id']);
	    	if($count<=0)
			{
				$op->import_news($val['list']);
			}
	    }   
    }
    header("Location:list.php");
}

这里对返回回来的数据稍微处理下

function import_opearte($list)
{
	$res = array();
	foreach ($list['item'] as $key => $val) 
	{
		foreach ($val['content']['news_item'] as $k => $v) 
		{
			$data = array(
				'media_id'           => $val['media_id'],
				'update_time'        => $val['update_time'],
				'title'              => $v['title'],
				'thumb_media_id'     => $v['thumb_media_id'],
				'show_cover_pic'     => $v['show_cover_pic'],
				'author'             => $v['author'],
				'digest'             => $v['digest'],
				'content'            => $v['content'],
				'url'                => $v['url'],
				'content_source_url' => $v['content_source_url'],
				);
			$res[]['media_id'] = $val['media_id'];
			$res[]['list'] = $data;
		}
    }
	return $res;	
}

上面有个thumb_media_id,微信解释是图文消息的封面图片素材id(必须是永久mediaID),但是发送图文信息时需要的是图片的地址,目前就这个还不知道咋弄。有知道的请不吝赐教,谢谢!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值