<?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>Website Design Sheffield - Mark Goddard &#187; twitpic</title>
	<atom:link href="http://blog.0100.tv/tag/twitpic/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.0100.tv</link>
	<description>Web Design &#38; Development blog</description>
	<lastBuildDate>Sat, 02 Oct 2010 09:47:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>UPDATED: Using Twitter and TwitPic, the API&#8217;s</title>
		<link>http://blog.0100.tv/2009/05/using-twitter-and-twitpic-the-apis/</link>
		<comments>http://blog.0100.tv/2009/05/using-twitter-and-twitpic-the-apis/#comments</comments>
		<pubDate>Wed, 06 May 2009 13:57:51 +0000</pubDate>
		<dc:creator>Mark Goddard</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[information]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[photos]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[status]]></category>
		<category><![CDATA[twitpic]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.0100.tv/?p=225</guid>
		<description><![CDATA[UPDATED: I have now added in a few lines to cache the file. This saves on bandwidth and can sometimes speed things up when the twitpic service is running slow, it&#8217;s a simple extension to an already simple script. Most people have become addicted to Twitter, and I&#8217;m no exception. I got an iPhone the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.0100.tv/wp-content/uploads/2009/05/twitpic.png"><img class="alignnone size-full wp-image-379" title="twitpic" src="http://blog.0100.tv/wp-content/uploads/2009/05/twitpic.png" alt="twitpic" width="510" height="160" /></a></p>
<p><strong>UPDATED: I have now added in a few lines to cache the file. This saves on bandwidth and can sometimes speed things up when the twitpic service is running slow, it&#8217;s a simple extension to an already simple script.</strong></p>
<p>Most people have become addicted to Twitter, and I&#8217;m no exception.</p>
<p>I got an iPhone the other week and <a href="http://twitter.com/0100">have been posting regularly</a>, but there is one thing that is very nice, and that&#8217;s the API built into Twitter. It&#8217;s quite a nice feature, and one that I have been getting to grips with recently.</p>
<p>I decided I wanted to show my Twitter updates on the new website I have been building and because Twitter supports JSON feeds, I thought I&#8217;d have a crack at using jQuery to show these posts.</p>
<p>It&#8217;s something that&#8217;s actually quite easy. Using jQuery&#8217;s AJAX function $.getJSON it meant I could grab the feed and format it how I wished. First off make sure you have included the latest version of jQuery into your website.</p>
<pre class="code"><code class="javascript">&lt;<span class="start-tag">script</span><span class="attribute-name"> type</span>=<span class="attribute-value">"text/javascript" </span><span class="attribute-name">src</span>=<span class="attribute-value">"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"</span>&gt;&lt;/<span class="end-tag">script</span>&gt;</code></pre>
<p>This needs to go where scripts usually reside. In your HTML &lt;head&gt; tag. You will also need to wrap all the below code in the jQuery documen.ready() function.</p>
<p>In this example I only wanted to grab one result so I started off with&#8230;<span id="more-225"></span></p>
<pre class="code"><code class="javascript">$(document).ready(function() {
var userName = "0100";
	$.getJSON("http://twitter.com/statuses/user_timeline/"+ userName +".json?count=1&amp;callback=?", function(json){
 		var lastTweet = json[0].text;
		var tweetTime = json[0].created_at;
		$("#twitterTxt").html(lastTweet);
		$("#twitterTime").html(tweetTime);
	});
});</code></pre>
<p>That&#8217;s it! All nine lines of it. All you need to do it make two divs, one with the ID twitterTxt and the other with the ID twitterTime. You also need to change your username in the variable too.</p>
<p>The only problem with this is the dates. It&#8217;s not &#8220;twitter timed&#8221; at all. We want the &#8220;posted 1 minute ago&#8221; text we all look for on twitter. Luckily, twitter already wrote a function for this called <a href="http://blog.0100.tv/wp-content/uploads/2009/05/relative_time.js">relative_time()</a> which you <a href="http://blog.0100.tv/wp-content/uploads/2009/05/relative_time.js">can download and use</a>. All you have to do is wrap the tweetTime var with the function like:</p>
<pre class="code"><code class="javascript">		var tweetTime = relative_time(json[0].created_at);</code></pre>
<p>Just make sure you copy in the function to your javascript or include it into the page.</p>
<h2>TwitPic API, displaying images</h2>
<p>After we have our twitter updates showing on our page we can now show the TwitPic photos too! Every time you post a photo through your twitter updates we can show this on the page as a link. I only wanted to show one image but for this example I have written the code to show as many images as TwitPic allows. I&#8217;m not sure of the actual limit but I think ten is a good number. For this example you need to have PHP 5 with <a href="http://us2.php.net/manual/en/book.simplexml.php">SimpleXML</a>. It&#8217;s a brilliant function and something which makes using RSS feeds with PHP very easy.</p>
<p>Once again it&#8217;s only around ten lines of code, but the results are great.<br />
We will need to start by looking at the format of the RSS feed. Open up http://twitpic.com/photos/0100/feed.rss and analyse it. (view the source if you just see a formatted page in firefox).</p>
<p>We start off using the <a href="http://us.php.net/simplexml_load_file">simplexml_load_file()</a> function in PHP to grab the actual feed:</p>
<pre class="code"><code class="php">$username = "0100";
$yourRSS = "http://twitpic.com/photos/".$username."/feed.rss";
$xml = simplexml_load_file($yourRSS, 'SimpleXMLElement', LIBXML_NOCDATA);</code></pre>
<p>Nothing too difficult there except the option <strong>LIBXML_NOCDATA</strong>. TwitPic gives us the data as CDATA. This option simply strips out the uneeded code.</p>
<p>We then need to grab the actual image a link. Note: TwitPic&#8217;s TOS state you must link back to the original TwitPic page, which is fair enough.</p>
<pre class="code"><code class="php">$link = str_replace($username.": &lt;br&gt;","",$xml-&gt;channel-&gt;item[$i]-&gt;description);</code></pre>
<p>This simply points to the SimpleXML object and grabs the description entry in the RSS. This is placed in a for() loop to loop through the entries. We also have to replace the username and page break that TwitPic outputs, it looks silly otherwise. All we have to do is echo that out and we are done!</p>
<p>To save you all time I have uploaded a full PHP script for you to use. It&#8217;s very easy and only requires you to change a few parameters. It will then echo out a unordered list for you to use in your site!</p>
<p><strong>Note: There is now an <a href="http://blog.0100.tv/wp-content/uploads/2009/05/twitpicall2.txt">updated version of the script that you can download</a>.</strong></p>
<p><strong><span style="text-decoration: line-through;">Download the full PHP file <a href="http://blog.0100.tv/wp-content/uploads/2009/05/twitpicall1.txt">TwitPic API &#8211; Simple XML PHP</a></span>. You can also <a href="http://0100.tv/dev/new/twitpicAll.php">see a demo of the output</a>.</strong></p>
<p>Hope this all helped you to use Twitters and TwitPic&#8217;s API&#8217;s!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.0100.tv/2009/05/using-twitter-and-twitpic-the-apis/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

