Zend Framework:如何从twitter获取我的状态

时间:2009-12-30 12:23:58

标签: php zend-framework twitter

我想在我的网站上显示我最近的5或10个来自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 ( ) ) ) ) )

我有两个问题:

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。

3 个答案:

答案 0 :(得分:8)

您可以通过 Zend_Service_Twitter_Search 从单个用户获取状态:

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

答案 1 :(得分: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类来看,它看起来并不像。

答案 2 :(得分:1)

我已将字符串转换为SimpleXML对象/数组,如

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

我不知道在这种情况下这是否适合你。那是从我从API获得的XML字符串中获取数组。说到这个,是不是可以将Twitter feed作为json并使用json_decode?这比使用XML要容易得多。

无论如何,看起来你的推文的路径是

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

但如果没有手中的物体或更清晰的印刷品,很难说清楚。

对于Q2,是的,您可以从Twitter获取任何人的消息而没有密码(如果他们没有受到保护)。