【发布时间】:2011-01-17 13:25:53
【问题描述】:
我正在尝试创建一个简单的站点,该站点使用 php 和 Twitter API 来写出我在 Twitter 上的最新帖子。我找到了一个教程,向我展示了如何以 xml 格式获取我的时间线(或最近 20 个帖子),但不知道如何打印出 just 最新帖子和 just 它的文本(不是 xml/rss/etc 附带的时间、日期等)。
我的代码是这样的
<h2 id = "latest">
<?php
var $username='myusername';
var $password='mypassword';
var $responseInfo=array();
function latest_status($format='json',$id=null) {
$request = 'http://twitter.com/statuses/user_timeline.'.$format;
if($id) {
$postargs = "id=$id";
return $this->process($request,$postargs);
}
return $this->process($request);
}
echo latest_status("json");
?>
</h2>
它会返回这个:
[{"贡献者":null,"created_at":"星期二 2 月 16 日 19:56:08 +0000 2010","in_reply_to_user_id":null,"source":"API","favorited":false,"in_reply_to_status_id":null,"truncated":false,"user":{"notifications":null,"profile_link_color": "0000ff","description":"","verified":false,"profile_background_tile":false,"created_at":"星期二 2 月 16 日 01:16:15 +0000 2010","profile_background_color":"9ae4e8","profile_image_url":"http://s.twimg.com/a/1265999168/images/default_profile_1_normal.png","time_zone":"夏威夷","profile_sidebar_fill_color":"e0ff92","followers_count":0,"screen_name":"whisperingweb" ,"lang":"en","friends_count":0,"profile_sidebar_border_color":"87bc44","statuses_count":2,"following":null,"protected":false,"favourites_count":1,"location" :"","姓名":"克里斯 Armstrong","contributors_enabled":false,"profile_text_color":"000000","id":114608397,"geo_enabled":true,"profile_background_image_url":"http://s.twimg.com/a/1265999168/images/themes/theme1/bg.png","utc_offset":-36000,"url": null},"in_reply_to_screen_name":null,"geo":null,"id":9199090048,"text":"某人 在你的 网站"},{"收藏夹":false,"来源":"web","in_reply_to_user_id":null,"created_at":"星期二 2 月 16 日 18:50:21 +0000 2010","geo":null,"user":{"verified":false,"description":"","notifications":false,"profile_text_color":"000000","screen_name":"whisperingweb"," profile_background_image_url":"http://s.twimg.com/a/1265999168/images/themes/theme1/bg.png","url":null,"profile_link_color":"0000ff","followers_count":0,"statuses_count":2,"profile_background_tile":false,"created_at":"星期二 2 月 16 日 01:16:15 +0000 2010","friends_count":0,"profile_background_color":"9ae4e8","contributors_enabled":false,"time_zone":"夏威夷","favourites_count":0,"profile_sidebar_fill_color":"e0ff92","protected":false ,"位置":"","姓名":"克里斯 Armstrong","lang":"en","geo_enabled":true,"profile_sidebar_border_color":"87bc44","id":114608397,"following":false,"utc_offset":-36000,"profile_image_url":"@ 987654324@"},"contributors":null,"in_reply_to_status_id":null,"id":9196705546,"in_reply_to_screen_name":null,"truncated":false,"text":"The 敏捷的棕狐跳过懒惰的人 狗"}]
我对 php 很陌生,对 Twitter API 也很陌生,因此我将不胜感激任何帮助或建议。
编辑:已将示例从 xml 更改为 json
【问题讨论】:
-
您的代码中的
$twitter是什么?您是否使用现有的 Twitter 库来访问它?$this指的是函数内部的什么(即类定义的其余部分在哪里)?该函数的输出是什么类型(检查var_dump)。 -
啊,是的,对不起。我正在使用从这里获得的库brandontreb.com/…,我只是复制并粘贴了我认为相关的位。我应该通过删除 $twitter-> 来纠正我的问题,还是我会更好地显示整个库?