Categories

Tags Cloud

RSS Feed

Subscribe to blog RSS Feed Subscribe to Blog's RSS Feed
AUG
2011
12
1 Comment
842 Hits
Fetching Tweets on Twitter by PHP
Posted under: PHP, Tutorial

Twitter BirdIf you have your own blog which is PHP powered, you can show your tweets on twitter by using PHP easily. According to GET Search Twitter API documentation and Using Search documentation, we can do any search for tweets related to any keyword provided. The resource URL for searching is:

http://search.twitter.com/search.format

where format is the format we would like to get. It could be rss, atom, or even json. The Twitter search API is very powerful, even you can get any tweets which is posted by someone near you.

Right to example, I have used this API to show any recent tweets related to me, which is containing the word "aryoxp". I need it in RSS format so I can process it further in PHP by using RSS parser. So the resource URL would be as simple as:

http://search.twitter.com/search.rss?q=aryoxp

Note that I did not including the "@" character to the query string, because I would like to get all posts related to aryoxp not only posts by @aryoxp or have @aryoxp mentioned. You can get more complete information about parameters used in the query string here.

Try to copy and paste the above resource URL to your browser. The twitter API then will return all of matched post related to your search.

Fetching The Resource URL

To fetch the RSS by using PHP, you will need the PHP cURL library. If you're using public commercial web hosting service which provides PHP support, usually the cURL library is already installed and ready for you to use. For more information about cURL installation, read it here. You don't know whether cURL is enabled or not on your PHP? Check it out, by using the following world famous simple oneliner PHP code. If it's already installed, then it will show the installed cURL library information.

<?php phpinfo(); ?>

The following PHP code is used to fetch the twitter RSS (in XML format) using PHP cURL:

<?php

$keyword = "aryoxp"; 
$timeout = 0;
$url = "http://search.twitter.com/search.rss?q=".$keyword;

//initialize cURL
$ch = curl_init();

//Set cURL options
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

// execute the cURL to fetch the RSS
$rss=curl_exec($ch);
curl_close($ch);

?>
The RSS fetched from the server will be contained in $rss variable. You can get the post items from the fetched RSS by using any of PHP RSS parser out there or even you can build your own RSS parser by using PHP XML built in library.
 
Here is some parts of the RSS which contains a single post on twitter:
...
<item>
    <title>Starting a project: caching my #twitter posts and other related.</title>
    <link>http://twitter.com/aryoxp/statuses/101411289137553410</link>
    <description>Starting a project: caching my &lt;a href="http://search.twitter.com/search?q=%23twitter" title="#twitter" class=" "&gt;#twitter&lt;/a&gt; posts and other related.</description>
    <pubDate>Wed, 10 Aug 2011 21:55:10 +0000</pubDate>
    <guid>http://twitter.com/aryoxp/statuses/101411289137553410</guid>
    <author>aryoxp@twitter.com (Aryo)</author>
    <media:content type="image/jpg" height="48" width="48" url="http://a3.twimg.com/profile_images/1325498499/foto_normal.jpg" />
    <google:image_link>http://a3.twimg.com/profile_images/1325498499/foto_normal.jpg</google:image_link>
    <twitter:metadata>
        <twitter:result_type>recent</twitter:result_type>
    </twitter:metadata>
</item>
...
Next I will try to show you how the parse the fetched RSS from twitter by using an RSS parser. Drop me a line or two if you have any questions or you have something to tell me about what you think.
Next
Prev
25 Feb 2012
04:35 AM
1
Okho

Buat twitter JSON lebih irit benwit yo. Enaknya lg langsung si browser client yg decode. Jadi server melayani sebanyak mungkin static files. Seicrit dynamic pages Hehehe good post bro! Aktifis FPS (front pembela server)

Write Your Comments

Comments are parsed with Markdown.
 
Notes
* Your email is required to submit this form, and it will not be published or shared without your consent. We use your email address to show your avatar picture profile from Gravatar. Don't have one? Then sign up to gravatar and create your own here.
We also filters your comment against SPAM because we hate SPAM as much as you do. If your comment is recognized as SPAM then it will be moderated, otherwise it will shows up immediately.
Form Key: #b2ecef472af0403ee818ba10391f8521
Loading...