<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TweetHacking.com - Twitter Hacks &#187; twitter api</title>
	<atom:link href="http://tweethacking.com/tag/twitter-api/feed/" rel="self" type="application/rss+xml" />
	<link>http://tweethacking.com</link>
	<description>Twitter Hacks</description>
	<lastBuildDate>Thu, 17 Jun 2010 14:15:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Your Complete HOWTO Guide to Twitter API using PHP and Javascript/AJAX!</title>
		<link>http://tweethacking.com/twitter-hacks/your-complete-howto-guide-to-twitter-api-using-php-and-javascriptajax/</link>
		<comments>http://tweethacking.com/twitter-hacks/your-complete-howto-guide-to-twitter-api-using-php-and-javascriptajax/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 23:05:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Twitter Apps]]></category>
		<category><![CDATA[Twitter Hacks]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[complete]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[twitter api]]></category>

		<guid isPermaLink="false">http://tweethacking.com/?p=178</guid>
		<description><![CDATA[Recently, I&#8217;ve been developing a new Twitter app site and found some great resources on Twitter API.  Believe me, this isn&#8217;t my first Twitter app ever so I have some experience dealing with Twitter API.
I&#8217;ve begun to realize more and more that there&#8217;s a lot of great Twitter API resources out there but none are [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been developing a new Twitter app site and found some great resources on Twitter API.  Believe me, this isn&#8217;t my first Twitter app ever so I have some experience dealing with Twitter API.</p>
<p>I&#8217;ve begun to realize more and more that there&#8217;s a lot of great Twitter API resources out there but none are so &#8220;complete&#8221; for PHP/AJAX programmers out there.  It&#8217;s basically a big maze out there for someone who&#8217;s just testing out the Twitter API waters but let me give you some great sites and tips on how to get started with Twitter API using PHP and/or javascript/AJAX.</p>
<p>First off, you will want to start learning how to use the Twitter API via curl.  <a href="http://en.wikipedia.org/wiki/CURL">Curl</a> is basically a linux/PHP command that allows you to retrieve other web pages to the web server.  This would be way your web server actually communicates with Twitter API.</p>
<p>Simply put it, your web server ask for a webpage such as <strong>http://twitter.com/user/statuses</strong> and would get back some of of user statuses in the format of XML and JSON. (Btw, the webpage URL is just made up in this example.)</p>
<p><a href="http://en.wikipedia.org/wiki/XML">XML</a> and <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> are simply how data are formatted so you can easily access it.  I have used both XML and JSON for receiving data and I would have to say to learn both but use JSON if you can as it&#8217;s faster.  (Plus Twitter team also recommends JSON over XML)</p>
<p>So the very first thing I want you to do (if you don&#8217;t know how already), is to learn how to use the Twitter API via Curl.  Try <a href="http://morethanseven.net/2007/01/20/posting-to-twitter-using-php/">this guide on MoreThanSeven.Net</a>.  It should only take you 15-30 minutes to try out the code and get somewhat familiar with how simply Twitter API is and how it works.</p>
<p><strong>Of course, don&#8217;t forget, you need &#8220;Curl&#8221; installed on your web server. </strong> From linux command line, simply do &#8220;yum install curl&#8221; for Fedora/CentOS and probably &#8220;sudo apt-get install curl&#8221; on Ubuntu/Debian.</p>
<p>Once you have mastered that Curl API method, you will want to learn about &#8220;caching&#8221;.  If you don&#8217;t &#8220;cache&#8221; your API requests, you will eventually run out of API requests very quickly and your Twitter app will come to a screeching halt.  You don&#8217;t want that so let&#8217;s learn to &#8220;cache&#8221; the API requests itself.  <a href="http://mark.koli.ch/2009/03/using-the-twitter-api-cache-your-tweets-in-php.html">Mark Kolich has a great guide Twitter Caching which comes with some of the basic API request functions</a>.  Somewhat different from the previous Curl guide but this will help you get some ideas on how to &#8220;cache&#8221; your API requests.</p>
<p>AND/OR you can setup a file caching system that will cache every PHP file in your web server.  This way, if a webpage gets more than 1 hit, it will load super-fast.   You can follow the <a href="http://zedomax.com/blog/2009/01/04/php-diy-how-to-add-caching-to-any-website/">PHP File Caching guide here</a>.  Also note that <em><strong>you can actually include the files cache.php and endcache.php on the header and footer of your file to cache</strong></em> if you don&#8217;t feel like using .htaccess or use non-Apache servers Nginx or Lighttpd like I do.</p>
<p>I am sure after trying all of the above, you will come up with a method to cache all your web pages efficiently as possible.  (This will save you a ton of money on servers if your Twitter app site gets HUGE.)</p>
<p>Another way you can also optimize your server for heavy traffic is to install Lighttpd or Nginx.  See <a href="http://webserverhacks.com/performance-tests/how-to-run-lighttpd-with-wordpress-on-plesk/">how to install Lighttpd here</a> and <a href="http://webserverhacks.com/web-server-hacks/how-to-install-nginx-on-a-wordpress-blog/">how to install Nginx here</a>.</p>
<p>Now you got the basics out of the way,  you will want to learn how to authenticate Twitter users using the OAuth Twitter API.  This is simply a method for authenticating your Twitter users without them typing in their Twitter username/password into your site.  Plus, by using OAuth, you have to benefits of adding &#8220;from MyTwitterAppSite.com&#8221; instead of the default &#8220;from web&#8221; or &#8220;from API&#8221; under tweet signatures.</p>
<p>I&#8217;ve tried a bunch of OAuth guides out there and the best one (that actually worked) is <a href="http://github.com/jmathai/twitter-async">Twitter-Async by Jaisen Mathai</a>.  Btw, don&#8217;t download the source code using the zipped file, download the &#8220;latest&#8221; files under source.  (Or copy and paste every file like I did)  The zipped file comes with older files that don&#8217;t work.</p>
<p>Also, there&#8217;s a PHP 5.2 requirement for Twitter Async.  If you are running PHP 5.1.6 or below, you might want to upgrade before trying.  Btw, PHP 5.2 runs JSON natively and runs like 20 times faster than PHP 5.1.6 so you have every reason to upgrade to PHP 5.2.  If you are running on CentOS, try <a href="http://webserverhacks.com/web-server-hacks/centos-hack-how-to-upgrade-from-php-5-1-6-to-5-2/">my guide on how to upgrade PHP 5.1.6 to 5.2 here</a>.</p>
<p>To check your PHP version, do &#8220;php -v&#8221; at linux command line.</p>
<p>Also, another note on OAuth in general.  You need to set your web server time correctly otherwise Twitter OAuth won&#8217;t communicate with you.  <strong>Make sure your web server has correct date and time!</strong> (I spent about 10 hours trying to figure out why I kept getting failed authentications and my web server time was too fast by 2 hours.)</p>
<p>Twitter-Async also comes with dynamic way of calling API plus ability to send API calls in &#8220;parallel&#8221; or asynchronously.  (This means you can do 5 API calls in 1 API call time, much faster than doing one by one.)  One thing that&#8217;s missing from the API is caching and I think it shouldn&#8217;t be too hard to hack one in.</p>
<p>My advice is to use the OAuth code separately only for logged in users and use simpler &#8220;cached&#8221; Curl library (I use the Mark Kolich&#8217;s Twitter Cacher code for non-users) for visitors that are not logged in.</p>
<p>One more thing, <strong>make sure you call for authorization tokens on OAuth in real-time using javascript</strong>, otherwise you will be requesting tokens from Twitter OAuth on every page load.  (and sometimes that will slow down your whole site.  Instead use something like onClick=&#8221;getTokens()&#8221;)</p>
<p>Whatever the case, you will want to learn the basic of Twitter API I have outlined here and practice it.  Once you get the hang of it, make a good plan of how you will make your overall Twitter app site.</p>
<p>Good luck and I have just provided you with some of the best Twitter API guides/tips so you got nothing to lose!  Feel free to ask me any questions on the comments line.</p>
<p><strong>Extras</strong></p>
<p>I find that you will need to use a lot of Javascript Regex to process twitter tweets and whatnot, here&#8217;s some good guides for that.</p>
<p><a href="http://www.webreference.com/js/column5/backreferences.html">Javascript Regex</a> &#8211; This could work well for matching/replacing strings.  SitePoint also has a great guide on <a href="http://articles.sitepoint.com/print/expressions-javascript">Javascript Regex</a></p>

	Tags: <a href="http://tweethacking.com/tag/ajax/" title="ajax" rel="tag">ajax</a>, <a href="http://tweethacking.com/tag/complete/" title="complete" rel="tag">complete</a>, <a href="http://tweethacking.com/tag/guide/" title="guide" rel="tag">guide</a>, <a href="http://tweethacking.com/tag/how-to/" title="how to" rel="tag">how to</a>, <a href="http://tweethacking.com/tag/javascript/" title="javascript" rel="tag">javascript</a>, <a href="http://tweethacking.com/tag/php/" title="php" rel="tag">php</a>, <a href="http://tweethacking.com/tag/twitter-api/" title="twitter api" rel="tag">twitter api</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://tweethacking.com/twitter-hacks/how-to-make-a-twitter-webcam-security-system/" title="How to Make a Twitter Webcam Security System! (January 3, 2010)">How to Make a Twitter Webcam Security System!</a> (0)</li>
	<li><a href="http://tweethacking.com/twitter-apps/how-to-back-up-your-tweets-on-google-calendar/" title="How to Back Up Your Tweets on Google Calendar! (December 6, 2009)">How to Back Up Your Tweets on Google Calendar!</a> (0)</li>
	<li><a href="http://tweethacking.com/twitter-hacks/blog-diy-how-to-add-hashtags-to-your-blog-rss-feed-tweets/" title="Blog DIY &#8211; How to Add Hashtags to Your Blog RSS Feed Tweets! (March 1, 2010)">Blog DIY &#8211; How to Add Hashtags to Your Blog RSS Feed Tweets!</a> (1)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://tweethacking.com/twitter-hacks/your-complete-howto-guide-to-twitter-api-using-php-and-javascriptajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->