<?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>Think2Loud &#187; social media</title> <atom:link href="http://think2loud.com/tag/social-media/feed/" rel="self" type="application/rss+xml" /><link>http://think2loud.com</link> <description>Random thoughts from a few geeks</description> <lastBuildDate>Wed, 04 Jan 2012 01:43:02 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> <item><title>Adding Twitter to your site with jQuery Part 1</title><link>http://think2loud.com/399-adding-twitter-to-your-site-with-jquery-part-1/</link> <comments>http://think2loud.com/399-adding-twitter-to-your-site-with-jquery-part-1/#comments</comments> <pubDate>Wed, 22 Jul 2009 23:17:59 +0000</pubDate> <dc:creator>Jared</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[social media]]></category> <category><![CDATA[Twitter]]></category><guid isPermaLink="false">http://think2loud.com/?p=399</guid> <description><![CDATA[<p>This series of articles will explain how to retrieve information from Twitter using their API, and ways you can display it on your website in an attractive way.  Part one we will go over the basic of the Twitter API and look at some simple code that will print out a list of tweets. <a href="http://think2loud.com/399-adding-twitter-to-your-site-with-jquery-part-1/">Read More</a></p><p><a href="http://think2loud.com/399-adding-twitter-to-your-site-with-jquery-part-1/">Adding Twitter to your site with jQuery Part 1</a></p>]]></description> <content:encoded><![CDATA[<p>For this example I will use my own tweets, feel free to follow me <a target="_blank" href="http://www.twitter.com/jaredharbour" target="_blank">@jaredharbour</a>.</p><p><strong>What is Twitter?</strong></p><p>In recent years, social media has become a huge part of peoples lives.  Sites like MySpace, Facebook and LinkedIn (which I sometimes refer to as ‘The Big Three’) allow people from all over the world connect and share their lives and ideas instantly.  Recently Twitter has become the next big addition to the global conversation.  Twitter is unique among the others in that its only function is to accept and display 140 character “micro-blogs”.  Let me get one thing out of the way before we continue, micro-blogging doesn’t mean every message has to be about taking out the trash and mowing the lawn.  There are 5 million+ Twitter users in the world today, and that number will only get bigger.  Now lets get into some code.</p><p><strong>The Twitter API</strong></p><p>The Twitter API attempts to conform to the principals of <a target="_blank" href="http://en.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">Representational State Transfer</a> or REST.  I&#8217;ll leave it to you to read up on what that means, simply put it means that with the Twitter API you simply need to change the extension to get your results in a different format.</p><div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">http://search.twitter.com/search.format</pre></div></div><p>Since we just want to print out a list of tweets, we will start by using the search function.  The search function will return its results in two formats, XML and JSON.  We will use JSON because in my experience it&#8217;s faster and easier to use than XML.</p><div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">http://search.twitter.com/search.json</pre></div></div><p>For a more in depth look at the search function take a look at the Twitter <a target="_blank" href="http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search" target="_blank">documentation</a>.  Now lets get to the jQuery part.  We get to use one of my favorite methods jQuery gives us, <a target="_blank" href="http://docs.jquery.com/Ajax/jQuery.getJSON" target="_blank">jquery.getJSON</a>.  This handy function will return the JSON via a GET HTTP request, just what the doctor ordered.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span>
    <span style="color: #3366CC;">'http://search.twitter.com/search.json?callback=?&amp;rpp=25&amp;q=from:jaredharbour'</span><span style="color: #339933;">,</span>
    <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// our code to handle the data here</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>That&#8217;s not all though, we need to get at our data.  jQuery makes it easy with the each function.  This will let us loop through the JSON object with ease. In this case, &#8220;tweets&#8221; becomes our array of data. Then we need to check a few things to make sure we are in the section of the object we want.  This logic isn&#8217;t perfect, but it will work for all the major browsers.  The first and second if statements are to check if there are tweets to look at.  I needed them both because it seems IE was unhappy with accessing the first element, because in some cases there isn&#8217;t one.  The third if statement makes sure that what we&#8217;re looking at is in fact a tweet, and not some random piece of data.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> tweets<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tweets.<span style="color: #660066;">length</span> <span style="color: #339933;">!=</span> undefined<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tweets<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> undefined<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tweets<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">created_at</span> <span style="color: #339933;">!=</span> undefined<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>Now we just need to print out some data.  Just a simple for loop and a document.write and we&#8217;re all set.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> tweets.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;div class='tweet'&gt;&quot;</span><span style="color: #339933;">+</span>tweets<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">text</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&lt;/div&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>That&#8217;s all there is to it!  In the next post I will get into formatting the tweets to add more functionality.  Here are some of the topics I will cover later in the series.</p><ul><li>Hyperlinking hash tags and users</li><li>Formatting tweet time and date</li><li>Linking to the Twitter app</li></ul><p>Have a question? Looking for something specific?  Leave a comment and I&#8217;ll get back to you!</p><p><a href="http://think2loud.com/399-adding-twitter-to-your-site-with-jquery-part-1/">Adding Twitter to your site with jQuery Part 1</a></p>]]></content:encoded> <wfw:commentRss>http://think2loud.com/399-adding-twitter-to-your-site-with-jquery-part-1/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> </channel> </rss>
<!-- Served from: think2loud.com @ 2012-02-03 22:01:45 by W3 Total Cache -->
