2009-12-30 35 views
1

我想在我的网站上显示来自twitter的最近5或10个状态。现在我正在使用以下代码来获取我的twitter状态。Zend Framework:如何从twitter获取我的状态

public function getOrganizationsTwitterUpdates(){ 

    $twitter = new Zend_Service_Twitter('myusername', 'mypassword'); 
    $response = $twitter->status->userTimeline(); 
    return $response; 
} 

而且我必须按照上面的代码进行响应。

Zend_Rest_Client_Result Object ([_sxml:protected] => SimpleXMLElement Object ([@attributes] => Array ([type] => array) [status] => Array ([0] => SimpleXMLElement Object ([created_at] => Wed Dec 30 11:02:13 +0000 2009 [id] => 7192975030 [text] => This is my 2nd tweet. [source] => web [truncated] => false [in_reply_to_status_id] => SimpleXMLElement Object () [in_reply_to_user_id] => SimpleXMLElement Object () [favorited] => false [in_reply_to_screen_name] => SimpleXMLElement Object () [user] => SimpleXMLElement Object ([id] => 100469557 [name] => naveed [screen_name] => naveedriksof [location] => SimpleXMLElement Object () [description] => SimpleXMLElement Object () [profile_image_url] => http://s.twimg.com/a/1262113883/images/default_profile_6_normal.png [url] => SimpleXMLElement Object () [protected] => false [followers_count] => 0 [profile_background_color] => 9ae4e8 [profile_text_color] => 000000 [profile_link_color] => 0000ff [profile_sidebar_fill_color] => e0ff92 [profile_sidebar_border_color] => 87bc44 [friends_count] => 0 [created_at] => Wed Dec 30 10:59:31 +0000 2009 [favourites_count] => 0 [utc_offset] => SimpleXMLElement Object () [time_zone] => SimpleXMLElement Object () [profile_background_image_url] => http://s.twimg.com/a/1262113883/images/themes/theme1/bg.png [profile_background_tile] => false [notifications] => false [geo_enabled] => false [verified] => false [following] => false [statuses_count] => 2) [geo] => SimpleXMLElement Object ()) [1] => SimpleXMLElement Object ([created_at] => Wed Dec 30 11:01:43 +0000 2009 [id] => 7192966364 [text] => This is my 1st tweet [source] => web [truncated] => false [in_reply_to_status_id] => SimpleXMLElement Object () [in_reply_to_user_id] => SimpleXMLElement Object () [favorited] => false [in_reply_to_screen_name] => SimpleXMLElement Object () [user] => SimpleXMLElement Object ([id] => 100469557 [name] => naveed [screen_name] => naveedriksof [location] => SimpleXMLElement Object () [description] => SimpleXMLElement Object () [profile_image_url] => http://s.twimg.com/a/1262113883/images/default_profile_6_normal.png [url] => SimpleXMLElement Object () [protected] => false [followers_count] => 0 [profile_background_color] => 9ae4e8 [profile_text_color] => 000000 [profile_link_color] => 0000ff [profile_sidebar_fill_color] => e0ff92 [profile_sidebar_border_color] => 87bc44 [friends_count] => 0 [created_at] => Wed Dec 30 10:59:31 +0000 2009 [favourites_count] => 0 [utc_offset] => SimpleXMLElement Object () [time_zone] => SimpleXMLElement Object () [profile_background_image_url] => http://s.twimg.com/a/1262113883/images/themes/theme1/bg.png [profile_background_tile] => false [notifications] => false [geo_enabled] => false [verified] => false [following] => false [statuses_count] => 1) [geo] => SimpleXMLElement Object ())))) 

我有2个问题:

Q1。如何将上面的对象转换成数组。在上面的对象中,我可以看到我的两个状态,但是如何将我的状态存储到像这样的变量中。

$firstStatus = "This is my first tweet"; 
$firstStatusTime = "4:30PM 12-12-09"; 
$secondStatus = "This is my second tweet"; 
$secondStatusTime = "9:30PM 12-12-09"; 

Q2。我可以在没有我的密码的情况下获得我的所有状态(因为我们可以在网上看到任何人的状态)。我不想使用RSS。

回答

8

您可以从单个用户获得与状态Zend_Service_Twitter_Search

$twitter_search = new Zend_Service_Twitter_Search('json'); 
$response = $twitter_search->search('from:Username'); 
print_r($response); 
+0

这是为我工作。此搜索方法以数组的形式返回结果,我不需要将其转换为数组。我可以通过用户名搜索它,我不需要密码。谢谢。 – NAVEED 2009-12-31 07:08:31

+0

如果有人遇到这个答案,值得注意的是Zend_Service_Twitter_Search使用搜索API,因此只会返回大约一周的推文。如果用户在上周没有发推文,则必须使用zend_service_twitter的用户时间轴方法 – raymosley 2013-04-04 00:52:31

1

我已经转换成字符串对象的SimpleXML /阵列等

$阵列=(数组)simplexml_load_string($ stroq);

我不知道在这种情况下这是否适合你。这是为了从我从API获得的XML字符串中获取数组。说到这一点,是否有可能以json的形式获取Twitter feed并在其上使用json_decode?这比使用XML要容易得多。

不管怎么说,它看起来像你的微博路径

$firstStatusTime=$response->status[1]->created_at 
$firstStatus=$response->status[1]->text 

但很难说没有对象本身在手或一个更加清晰的打印。

对于第二季度来说,是的,你可以从没有密码的twitter上获取任何人的消息(如果他们没有被保护)。

2

至于回答你的第一个问题,你可以使用以下直接从Zend Framework Wiki采取的代码片段:

$twitter = new Zend_Service_Twitter($user, $pass); 

// Get public timeline 
$publicTimeline = $twitter->status->publicTimeline(); 

// Loop through results: 
foreach ($publicTimeline->status as $status) { 
    $date = $status->created_at(); 
    $text = $status->text(); 
    $user = $status->user->screen_name(); 
    echo "$date: @$user: $text<br />\n"; 
} 

现在变量$date$text$user也可以是数组,在这种情况下,你可以写:

$statusidx = 0; 
// Loop through results: 
foreach ($publicTimeline->status as $status) { 
    $date[$statusidx] = $status->created_at(); 
    $text[$statusidx] = $status->text(); 
    $user[$statusidx] = $status->user->screen_name(); 
    $statusidx++; 
    echo "$date: @$user: $text<br />\n"; 
} 

我无法帮助你与你的第二个问题,仅仅是因为我没有一个Twitter帐户,并没有经验,与一般的Twitter API的工作。但从Zend Framework API类来看,它看起来并不像它。

+0

收到以下错误:致命错误:调用未定义的方法的SimpleXMLElement :: created_at() – NAVEED 2009-12-30 13:33:31

+0

尝试foreach循环之前插入这样的:' $ twitter_status = new SimpleXMLElement($ publicTimeline);'并将foreach循环中的'$ publicTimeline-> status'更改为:'$ twitter_status',因此它说:'$ twitter_status as $ status' ...这只是一个猜测。 – 2009-12-30 13:59:30

+0

您可以根据您的上述评论编辑您的答案。不管怎样,谢谢。让我试试这个。 – NAVEED 2009-12-30 16:28:34