Render a set of headlines using PHP

Overview

The sample below shows how to render a set of headlines matching a hard coded search term.

Download it here

Test it here

Tricky Bits

  • The signature is generated with a query term
  • The signature should be generated with an unencoded query term, but the call itself should take an encoded query term

Exercises for the reader

  • Limit results to a list of your favorite blogs using source filtering
  • Display the headlines using a fancy javascript headline crawler
  • Step through pages of headlines using <limit> and <offset> parameters

<?php

# Configure the daylife api server url 
	$daylife_server = "freeapi.daylife.com";
	$protocol = "phprest";
	$version = "4.0";
	$publicapi_access_url = "http://" . $daylife_server . "/" . $protocol . "/publicapi/" . $version . "/";
	$method='search_getRelatedArticles';

# Configure your api credentials 
	$accesskey = "8befa1cf0a7c0291613242235638a662";
	$sharedsecret = "2e548ef751397c653752057adcff0c9f";


# specify your query

$query = 'Yankee Stadium';

$url_encoded_query = urlencode($query);

# Build your method signature

# For search_X methods, the Core Input is the query term itself
$signature = md5($accesskey . $sharedsecret . $query);

$sort = 'date';

#Draw from news in last 3 days
$end_time = date(U);
$start_time = $end_time - (3 * 86400);

$composed_url = $publicapi_access_url . $method . '?accesskey=' . $accesskey . '&signature=' . $signature . '&query=' . $url_encoded_query;

# kick off the call
$result = file_get_contents($composed_url);

$relatedArticlesResponse = unserialize($result);

$relatedArticles = $relatedArticlesResponse['response']['payload']['article'];

?>

<html>
	<head>
		<title>
			DayPI headline demonstration
		</title>
	</head>
<body>
<p>The Method Call:<br>

<? print '<a href=http://'. $composed_url . '>' . $composed_url . '</a>'?>
<h1>Recent headlines for <? print $query ?></h1> <ul> <? foreach ($relatedArticles as $article) { $source = $article['source']; $articleListing = "<li><b><a href=" . $article['url'] . ">" ; $articleListing .= $article['headline'] . '</a></b>'; $articleListing .= " " . $article['timestamp'] . ", "; $articleListing .= " from the <i><a href=" .$source['url'] . ">" . $source['name'] . "</a></i> " ; echo $articleListing; } ?> </body> </html>