$val) {
if ($query != '?') {
$query .= '&';
}
$query .= $key . '=' . urlencode($val);
}
return $query;
}
ini_set('user_agent', 'RandomDiggStory/1.0');
// Build the request parameters
$params = array();
$params['appkey'] = 'http://www.mydomain.com';
$params['type'] = 'xml';
$params['count'] = '0';
// Returns the formatted query string
$query = buildQuery($params);
// Change 'topic' to point to your desired topic
$topic = 'programming';
$baseurl = 'http://services.digg.com';
$endpoint = '/stories/topic/' . $topic;
$url = $baseurl . $endpoint;
$reqUrl = $url . $query;
$xml = simplexml_load_file($reqUrl);
// Save the total number of stories in the topic
$total = (int) $xml['total'];
// New query to fetch one random article
$params = array();
$params['appkey'] = 'http://www.mydomain.com';
$params['type'] = 'xml';
$params['count'] = '1';
$params['offset'] = rand(0, $total);
// Reuse the $url created above
$query = buildQuery($params);
$reqUrl = $url . $query;
$xml = simplexml_load_file($reqUrl);
// Add your outputting/formatting here. This is just an example.
echo 'Title: ' . $xml->story->title . '
';
echo 'Date: ' . date('m-d-Y, H:i:s', (int) $xml->story['submit_date']) . '
';
echo 'Link: ' . $xml->story['link'] . '
';
echo 'Desc: ' . $xml->story->description . '
';
?>