网站调取Twitter信息|格式化输出时间
之前,我已经写过这方面的文章了,因此,
若是Wordpress 博客调取Twitter信息,请参看:wordpress获得Twitter最新内容代码[非插件][不登录][不怕啬]
本文方法为通解,适宜于各类网站使用。[注:国内主机无效,请参看上面的文章解决]
以下为获得一条信息方法,多条可以控制 &rpp=X 并调整截取函数为循环 达成,【已补充获取多条的代码,在文末】
第一阶段:
<?php // Your twitter username. $username = "hzlzh"; $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1"; function parse_feed($feed) { $step1 = explode("<content type=\"html\">", $feed); $step2 = explode("</content>", $step1[1]); $tweet = $step2[0]; $step3 = explode("<updated>", $step2[1]); $step4 = explode("</updated>", $step3[1] ); $time = $step4[0]; $step5 = explode("<twitter:source>", $step4[1]); $step6 = explode("</twitter:source>", $step5[1] ); $from = $step6[0]; $all = str_replace(array("<",">"),array("<",">"),array($tweet,$time,$from)); return $all; } $twitterFeed = file_get_contents($feed); $result = parse_feed($twitterFeed); list($tweet,$time,$from)=$result; echo $tweet."****".$time."***".$from; //echo strip_tags($tweet).date("G:i:s A F-jS-Y",$time).strip_tags($from); //去掉注释,得到纯文本 ?>
可复制此代码在 test.hzlzh.com 页面在线调试,至于CSS样式,自己舞弄吧。
PS:这里得到的时间不是本地时间,最好在echo 前加上这个设置:date_default_timezone_set("Asia/Chongqing"); 语句
第二阶段:
怎样得到这样的时间格式?----------> 14小时 9分钟 前 From 推TE
使用下面的函数:
<?php
function time_since($older_date, $newer_date = false){
$chunks = array(array(60 * 60 , '小时'),array(60 , '分钟'),);
$newer_date = ($newer_date == false) ? (time()+(60*60*get_settings("gmt_offset"))) : $newer_date;
$since = $newer_date - $older_date;
if($since<86400 and $since>60){
for ($i = 0, $j = count($chunks); $i < $j; $i++){
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0){
break;}}
$output = "$count{$name}";
if ($i + 1 < $j){
$seconds2 = $chunks[$i + 1][0];
$name2 = $chunks[$i + 1][1];
if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0){
$output .= " $count2{$name2}";}}
return $output." 前";}
elseif($since<60 and $since=60){
echo '1 分钟 之内';}
else{
printf(the_time('Y-m-j G:i'));
}}
?>
然后调用 time_since(abs(strtotime($time)), time()); 即可得到如上格式。
补充说明:以上方法可以得到纯净的 twitter消息,以便于自定义其css样式,所以如果对样式要求不高,
那么下面这个调用途径更佳!
先番啬,然后到 http://twitter.com/goodies 选择 weight 即可,之后的操作方便快捷,不必多说。
附:Twitter XML文件格式
<content type="html"></content> 消息主体
<updated>2010-05-19T00:56:37Z</updated> 时间
<link type="image/png" href="http://a3.twimg.com/profile_images/837942033/hzlzh_normal.gif" rel="image"/> 头像图片
<twitter:geo></twitter:geo>此项是最近特火的 共享地理位置
<twitter:metadata></twitter:metadata>
<twitter:source></twitter:source> 发推的方式,即 FROM where
<twitter:lang>zh</twitter:lang> 语言
<author>
<name>hzlzh (HzlzH)</name>
<uri>http://twitter.com/hzlzh</uri>
</author>
有了这个简单替换上面代码便可得到想要的 twitter 条目啦。
ps,若使用twitter 的rss 导出,由于请求次数有限制150个,时常会发生抓取不到信息的情形,而使用search 功能的到timeline就没有这个弊端。
后期补充:获得多条的代码,格式请自己调整!
<?php
// Your twitter username.
$username = "hzlzh";
// How many items you want.
$X=2;
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=".$X;
function parse_feed($feed,$X) {
$all = array();
$step1 = explode("<content type=\"html\">", $feed);
for($i=0;$i<$X;$i++){
$step2 = explode("</content>", $step1[$i+1]);
$tweet = $step2[0];
$step3 = explode("<updated>", $step2[1]);
$step4 = explode("</updated>", $step3[1] );
$time = $step4[0];
$step5 = explode("<twitter:source>", $step4[1]);
$step6 = explode("</twitter:source>", $step5[1] );
$from = $step6[0];
$all[$i] = str_replace(array("<",">"),array("<",">"),array($tweet,$time,$from,$last));
}
return $all;
}
$twitterFeed = file_get_contents($feed);
for($i=1;$i<=$X;$i++){
$result = parse_feed($twitterFeed,$i);
list($tweet,$time,$from,$last)=$result[$i-1];
echo $tweet."****".$time."****".$from."</br>";
//echo strip_tags($tweet).date("G:i:s A F-jS-Y",$time).strip_tags($from)."</br>"; //去掉注释,得到纯文本
}
?>
可复制此代码在 test.hzlzh.com 页面在线调试

本页面仅不支持 Chrome浏览器,我还偏偏是个chrome浏览器
@Ray Chow, 沙发被抢了
@Lene,
这么夜深,拿到板凳也实属不易啊
@Ray Chow, 不能怨我啊,这个代码测试程序在chrome下不能正常显示,换IE都能,怒啊。
@Ray Chow: 咪兔><
还可以输出twitter的from,这个厉害了
@万戈, 还能输出 共享地理位置呢,呵呵
學習,看看怎麼能用上
兼容问题很烦人啊。
不用翻墙吗?
@1905电影网, 文章中不是说了么,国内主机无效,但以前的文章有解决办法的
有關於調用多條訊息那兒不太明白

你說是要修改&rpp=X 中的X,我在測試頁面看不到多條訊息耶……是我遺漏了什麼步驟嗎
代碼: http://img.ly/images/340122/full
@小闇, 图片看到了,你这是改 X 成10 是不行的,你改过之后 $twitterFeed 这个变量得到的的确是 10条,但是处理函数之摘取了第一条,因为你没有写一个循环函数来逐个读取。原文中也说了:要写个循环之类的处理多条的情况,由于当时我只需要一条 所以没写,呵呵
@小闇, 好了,我给你把代码写好了,直接用吧,代码在文章结尾。
@HzlzH, 博主你是好人哦XD 感謝補充代碼m(_ _)m
弱问第二阶段那堆 是放到function里然后在sidebar调用就可以了吧
我怎么输出地时间还是正常时间且不是我发布twitter的时间呢
@snowxh: 你要在sidebar调取啊,最好把第二阶放在那个函数使用的页面,比如我在header.php中使用了twitter调取,于是就把第二阶段代码全都放在了header.php最顶部!
@HzlzH: 试了下 依然无效T-T
@snowxh: 你第一阶段的放哪里的?把第二阶段放在一起,公用一组 < ?PHP ?>试试