My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
TwitterAPIClassExamples  

Here are a few examples on using the Twitter extension.

  1. First make sure you've added the extension under the extensions directory located at: protected/extensions.
  2. Add the following array code to the components array in the application configuration file:
'twitter' => array(
			'class' => 'application.extensions.twitter.VGTwitter', // path to the twitter extension
			'username' => 'LOGINNAME', // login name, this is not required all the time but most api calls need this set
                        'apiCallType' => 'statuses/home_timeline', // the api call to perform, the default is set to statuses/user_timeline
			'password' => 'PASSWORD', // password for the twitter account
			'authenticate' => true, // if the twitter api call needs authentication then this must be set to true since by default it is set to false
			'format' => 'rss', // default is xml so we will configure this as rss for this example
			'postParams' => array( 'count' => 2 ) // we want only the first two results, meaning tweets
			),

Then all we have to do in our code is to run the following:

$returned = Yii::app()->twitter->get()->getResponseData();

Now the $returned is an array/object/json depends on the format you chose to receive, that can be manipulated.

If we do the following:

CVarDumper::dump(Yii::app()->twitter->get()->getResponseData(), 10, true);

Then we shall get something similar to this (with the above configuration set):

'<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:georss="http://www.georss.org/georss">
  <channel>
    <title>Twitter / gabrielva</title>
    <link>http://twitter.com/gabrielva</link>
    <atom:link type="application/rss+xml" href="http://twitter.com/statuses/user_timeline.rss?count=2" rel="self"/>
    <description>Twitter updates from Vadim Gabriel / gabrielva.</description>
    <language>en-us</language>
    <ttl>40</ttl>
  <item>
    <title>gabrielva: New blog post: HipHop PHP - &#1500;&#1494;&#1493;&#1494; &#1502;&#1492;&#1512;? http://www.vadimg.com/2010/02/07/hiphop-php-%d7%9c%d7%96%d7%95%d7%96-%d7%9e%d7%94%d7%a8/</title>
    <description>gabrielva: New blog post: HipHop PHP - &#1500;&#1494;&#1493;&#1494; &#1502;&#1492;&#1512;? http://www.vadimg.com/2010/02/07/hiphop-php-%d7%9c%d7%96%d7%95%d7%96-%d7%9e%d7%94%d7%a8/</description>
    <pubDate>Sun, 07 Feb 2010 10:40:45 +0000</pubDate>
    <guid>http://twitter.com/gabrielva/statuses/8760440526</guid>
    <link>http://twitter.com/gabrielva/statuses/8760440526</link>
  </item>
  <item>
    <title>gabrielva: #Zend_Magic Proposal is hilarious  http://tr.im/LzAI LOL #ZF #Zend</title>
    <description>gabrielva: #Zend_Magic Proposal is hilarious  http://tr.im/LzAI LOL #ZF #Zend</description>
    <pubDate>Mon, 25 Jan 2010 19:36:11 +0000</pubDate>
    <guid>http://twitter.com/gabrielva/statuses/8203912090</guid>
    <link>http://twitter.com/gabrielva/statuses/8203912090</link>
  </item>
  </channel>
</rss>
' '<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:georss="http://www.georss.org/georss">
  <channel>
    <title>Twitter / gabrielva</title>
    <link>http://twitter.com/gabrielva</link>
    <atom:link type="application/rss+xml" href="http://twitter.com/statuses/user_timeline.rss?count=2" rel="self"/>
    <description>Twitter updates from Vadim Gabriel / gabrielva.</description>
    <language>en-us</language>
    <ttl>40</ttl>
  <item>
    <title>gabrielva: New blog post: HipHop PHP - &#1500;&#1494;&#1493;&#1494; &#1502;&#1492;&#1512;? http://www.vadimg.com/2010/02/07/hiphop-php-%d7%9c%d7%96%d7%95%d7%96-%d7%9e%d7%94%d7%a8/</title>
    <description>gabrielva: New blog post: HipHop PHP - &#1500;&#1494;&#1493;&#1494; &#1502;&#1492;&#1512;? http://www.vadimg.com/2010/02/07/hiphop-php-%d7%9c%d7%96%d7%95%d7%96-%d7%9e%d7%94%d7%a8/</description>
    <pubDate>Sun, 07 Feb 2010 10:40:45 +0000</pubDate>
    <guid>http://twitter.com/gabrielva/statuses/8760440526</guid>
    <link>http://twitter.com/gabrielva/statuses/8760440526</link>
  </item>
  <item>
    <title>gabrielva: #Zend_Magic Proposal is hilarious  http://tr.im/LzAI LOL #ZF #Zend</title>
    <description>gabrielva: #Zend_Magic Proposal is hilarious  http://tr.im/LzAI LOL #ZF #Zend</description>
    <pubDate>Mon, 25 Jan 2010 19:36:11 +0000</pubDate>
    <guid>http://twitter.com/gabrielva/statuses/8203912090</guid>
    <link>http://twitter.com/gabrielva/statuses/8203912090</link>
  </item>
  </channel>
</rss>
' '<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:georss="http://www.georss.org/georss">
  <channel>
    <title>Twitter / gabrielva</title>
    <link>http://twitter.com/gabrielva</link>
    <atom:link type="application/rss+xml" href="http://twitter.com/statuses/user_timeline.rss?count=2" rel="self"/>
    <description>Twitter updates from Vadim Gabriel / gabrielva.</description>
    <language>en-us</language>
    <ttl>40</ttl>
  <item>
    <title>gabrielva: New blog post: HipHop PHP - &#1500;&#1494;&#1493;&#1494; &#1502;&#1492;&#1512;? http://www.vadimg.com/2010/02/07/hiphop-php-%d7%9c%d7%96%d7%95%d7%96-%d7%9e%d7%94%d7%a8/</title>
    <description>gabrielva: New blog post: HipHop PHP - &#1500;&#1494;&#1493;&#1494; &#1502;&#1492;&#1512;? http://www.vadimg.com/2010/02/07/hiphop-php-%d7%9c%d7%96%d7%95%d7%96-%d7%9e%d7%94%d7%a8/</description>
    <pubDate>Sun, 07 Feb 2010 10:40:45 +0000</pubDate>
    <guid>http://twitter.com/gabrielva/statuses/8760440526</guid>
    <link>http://twitter.com/gabrielva/statuses/8760440526</link>
  </item>
  <item>
    <title>gabrielva: #Zend_Magic Proposal is hilarious  http://tr.im/LzAI LOL #ZF #Zend</title>
    <description>gabrielva: #Zend_Magic Proposal is hilarious  http://tr.im/LzAI LOL #ZF #Zend</description>
    <pubDate>Mon, 25 Jan 2010 19:36:11 +0000</pubDate>
    <guid>http://twitter.com/gabrielva/statuses/8203912090</guid>
    <link>http://twitter.com/gabrielva/statuses/8203912090</link>
  </item>
  </channel>
</rss>

---

Manual Configuring

When we need to we can event override the default configuration by passing five arguments to the get() function. and those arguments are:

CVarDumper::dump(Yii::app()->twitter->get( $apiCall, $autheticate, $format, $postParams, $usePost )->getResponseData(), 10, true);
  • $apiCall means the api call to make based on the available calls that are listed in the Twitter API Documentationhttp://apiwiki.twitter.com/Twitter-API-Documentation.
  • $authenticate is a boolean value that means if we need to authenticate a member or not
  • $format means the format we would like to retrieve from the call (json, xml, rss, atom, etc...).
  • $postParams are an array of additional parameters to bound to the request. Each API call has it's own parameters so you will need to check the documentation for each one. Most of the calls have user_id, count, page, screen_name.
  • $usePost is a boolean value if you would like to use a post method instead of the default get which is set to be the default.

So if i use the following code:

CVarDumper::dump(Yii::app()->twitter->get( 'statuses/user_timeline', true, 'json', array('count' => 5) )->getResponseData(), 10, true);

Then i shall see the following output:

'[{"in_reply_to_screen_name":null,"created_at":"Sun Feb 07 10:40:45 +0000 2010","in_reply_to_status_id":null,"place":null,"favorited":false,"in_reply_to_user_id":null,"source":"<a href=\"http://alexking.org/projects/wordpress\" rel=\"nofollow\">Twitter Tools</a>","contributors":null,"coordinates":null,"user":{"profile_background_image_url":"http://s.twimg.com/a/1270070748/images/themes/theme9/bg.gif","created_at":"Mon Mar 30 05:50:24 +0000 2009","friends_count":47,"description":"Doing what i like best. Software Engineer, PHP 5 Zend Certified &amp; Zend Framework Certified. ","contributors_enabled":false,"profile_link_color":"2FC2EF","following":false,"profile_background_tile":false,"favourites_count":0,"profile_background_color":"1A1B1F","url":"http://www.vadimg.co.il/","geo_enabled":false,"profile_image_url":"http://a1.twimg.com/profile_images/114545546/n535332309_1262592_7481_normal.jpg","profile_sidebar_fill_color":"252429","location":"Israel","verified":false,"screen_name":"gabrielva","time_zone":"Jerusalem","profile_sidebar_border_color":"181A1E","protected":false,"name":"Vadim Gabriel","followers_count":65,"id":27593246,"lang":"en","statuses_count":148,"notifications":false,"utc_offset":7200,"profile_text_color":"666666"},"geo":null,"truncated":false,"id":8760440526,"text":"New blog post: HipHop PHP - \u05dc\u05d6\u05d5\u05d6 \u05de\u05d4\u05e8? http://www.vadimg.com/2010/02/07/hiphop-php-%d7%9c%d7%96%d7%95%d7%96-%d7%9e%d7%94%d7%a8/"},{"place":null,"favorited":false,"coordinates":null,"in_reply_to_screen_name":null,"in_reply_to_user_id":null,"geo":null,"source":"web","in_reply_to_status_id":null,"created_at":"Mon Jan 25 19:36:11 +0000 2010","user":{"geo_enabled":false,"profile_text_color":"666666","description":"Doing what i like best. Software Engineer, PHP 5 Zend Certified &amp; Zend Framework Certified. ","profile_background_image_url":"http://s.twimg.com/a/1268937079/images/themes/theme9/bg.gif","followers_count":65,"location":"Israel","friends_count":47,"profile_link_color":"2FC2EF","profile_background_tile":false,"favourites_count":0,"profile_background_color":"1A1B1F","verified":false,"notifications":false,"lang":"en","statuses_count":148,"profile_sidebar_fill_color":"252429","profile_image_url":"http://a1.twimg.com/profile_images/114545546/n535332309_1262592_7481_normal.jpg","created_at":"Mon Mar 30 05:50:24 +0000 2009","protected":false,"contributors_enabled":false,"time_zone":"Jerusalem","profile_sidebar_border_color":"181A1E","screen_name":"gabrielva","name":"Vadim Gabriel","following":false,"id":27593246,"utc_offset":7200,"url":"http://www.vadimg.co.il/"},"truncated":false,"contributors":null,"id":8203912090,"text":"#Zend_Magic Proposal is hilarious  http://tr.im/LzAI LOL #ZF #Zend"},{"in_reply_to_screen_name":null,"created_at":"Sun Dec 06 10:10:50 +0000 2009","in_reply_to_status_id":null,"place":null,"favorited":false,"in_reply_to_user_id":null,"source":"<a href=\"http://apiwiki.twitter.com/\" rel=\"nofollow\">API</a>","contributors":null,"coordinates":null,"user":{"profile_background_image_url":"http://s.twimg.com/a/1270070748/images/themes/theme9/bg.gif","created_at":"Mon Mar 30 05:50:24 +0000 2009","friends_count":47,"description":"Doing what i like best. Software Engineer, PHP 5 Zend Certified &amp; Zend Framework Certified. ","contributors_enabled":false,"profile_link_color":"2FC2EF","following":false,"profile_background_tile":false,"favourites_count":0,"profile_background_color":"1A1B1F","url":"http://www.vadimg.co.il/","geo_enabled":false,"profile_image_url":"http://a1.twimg.com/profile_images/114545546/n535332309_1262592_7481_normal.jpg","profile_sidebar_fill_color":"252429","location":"Israel","verified":false,"screen_name":"gabrielva","time_zone":"Jerusalem","profile_sidebar_border_color":"181A1E","protected":false,"name":"Vadim Gabriel","followers_count":65,"id":27593246,"lang":"en","statuses_count":148,"notifications":false,"utc_offset":7200,"profile_text_color":"666666"},"geo":null,"truncated":false,"id":6396615722,"text":"\u05d4\u05ea\u05e7\u05e0\u05d4 \u05d5\u05e9\u05d9\u05de\u05d5\u05e9 \u05d1 XDEBUGhttp://bit.ly/8FAvzc"},{"in_reply_to_screen_name":null,"created_at":"Mon Nov 30 11:28:38 +0000 2009","in_reply_to_status_id":null,"place":null,"favorited":false,"in_reply_to_user_id":null,"source":"web","contributors":null,"coordinates":null,"user":{"profile_background_image_url":"http://s.twimg.com/a/1270070748/images/themes/theme9/bg.gif","created_at":"Mon Mar 30 05:50:24 +0000 2009","friends_count":47,"description":"Doing what i like best. Software Engineer, PHP 5 Zend Certified &amp; Zend Framework Certified. ","contributors_enabled":false,"profile_link_color":"2FC2EF","following":false,"profile_background_tile":false,"favourites_count":0,"profile_background_color":"1A1B1F","url":"http://www.vadimg.co.il/","geo_enabled":false,"profile_image_url":"http://a1.twimg.com/profile_images/114545546/n535332309_1262592_7481_normal.jpg","profile_sidebar_fill_color":"252429","location":"Israel","verified":false,"screen_name":"gabrielva","time_zone":"Jerusalem","profile_sidebar_border_color":"181A1E","protected":false,"name":"Vadim Gabriel","followers_count":65,"id":27593246,"lang":"en","statuses_count":148,"notifications":false,"utc_offset":7200,"profile_text_color":"666666"},"geo":null,"truncated":false,"id":6198831751,"text":"Just got my Flickr account check it out http://www.flickr.com/photos/vadimvgabriel/ #flickr"},{"in_reply_to_screen_name":null,"created_at":"Thu Nov 26 17:11:51 +0000 2009","in_reply_to_status_id":null,"place":null,"favorited":false,"in_reply_to_user_id":null,"source":"web","contributors":null,"coordinates":null,"user":{"profile_background_image_url":"http://s.twimg.com/a/1270070748/images/themes/theme9/bg.gif","created_at":"Mon Mar 30 05:50:24 +0000 2009","friends_count":47,"description":"Doing what i like best. Software Engineer, PHP 5 Zend Certified &amp; Zend Framework Certified. ","contributors_enabled":false,"profile_link_color":"2FC2EF","following":false,"profile_background_tile":false,"favourites_count":0,"profile_background_color":"1A1B1F","url":"http://www.vadimg.co.il/","geo_enabled":false,"profile_image_url":"http://a1.twimg.com/profile_images/114545546/n535332309_1262592_7481_normal.jpg","profile_sidebar_fill_color":"252429","location":"Israel","verified":false,"screen_name":"gabrielva","time_zone":"Jerusalem","profile_sidebar_border_color":"181A1E","protected":false,"name":"Vadim Gabriel","followers_count":65,"id":27593246,"lang":"en","statuses_count":148,"notifications":false,"utc_offset":7200,"profile_text_color":"666666"},"geo":null,"truncated":false,"id":6086723330,"text":"Thinks whether he should open a public Hebrew support community for #IPS products that he can actually run, Maintain & support actually #ipb"}]'

---

Simple!

So in order to be able to actually fetch what ever you need to from twitter. You need to follow a few simple steps:

  1. Visit the Twitter API Documentationhttp://apiwiki.twitter.com/Twitter-API-Documentation and choose the api call you would like to request. For this demonstration i chose statuses/show which show a single status specified by ID.
  2. Next either through the application configuration which will be easier or directly through the get() method enter the api call. In this example i will use the get() method.

So we had this:

CVarDumper::dump(Yii::app()->twitter->get()->getResponseData(), 10, true);

After adding the api call we just chose we will end up with:

CVarDumper::dump(Yii::app()->twitter->get('statuses/show/X')->getResponseData(), 10, true);

Notice the X in the first argument, That means the number/ID of the status we would like to show. So from my twitter account i will show status number 8203912090. So now i have:

CVarDumper::dump(Yii::app()->twitter->get('statuses/show/8203912090')->getResponseData(), 10, true);

I can configure the rest four arguments but i will just leave them as is and the default ones will be used either from the application configuration settings or the default ones that are initialized when the extension is loaded.

the above code gave me the following result:

array
(
    [created_at] => 'Mon Jan 25 19:36:11 +0000 2010'
    [id] => '8203912090'
    [text] => '#Zend_Magic Proposal is hilarious  http://tr.im/LzAI LOL #ZF #Zend'
    [source] => 'web'
    [truncated] => 'false'
    [in_reply_to_status_id] => ''
    [in_reply_to_user_id] => ''
    [favorited] => 'false'
    [in_reply_to_screen_name] => ''
    [user] => array
    (
        [id] => '27593246'
        [name] => 'Vadim Gabriel'
        [screen_name] => 'gabrielva'
        [location] => 'Israel'
        [description] => 'Doing what i like best. Software Engineer, PHP 5 Zend Certified & Zend Framework Certified. '
        [profile_image_url] => 'http://a1.twimg.com/profile_images/114545546/n535332309_1262592_7481_normal.jpg'
        [url] => 'http://www.vadimg.co.il/'
        [protected] => 'false'
        [followers_count] => '65'
        [profile_background_color] => '1A1B1F'
        [profile_text_color] => '666666'
        [profile_link_color] => '2FC2EF'
        [profile_sidebar_fill_color] => '252429'
        [profile_sidebar_border_color] => '181A1E'
        [friends_count] => '47'
        [created_at] => 'Mon Mar 30 05:50:24 +0000 2009'
        [favourites_count] => '0'
        [utc_offset] => '7200'
        [time_zone] => 'Jerusalem'
        [profile_background_image_url] => 'http://s.twimg.com/a/1270070748/images/themes/theme9/bg.gif'
        [profile_background_tile] => 'false'
        [notifications] => 'false'
        [geo_enabled] => 'false'
        [verified] => 'false'
        [following] => 'false'
        [statuses_count] => '148'
        [lang] => 'en'
        [contributors_enabled] => 'false'
    )
    [geo] => ''
    [coordinates] => ''
    [place] => ''
    [contributors] => ''
)

Remember that each call has it's own allowed formats to make. So the statuses/show/ID allows either xml or json so the i just changed the application configuration to xml for this call to work, And as you can see it did.

The twitter extension comes with a built-in caching system which can use any caching component you have set up in your application. By default it uses the cache component named 'cache' which is the default one. If you would like to change this you can do this by configuring the twitter extension in the application configuration:

'twitter' => array(
			'class' => 'application.extensions.twitter.VGTwitter',
			'username' => 'LOGIN',
			'password' => 'PASSWORD',
			'authenticate' => true,
			'cacheID' => 'cache component name', // default is set to 'cache' if you would like to disable caching then set this property to 'false', if you want to use another caching component then just enter it's name here. 
			'cachingDuration' => 5, // number of seconds to store the returned data in cache so you won't need to make the call each time a user requests the page. The default is 60 seconds
			),

---

This is just part of the features the twitter class offers. For the full capabilities please read the contents of the twitter application extension class file as it is well documented.

Thanks.

Comment by imr...@gmail.com, Dec 14, 2010

twitter don't support basic authentication


Sign in to add a comment
Powered by Google Project Hosting