Digg API: Showing latest stories from your site/URL/domain
To do: To show a list of the latest stories from your site/URL/domain using the Digg API.
Solution:
The following PHP code will display the latest 20 stories from the domain of your choice. Thanks a heap to Web Cash’s entry on grabbing a random Digg story as well as the Digg Story API which helped with this code.
<?php
// Takes an array of arguments and returns a query string
// Each parameter's name is its key in the array
function buildQuery ($args) {
$query = '?';
foreach ($args as $key => $val) {
if ( $query != '?' )
$query .= '&';
$query .= $key . '=' . urlencode($val);
}
return $query;
}
// Digg specifies that user agent must be set
ini_set('user_agent', 'My domain stories/1.0');
$baseurl = 'http://services.digg.com';
$endpoint = '/stories';
$url = $baseurl . $endpoint;
// Number of stories to display
$numberStories = 20;
// Your domain of choice - CHANGE THIS
$domainName = 'your.domain.com';
// Parameters to fetch latest articles
$params = array();
$params['appkey'] = 'http://' . $domainName;
$params['domain'] = $domainName;
$params['type'] = 'xml';
$params['count'] = '' . $numberStories;
// The following displays the newest items first
// This can be changed using the parameters in the Digg API
$params['sort'] = 'submit_date-desc';
// Reuse the $url created above
$query = buildQuery($params);
$reqUrl = $url . $query;
// Display the URL. Uncomment for troubleshooting.
// echo $reqUrl . "\n";
$xml = simplexml_load_file($reqUrl);
// Output the stories
for ($i=0; $i<$numberStories;$i++) {
$numberDiggs = intval($xml->story[$i]['diggs']);
echo '<p>';
echo '<h2><a href="' . $xml->story[$i]['href'] . '">'
. $xml->story[$i]->title . '</a></h2>';
// Number of Diggs
echo '<b>Diggs: ' . $xml->story[$i]['diggs'] . ' ' ;
// If you don't have an image for number of diggs,
// then uncomment the following line
for ($j=0; $j<$numberDiggs; $j++) echo '<img src="digg.gif"> ';
echo '</b><br />';
echo 'Date: ' . date('m-d-Y, H:i:s', (int) $xml->story[$i]['submit_date']) . '<br />';
echo 'Link: <a href="' . $xml->story[$i]['link'] . '">'
. $xml->story[$i]['link'] . '</a><br />';
echo 'Topic: ' . $xml->story[$i]->topic['name'] . '<br />';
echo 'Container: ' . $xml->story[$i]->container['name'] . '<br />';
echo 'Desc: ' . $xml->story[$i]->description . "</p>\n";
}
?>
No Comment
No comments yet
Leave a reply