views:

321

answers:

3

I want to show my last 5 or 10 statuses from twitter on my site. For now I am using following code to get my twitter statuses.

public function getOrganizationsTwitterUpdates(){

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

And I have to following response from above code.

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 ( ) ) ) ) )

I have 2 question:

Q1. How to convert above object into array. In above object I can see my two statuses but how can I store my status into variable like this.

$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. Can I get my all statuses without my password (As we can see anyone's status on web). I dont want to use RSS.

+1  A: 

I've converted strings into SimpleXML objects/arrays like

$array=(array)simplexml_load_string($stroq);

I don't know if this will work for you in this case. That was for getting an array from an XML string I got from an API. Speaking of which, is it possible to just get the Twitter feed as json and use json_decode on it? That would be a lot easier than working with XML.

Anyway, it looks like the path for your tweets is

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

But it's hard to tell without the object itself in hand or a more clear printing.

For Q2, yes, you can get anyone's messages from twitter with no password (if they're not protected).

Alex JL
Thanks for your response.
NAVEED
+2  A: 

As answer to your first question, you could use following code snippet taken directly from the 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"; 
} 

Now the variables $date, $text and $user could also be arrays, in which case you could write:

$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"; 
} 

I am unable to help you with your second question, simply because I don't have a Twitter account and have no experience working with the Twitter API in general. But it doesn't seem like it, judging from the Zend Framework API class.

TommyA
getting following error: Fatal error: Call to undefined method SimpleXMLElement::created_at()
NAVEED
Try inserting this before the foreach loop: `$twitter_status = new SimpleXMLElement($publicTimeline);` and change the `$publicTimeline->status` in th foreach loop to: `$twitter_status` so it says: `$twitter_status as $status` ... this is just a guess.
TommyA
you can edit your answer according to your above comments. anyway thanks. let me try this.
NAVEED
I know this, but I didn't want to edit the answer until I could find out if my addition helped or not. :)
TommyA
+3  A: 

You can get statuses from an individual user with Zend_Service_Twitter_Search:

$twitter_search = new Zend_Service_Twitter_Search('json');
$response = $twitter_search->search('from:Username');
print_r($response);
GZipp
This is working for me. This search method return the result in the form of array and I don't need to to convert it into array. I can search it by username, I don't need password for this. Thanks.
NAVEED