Created
February 14, 2012 13:03
-
-
Save lizheming/1826642 to your computer and use it in GitHub Desktop.
获取一周内的新浪微博消息
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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