SEARCH

// developer.daylife.com


A Lightweight PHP Client Library

The PHP sample below provides a sample client library to consume the daylife APIs. We have demonstrated integration with a single API call - search_getRelatedArticles.

Download it here

Test it here

The publicap.php code provides hooks to receive json encoded response objects from the daylife API. It does NOT use the XML encoded response.

Here is some sample code to demonstrate how to make the API call using publicapi.php, and loop through a set of headlines returned by publicapi.php. Make sure to replace the path to your publicapi.php file, your accesskey and your sharedsecret.


include_once('***PATH_TO_PUBLICAPI.PHP***/publicapi.php');

#configure the daylife api server url here
$daylife_server = "freeapi.daylife.com";
$protocol = "jsonrest";
$version = "4.10";
$publicapi_access_url = "http://" . $daylife_server . "/" . $protocol . "/publicapi/" . $version . "/";

#configure your api credentials here
$accesskey = "***YOUR_ACCESSKEY***";
$sharedsecret = "***YOUR SHAREDSECRET***";

#parse input params
$query = (isset($_GET['query'])) ? $_GET['query'] : 'george bush';
$limit = (isset($_GET['limit'])) ? $_GET['limit'] : 10;


#demoing search_getCounts
$count = search_getCounts($query, $source_filter_id);
if ($count) echo 'Number of mentions in last week=' . $count .' 
'; #demoing search_getRelatedArticles $articles = search_getRelatedArticles($query, $limit); #rendering sample HTML renderHTML($query, $articles); function search_getRelatedArticles($query, $limit) { global $sharedsecret, $accesskey, $publicapi_access_url; $now_s = time(); #get handle to public api $publicapi = new PublicApi($publicapi_access_url, $accesskey, $sharedsecret); #build params for article search $params = array(); $params['query'] = $query; $params['sort'] = 'date'; $params['offset'] = 0; $params['limit'] = $limit; $params['end_time'] = $now_s; #news in last 1 week $params['start_time'] = $now_s - (7 * 86400); #make the API call $results = $publicapi->search_getRelatedArticles($params); #check for response and error codes if ((isset($results['response']['code'])) && (($results['response']['code'] == 2001) || ($results['response']['code'] === 2002)) && (isset($results['response']['payload']['article']))) return $results['response']['payload']['article']; else return array(); } function renderHTML($query, $articles) { echo ''; $single_article_html = '
%ARTICLE_HEADLINE%
%ARTICLE_EXCERPT%
published at %ARTICLE_TIMESTAMP% by %ARTICLE_SOURCE_NAME%

'; foreach($articles as $article) { $html = $single_article_html; $html = str_replace("%ARTICLE_HEADLINE%", $article['headline'], $html); $html = str_replace("%ARTICLE_EXCERPT%", $article['excerpt'], $html); $html = str_replace("%ARTICLE_URL%", $article['url'], $html); $html = str_replace("%ARTICLE_TIMESTAMP%", date("Y-m-d H:i:s",$article['timestamp_epoch']), $html); #this prints time in the local timezone of your server $html = str_replace("%ARTICLE_SOURCE_NAME%", $article['source']['name'], $html); $html = str_replace("%ARTICLE_SOURCE_URL%", $article['source']['url'], $html); echo $html; } echo ''; }
AttachmentSize
publicapi.zip2.17 KB