新浪微博api讀取指定日期之後的微博

來源:互聯網
上載者:User

以user_timeline介面為例

先說一下我的思路

首先你得明白微博介面調用的本質

其實你下載的那個sdk只是又封裝了一下,但其核心就是類比瀏覽器發送post或者get請求擷取json資料,

$res = Http::request($url, $params, 'GET');//訪問api

這句話就是來用get方法調用api的

指定日期如2013/7/20

將其轉化成int 

intval(strtotime("2013/7/20"));

拿出每條微博的created_at將其轉化成int在與指定日期比

具體實現如下


public static function getWeiboByDate($access_token,$screen_name, $startDate){
$url="https://api.weibo.com/2/statuses/user_timeline.json";
$page=1;
$count=100;
$continue=true;

$j=0;
$i=0;
while($continue){

$params=array(
"access_token"=> $access_token,
"screen_name"=> $screen_name,
"page"=>$page,
"count"=>$count
);
$res = Http::request($url, $params, 'GET');//訪問api
$result = json_decode($res,true);
foreach($result['statuses'] as $key => $value){
// $time[$i]= strtotime($value['created_at']);
if(intval(strtotime($value['created_at']))>$startDate){
$wl[$i]['id']=$value['id'];
$wl[$i]['source']=$value['source'];
$wl[$i]['text']=$value['text'];
if(isset($value['retweeted_status'])){
$wl[$i]['type'] = "轉寄";
}
else{
$wl[$i]['type'] = "原創";
}

$wl[$i]['reposts_count'] =$value['reposts_count'];
$wl[$i]['comments_count'] =$value['comments_count'];
$wl[$i]['created_at'] =$value['created_at'];
$wl[$i]['bmiddle_pic'] = $value['bmiddle_pic'];
preg_match_all("%#(.*?)#%si",$value['text'],$match);
$wl[$i]['title'] = $match[1][0];
$i++;
}
}
$endDate = intval(strtotime($result['statuses'][$count-1]['created_at']));
if($endDate>$startDate){
$continue=true;
}
else{
$continue = false;
}
$page++;
}

return $wl;



}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.