Skip to content

Instantly share code, notes, and snippets.

@lizheming
Created February 14, 2012 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizheming/1826642 to your computer and use it in GitHub Desktop.
Save lizheming/1826642 to your computer and use it in GitHub Desktop.
获取一周内的新浪微博消息
<?php
function wholeweek($gdate = "", $first = 0) //get the week's start time and end time
{
if (!$gdate) $gdate = date("Y-m-d");
$w = date("w", strtotime($gdate));
$dn = $w ? $w - $first : 6;
$st = date("Y-m-d", strtotime("$gdate -" . $dn . " days"));
$en = strtotime("$st +6 days");
return array(strtotime($st),$en);
}
function getFromUrl($url,$timeout=5) //file_get_contents
{
$opts = array("http"=>array("header"=>"Referer:".$url,"method"=>"GET","timeout"=>$timeout));
$context = stream_context_create($opts);
$html = @file_get_contents($url, false, $context);
return $html;
}
$week = wholeweek();
$post = array();
function get_post($max=0)
{
$add_max = '';
if($max != 0) $add_max = '&max_id=' . $max;
$url = 'http://api.weibo.com/2/statuses/user_timeline.json?source=2546437393&screen_name=mattdallas' . $add_max;
$json = json_decode(getFromUrl($url), true);
$statuses = $json['statuses'];
$count = count($statuses);
for($i=0;$i<$count;$i++)
{
$created_at = strtotime($statuses[$i]['created_at']);
if( $created_at > $week[0] && $created_at < $week[1])
{
$post[] = $statuses[$i];
if($i == $count-1) get_post($statuses[$i]['mid']);
}
else
{
break;
}
}
}
get_post();
echo '<pre>';
print_r($post);
echo '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment