<?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>My Workground</title>
	<atom:link href="http://wg.vinayraikar.com/b/feed/" rel="self" type="application/rss+xml" />
	<link>http://wg.vinayraikar.com/b</link>
	<description>my playground</description>
	<lastBuildDate>Sun, 15 Mar 2009 12:21:29 +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>Write a twitter application in PHP</title>
		<link>http://wg.vinayraikar.com/b/2009/03/write-a-twitter-application-in-php/</link>
		<comments>http://wg.vinayraikar.com/b/2009/03/write-a-twitter-application-in-php/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 12:21:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[application]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[apps]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://wg.vinayraikar.com/b/?p=13</guid>
		<description><![CDATA[I have been addicted to twitter, so much that (in recent days) there hasn&#8217;t been a day when I have stayed away from twitter [Except for one day, when a friend of mine challenged me to stay away &#38; yes, I won it! But that friend is yet to do what he was supposed to [...]]]></description>
			<content:encoded><![CDATA[<p>I have been addicted to twitter, so much that (in recent days) there hasn&#8217;t been a day when I have stayed away from twitter [Except for one day, when a friend of mine challenged me to stay away &amp; yes, I won it! But that friend is yet to do what he was supposed to do after losing.].</p>
<p>I also have been using the twitter API to write simple twitter apps at <a title="application bin" href="http://appb.in" target="_blank">appb.in</a>. Felt like, sharing some gyaan on how to write a twitter app in PHP:purpose of this post. Target audience: a n00b or a beginner. Here we start&#8230;</p>
<p> </p>
<p>First, If you haven&#8217;t gone through the  <a title="twitter rest api documentation" href="http://apiwiki.twitter.com/REST+API+Documentation" target="_blank">Twitter API wiki,</a>  skim through it. You will get an idea about what we are interacting with. Keep it handy.  </p>
<p>Have <a title="PHP manual" href="http://www.php.net/manual/en/" target="_blank">PHP manual</a> open for reference. [I have the .chm version of the manual open whenever I do some coding.]</p>
<p>To receive (or send) any info from(to) twitter, you will have to do a POST  or GET  request to twitter. There are different URL&#8217;s(methods) for different operations as mentioned in the API documentation. Based upon the application that you are developing, you will use appropriate API methods.</p>
<p>These methods return <a rel="nofollow" href="http://en.wikipedia.org/wiki/XML"><span>XML</span></a>, <a rel="nofollow" href="http://en.wikipedia.org/wiki/Json"><span>JSON</span></a>,  <a rel="nofollow" href="http://en.wikipedia.org/wiki/RSS"><span>RSS</span></a> and <a rel="nofollow" href="http://en.wikipedia.org/wiki/Atom_%28standard%29">Atom</a> depending upon the request that you send <em>(http://twitter.com/statuses/public_timeline.format here  format can be xml or json, etc.,)</em><em> . </em>I generally use  XML, but have also used JSON. PHP has functions that help you easily read these formats, so no worries. Next task is to parse it &amp; interpret the information. Then we need to do the processing as per the applications needs.</p>
<p>Then comes the last but the most challenging part, presenting the information to the user. AJAXy stuff are popular these days, so learn a little bit of jQuery( because there isn&#8217;t any other easy way of ajaxyfying that I know of) if you want. I&#8217;m not dealing with AJAX here, so let&#8217;s keep it aside. You can easily present the information in a HTML page with some neat stying using css.</p>
<p> </p>
<p>Now, let us go through a simple app called <a title="commontweeps" href="http://wg.vinayraikar.com/apps/commontweeps/" target="_blank">commontweeps</a>, which lets you find common friends between two people.  This is non-AJAXy and uses XML format.</p>
<p>From the API documentation, to get a list of friends, the URL that we need to use is &#8220;http://twitter.com/statuses/friends/username.xml&#8221;.  You now need to query twitter for the info. We can do that with a simple function,</p>
<p> <code><br />
function getfilecontents($url){<br />
$ch = curl_init();<br />
curl_setopt($ch, CURLOPT_URL, $url);<br />
curl_setopt($ch, CURLOPT_HEADER, 0);<br />
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);<br />
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');<br />
$res=curl_exec($ch);<br />
$info=curl_getinfo($ch);<br />
curl_close($ch);<br />
if($res === false || $info['http_code'] !=200 ) { $ret=array(false,$info['http_code'],$res);}<br />
else{$ret=array(true,$info['http_code'],$res);}<br />
return $ret;<br />
}<br />
</code></p>
<p> It is a function that uses cURL to fetch data from the url being passed to it. This works without replacing the &#8216;username:password&#8217; (with your username and password) but considering that twitter API has <a title="twitter rate limiting" href="http://apiwiki.twitter.com/REST-API-Documentation#RateLimiting" target="_blank">rate limiting</a>, you can get your username whitelisted (i.e, if you are planning to release an app for friends/public).</p>
<p>Next is, how to use this function. Let us write a small piece of code, just to look at the response from twitter and how we can use it. </p>
<p><a title="twitter response demo " href="http://wg.vinayraikar.com/apps/commontweeps/asktwitter.php.txt" target="_blank">http://wg.vinayraikar.com/apps/commontweeps/asktwitter.php.txt</a>  This script will read, parse the xml and show results like <a title="result from twitter" href="http://wg.vinayraikar.com/apps/commontweeps/asktwitter.php" target="_blank">this</a>.</p>
<p>Now, I guess we are clear with the querying-twitter part. Next, we need to do some processing to get our desired results. </p>
<p>Twitter returns only 100 friends per call, so if  someone has more than 100 friends then you should query more than once with a <em><span style="text-decoration: underline;">page</span><span style="font-style: normal;">  parameter in the url. We will do that in our application with a while loop until no more &#8216;user&#8217;s are returned. The code goes something like this&#8230;</span></em></p>
<p><code>$xml[$page] = new SimpleXMLElement($fi[2]);<br />
$user1count=0;<br />
while(isset($xml[$page]-&gt;user[0])){<br />
      foreach ($xml[$page]-&gt;user as $user) {<br />
         $userdet[(string)$user-&gt;screen_name]=array( 'screen_name'=&gt;  (string)$user-&gt;screen_name, 'location'=&gt;(string)$user-&gt;location, 'description'=&gt;(string)$user-&gt; description,  'profile_image_url'=&gt; (string)$user-&gt; profile_image_url, 'url'=&gt;(string)$user-&gt; url, 'name'=&gt;(string)$user-&gt;name );<br />
         $user1details[$user1count]= (string)$user-&gt;screen_name;<br />
         $user1count++;<br />
       } <br />
      $page++;<br />
      $fi=getfilecontents($friendsurl.$username1.'.xml?page='.$page);<br />
      if($fi[0]===false){<br />
           echo "Error : ".$fi[1];<br />
           $err=new SimpleXMLElement($fi[2]);<br />
           echo "  ".$err-&gt;error."";<br />
           // echo "<!--".$fi[2]."-->";<br />
           die();<br />
      }<br />
   $xml[$page] = new SimpleXMLElement($fi[2]);<br />
}</code></p>
<p>This piece of code is doing two things,</p>
<p>One: Collecting userdetails in an array <strong>$userdet </strong>with the username as the key.</p>
<p>Two: Collecting the friends name in an array <strong>$user1details </strong>.</p>
<p>At the end of the loop, $user1details will have a list of all the followers &amp; $userdet will have all the follower details.</p>
<p> </p>
<p>We need to do this for two users and get their friends list. Say, $user1details and $user2details are the arrays for two users, then all you need to get common tweeps is use the <em>array_intersect</em> function:</p>
<p><code>$commonfriends=array_intersect($user1details, $user2details);</code></p>
<p>Now, $commonfriends will have a list of all common friends. [You can do much more magic with different ideas by using other array functions like array_diff, array_merge.]</p>
<p>Next is presentation, the most crucial and difficult task.  Let&#8217;s keep is simple, displaying an unordered list with this piece of code&#8230;</p>
<p><code>$commoncount=count($commonfriends);<br />
if(is_array($commonfriends)){<br />
echo "&lt;p&gt;You have $commoncount friends in common.&lt;/p&gt;";<br />
echo "&lt;ul id='commonlist'&gt;";<br />
foreach($commonfriends as $friend){<br />
echo "&lt;li&gt;";<br />
echo "&lt;span class='nl'&gt;&lt;img class='img' src='".$userdet[$friend]['profile_image_url']."'&gt;&lt;/img&gt;&lt;br/&gt;";<br />
echo "&lt;a class='screen-name' href='".$userdet[$friend]['screen_name']."'&gt;".$userdet[$friend]['screen_name']."&lt;/a&gt;&lt;/span&gt;";<br />
echo "&lt;span class='nr'&gt; &lt;p class='desc'&gt; ".$userdet[$friend]['description']." | &lt;a href='".$userdet[$friend]['url']."' title='".$userdet[$friend]['url']."'&gt;web&lt;/a&gt; &lt;/p&gt;&lt;/span&gt;";<br />
echo "&lt;/li&gt;";<br />
}<br />
echo "&lt;/ul&gt;";<br />
}<br />
else{echo "&lt;p&gt;No common Tweeps!&lt;/p&gt;";}<br />
}</code></p>
<p>You can now do some styling using CSS to give it a nice look.</p>
<p>Well, we are done with it, just that we didn&#8217;t talk about how to get the input from the user. We need to present a form to the user and then get his input. I hope, you will get it from the final working code.</p>
<p>So, our final <a title="commontweeps app" href="http://wg.vinayraikar.com/apps/commontweeps/" target="_blank">commontweeps </a>code is <a title="commontweeps code" href="http://wg.vinayraikar.com/apps/commontweeps/commontweeps.php.txt" target="_blank">here</a>. The code is more like an procedural hack, lacking proper error handling, cacheing &amp; stuff but it&#8217;s one of our first steps right?</p>
<p>Based upon the application idea, you will have to use different API methods and request types (GET or POST) as per the API documentation.</p>
<p>There are <a title="php libraries listed on apiwiki.twitter" href="http://apiwiki.twitter.com/Libraries#PHP" target="_blank">PHP libraries</a> for twitter like <a title="arc90 twitter PHP library" href="http://lab.arc90.com/2008/06/php_twitter_api_client.php" target="_blank">this</a> one by arc90 that will ease your task. </p>
<p>OK. That&#8217;s it!   Hope I  have been clear and not confusing {let me know in the comments, there&#8217;s always scope for improvement  :D}.  You can also catch me on twitter as <a title="twitter.com/vsr" href="http://twitter.com/vsr" target="_blank">@vsr</a>  .  Happy tweeting!!</p>
]]></content:encoded>
			<wfw:commentRss>http://wg.vinayraikar.com/b/2009/03/write-a-twitter-application-in-php/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>a new start, once again&#8230;</title>
		<link>http://wg.vinayraikar.com/b/2008/12/a-new-start-once-again/</link>
		<comments>http://wg.vinayraikar.com/b/2008/12/a-new-start-once-again/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 11:45:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://wg.vinayraikar.com/b/?p=3</guid>
		<description><![CDATA[I had a technical blog and I lost it. I mean, I lost the text,comments &#38; stuff. Who do I blame ? Myself, for sure, for doing some seriously stupid things! I just don&#8217;t have the patience to bring it back to life, so here I do a fresh install of wordpress.
So, you might miss [...]]]></description>
			<content:encoded><![CDATA[<p>I had a technical blog and I lost it. I mean, I lost the text,comments &amp; stuff. Who do I blame ? Myself, for sure, for doing some seriously stupid things! I just don&#8217;t have the patience to bring it back to life, so here I do a fresh install of wordpress.</p>
<p>So, you might miss a few posts, but here is a short snapshot of my few lost posts (just 4 posts ;P ).</p>
<ul>
<li><a href="http://wg.vinayraikar.com/apps/Appconverter/" target="_blank">Appconverter</a>: converts between units.</li>
<li><a href="http://wg.vinayraikar.com/apps/sMe/" target="_blank">Simple Math Executor</a>: Do some simple math calculations.</li>
<li>uload: A script to upload files remotely. Have removed it coz. there are a bunch of people who don&#8217;t care about doing as simple a task as changing password. Security, is my concern.</li>
<li>Project Homepage: Thought of doing some web design, so this project. The first and still the only one is <a href="http://wg.vinayraikar.com/apps/homepage/apod/" target="_blank">apod</a>.</li>
</ul>
<p>There is also this <a title="app bin" href="http://appb.in" target="_blank">appb.in</a> where I dump in some <em>at least presentable</em> apps.</p>
<p>I&#8217;m quite active on twitter, so may be you can catch me there.</p>
]]></content:encoded>
			<wfw:commentRss>http://wg.vinayraikar.com/b/2008/12/a-new-start-once-again/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
