微信開發之獲取素材列表並儲存
阿新 • • 發佈:2019-01-04
既然自己開發,就需要獲取到資料,並給出編號;
這個也很簡單,根據api能很容易拿到資料,但是封面圖片的地址我在寫這個記錄的時候還是沒有拿到,不知道是我的方法不對,還是微信沒有這個介面,因為傳送圖文資訊需要一個圖片地址連結;因為我要個文章編號,所以我需要一條一條獲取,微信可以一次獲取20條資料。
1、獲取token;
2、獲取文章數量
3、獲取文章列表;
獲取token
引數 | 是否必須 | 說明 |
---|---|---|
grant_type | 是 | 獲取access_token填寫client_credential |
appid | 是 | 第三方使用者唯一憑證 |
secret | 是 | 第三方使用者唯一憑證金鑰,即appsecret |
wchat.php
獲取這個是get就可以,所以可以直接用file_get_contents/** * 獲取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']; }
獲取文章數量
wchat.php
{ "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; }
獲取文章列表
引數 | 是否必須 | 說明 |
---|---|---|
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),但是傳送圖文資訊時需要的是圖片的地址,目前就這個還不知道咋弄。有知道的請不吝賜教,謝謝!