<?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>Web Cash &#187; syndicate</title>
	<atom:link href="http://www.earn-web-cash.com/tag/syndicate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Digg API: Grabbing a Random Digg Story with PHP</title>
		<link>http://www.earn-web-cash.com/2008/02/16/digg-api-random-story/</link>
		<comments>http://www.earn-web-cash.com/2008/02/16/digg-api-random-story/#comments</comments>
		<pubDate>Sun, 17 Feb 2008 00:46:22 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Online Tools]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[digg]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[syndicate]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/16/digg-api-random-story/</guid>
		<description><![CDATA[I read about the Digg API the other day, and I thought it was about time I played around with it. There is a ton of cool stuff you can do with this. Basically, you send a request to the Digg server and it sends back whatever kind of information you want &#8211; category names, [...]]]></description>
			<content:encoded><![CDATA[<p>I read about the <a href="http://apidoc.digg.com/">Digg API</a> the other day, and I thought it was about time I played around with it.</p>
<p>There is a ton of cool stuff you can do with this.  Basically, you send a request to the Digg server and it sends back whatever kind of information you want &#8211; category names, recent diggs, recent stories, archived stories, etc.  You can read through the API to see everything you can do.</p>
<p>For now, we&#8217;ll focus on one nifty little trick &#8211; grabbing a random story from a given topic.<br />
<span id="more-122"></span></p>
<h5>Digg API Basics: How It Works</h5>
<p>Before we can build our script, we need to know the basics of how the Digg API works.</p>
<p>Your script sends an HTTP GET request to the Digg server.  This has three major parts.  If this is valid, the server sends you a response in one of several designated formats.  You can then parse it into use-able information and display it on your site.</p>
<p>The basic parts of the request are&#8230;</p>
<ul>
<li>Base Digg URL: http://services.digg.com</li>
<li>An <strong>Endpoint</strong>: i.e. /stories/topic/programming &#8211; this determines the type of information you&#8217;re getting back</li>
<li>A <strong>query string</strong>: i.e. ?count=5&#038;offset=20</li>
</ul>
<p>For this tutorial, we&#8217;ll be working with the &#8220;Programming&#8221; topic because, well, it works with my site.  You can look at the list of topics on the API page and tailor this to your needs.</p>
<p>Therefore the URL we&#8217;ll be accessing is: http://services.digg.com/stories/topic/programming</p>
<h5>What Goes In the Query String</h5>
<p>To go along with this, we&#8217;re going to have to build a query string.  There are a few required elements (appkey, type) and some optional ones that we&#8217;ll also use (count, offset).</p>
<p>&#8220;appkey&#8221; is basically an ID tag associated with your application.  Digg uses this for statistical purposes.  You can set this equal to the URI of your website.</p>
<p>&#8220;type&#8221; describes the type of response you&#8217;ll get.  There are four response types, of which two are applicable for use in a PHP script.  We&#8217;re going to focus on the XML response and use SimpleXML to parse the information.  Therefore type should be set to &#8216;xml.&#8217;</p>
<p>&#8220;count&#8221; tells the server how many stories we want to fetch.  &#8220;offset&#8221; tells the server how many stories to skip before it starts fetching them.  The &#8220;offset&#8221; is going to allow us to fetch a random story.</p>
<h5>Building the Query String</h5>
<p>So how do we build the query string?  First, we need to turn each of our values into a parameter in the format <code>key=value</code>.  The value must also be url encoded.  Here&#8217;s how we do that in php.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$appkey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.earn-web-cash.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$appkeyParam</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'appkey='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$appkey</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We can then string together all these variables with ampersands (&#038;) in between.  We then place a ? in the beginning of the query string, attach it to our URL, and we&#8217;re good to go.</p>
<p>I wrote a short function I use to automatically build the query string from an array of values.  I create an empty array, store the value with a key equal to its parameter name, and then pass it through the function.  The function returns the full query string for me to use.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Example array:</span>
<span style="color: #666666; font-style: italic;">//    $params['appkey'] = 'http://www.earn-web-cash.com';</span>
<span style="color: #666666; font-style: italic;">//    $params['type'] = 'xml';</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> buildQuery <span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'?'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'?'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$query</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$query</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>One other minor task we need to do is set up a &#8220;user_agent&#8221; for PHP.  The Digg server wants to know who&#8217;s accessing it, so you need to define a &#8220;user_agent&#8221; value for your instance of PHP with the ini_set command.  You can call your application whatever you want, but it usually follows the format &#8211; Name/Version.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_agent'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'RandomDiggStory/1.0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h5>Firing Off the Request and Getting a Response</h5>
<p>We should now be able to send our request, get a response, and use some information.</p>
<p>To get a random story, we&#8217;re actually going to have to send two requests.  The first is going to allow us to find out how many stories exist in the topic.  The second will actually fetch a random story.</p>
<p>To find out how many stories exist in the topic, we&#8217;re going to create our request with the following information.  The appkey doesn&#8217;t matter, the type will be xml, the count will be 0, and the offset doesn&#8217;t matter.</p>
<p>Once we build the entire url with the query string, we open that location with simplexml_load_file &#8211; and the response will automatically be placed in a SimpleXML object for us.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'appkey'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.earn-web-cash.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'xml'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'count'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  We don't need an offset for this one</span>
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> buildQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://services.digg.com/stories/topic/programming'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$reqUrl</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$reqUrl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$xml</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Need to typecast, or it'll be an object</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Look at the source code to see what you fetched</span></pre></div></div>

<p>The last line (echo $xml->asXML()) isn&#8217;t necessary &#8211; it&#8217;s just to give you an idea of the information you just fetched.  It isn&#8217;t a whole lot &#8211; but we didn&#8217;t ask for a whole lot.</p>
<p>The piece of information we wanted was the total number of stories in our topic.  That&#8217;s the &#8216;total&#8217; attribute of the &#8216;stories&#8217; element.  We fetched it with <code>$xml['total']</code> and stored it in the $total variable.</p>
<h5>Last Step: Fetching the Random Story</h5>
<p>With this information in hand, we can craft a new request and get our random story.  You can copy and paste the same information we used before, but make these changes to the <code>$params</code> array.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'count'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// We want one story</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'offset'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>By generating a random value for offset between 0 and the maximum, we&#8217;re effectively choosing one random story from the available list.</p>
<p>Now run the script again.  At the end, $xml should have one random story stored in it.  The story information is stored in the <code>story</code> child element, and we can access some of the information like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">story</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// the URL</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">story</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit_date'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// timestamp when it was submitted</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">story</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">story</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you browse through the source code of the xml you outputted, you&#8217;ll find all of the essential information about the story.  Now you can format it nicely and add it to your website.</p>
<p>In case you ran into trouble along the way, here&#8217;s the complete <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/random-digg-article.txt' title='Random Digg Article Script'>source code</a> of the script.  You&#8217;ll need to go in and change the topic to your desired topic and set your own appkey, but otherwise it should be all set to go.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/16/digg-api-random-story/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

