SEARCH

// developer.daylife.com


A Topic Page Builder

This php sample demonstrates how you can make daypi calls to get stories, images, quotes and connections for a given topic name or a topic id.


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

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

#Input params
$topic_name = (isset($_GET['name'])) ? $_GET['name'] : Null; //'Central Intelligence Agency';
$topic_id = (isset($_GET['topic_id'])) ? $_GET['topic_id'] : '0eWPgqk5gb7px';
$end_time = (isset($_GET['end_time'])) ? $_GET['end_time'] : time();
$start_time  = (isset($_GET['start_time'])) ? $_GET['start_time'] : $end_time - (31 * 86400);
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'date';
$offset = (isset($_GET['offset'])) ? $_GET['offset'] : 0;
$limit = (isset($_GET['limit'])) ? $_GET['limit'] : 5;
$include_scores = (isset($_GET['include_scores'])) ? $_GET['include_scores'] : 1;
$include_images = (isset($_GET['include_images'])) ? $_GET['include_images'] : 1;
$include_publication_images = (isset($_GET['include_publication_images'])) ? $_GET['include_publication_images'] : 1;
$include_topic_type = (isset($_GET['include_topic_type'])) ? $_GET['include_topic_type'] : "PERSON,PLACE,ORGANIZATION";
$include_image = (isset($_GET['include_image'])) ? $_GET['include_image'] : 1;

$topic_info = topic_getInfo($topic_name, $topic_id);

$stories = topic_getRelatedStories($topic_name, $topic_id, $start_time, $end_time, $sort, $offset, $limit, $include_scores);

$images = topic_getRelatedImages($topic_name, $topic_id, $start_time, $end_time, $sort, $offset, $limit, $include_publication_images);

$topics = topic_getRelatedTopics($topic_name, $topic_id, $start_time, $end_time, $sort, $offset, $limit, $include_topic_type, $include_image);

$quotes = topic_getRelatedQuotes($topic_name, $topic_id, $start_time, $end_time, $sort, $offset, $limit);

#rendering sample HTML
renderHTML($topic_info, $stories, $images, $topics, $quotes);


function topic_getInfo($topic_name, $topic_id) {
	global $sharedsecret, $accesskey, $publicapi_access_url;

	#get handle to public api
	$publicapi = new PublicApi($publicapi_access_url, $accesskey, $sharedsecret);

	#build params for article search
	$params = array();
	if (isset($topic_name)) $params['name'] = $topic_name;
	else if (isset($topic_id)) $params['topic_id'] = $topic_id;

	#make the API call
	$results = $publicapi->topic_getInfo($params);

	#check for response and error codes
	if ((isset($results['response']['code']))
			&& ($results['response']['code'] > 0)
			&& (isset($results['response']['payload']['topic'])))
		return $results['response']['payload']['topic'];
	else
		return array();
}


function topic_getRelatedStories($topic_name, $topic_id, $start_time, $end_time, $sort, $offset, $limit, $include_scores) {
	global $sharedsecret, $accesskey, $publicapi_access_url;

	#get handle to public api
	$publicapi = new PublicApi($publicapi_access_url, $accesskey, $sharedsecret);

	#build params for article search
	$params = array();
	if (isset($topic_name)) $params['name'] = $topic_name;
	else if (isset($topic_id)) $params['topic_id'] = $topic_id;
	if (isset($start_time)) $params['start_time'] = $start_time;
	if (isset($end_time)) $params['end_time'] = $end_time;
	if (isset($sort)) $params['sort'] = $sort;
	if (isset($offset)) $params['offset'] = $offset;
	if (isset($limit)) $params['limit'] = $limit;
	if (isset($include_scores)) $params['include_scores'] = $include_scores;

	#make the API call
	$results = $publicapi->topic_getRelatedStories($params);

	#check for response and error codes
	if ((isset($results['response']['code']))
			&& ($results['response']['code'] > 0)
			&& (isset($results['response']['payload']['story'])))
		return $results['response']['payload']['story'];
	else
		return array();
}


function topic_getRelatedImages($topic_name, $topic_id, $start_time, $end_time, $sort, $offset, $limit, $include_publication_images) {
	global $sharedsecret, $accesskey, $publicapi_access_url;

	#get handle to public api
	$publicapi = new PublicApi($publicapi_access_url, $accesskey, $sharedsecret);

	#build params for article search
	$params = array();
	if (isset($topic_name)) $params['name'] = $topic_name;
	else if (isset($topic_id)) $params['topic_id'] = $topic_id;
	if (isset($start_time)) $params['start_time'] = $start_time;
	if (isset($end_time)) $params['end_time'] = $end_time;
	if (isset($sort)) $params['sort'] = $sort;
	if (isset($offset)) $params['offset'] = $offset;
	if (isset($limit)) $params['limit'] = $limit;
	if (isset($include_publication_images)) $params['include_publication_images'] = $include_publication_images;

	#make the API call
	$results = $publicapi->topic_getRelatedImages($params);

	#check for response and error codes
	if ((isset($results['response']['code']))
			&& ($results['response']['code'] > 0)
			&& (isset($results['response']['payload']['image'])))
		return $results['response']['payload']['image'];
	else
		return array();
}


function topic_getRelatedTopics($topic_name, $topic_id, $start_time, $end_time, $sort, $offset, $limit,  $include_topic_type, $include_image) {
	global $sharedsecret, $accesskey, $publicapi_access_url;

	#get handle to public api
	$publicapi = new PublicApi($publicapi_access_url, $accesskey, $sharedsecret);

	#build params for article search
	$params = array();
	if (isset($topic_name)) $params['name'] = $topic_name;
	else if (isset($topic_id)) $params['topic_id'] = $topic_id;
	if (isset($start_time)) $params['start_time'] = $start_time;
	if (isset($end_time)) $params['end_time'] = $end_time;
	if (isset($sort)) $params['sort'] = $sort;
	if (isset($offset)) $params['offset'] = $offset;
	if (isset($limit)) $params['limit'] = $limit;
	if (isset($include_topic_type)) $params['include_topic_type'] = $include_topic_type;
	if (isset($include_image)) $params['include_image'] = $include_image;

	#make the API call
	$results = $publicapi->topic_getRelatedTopics($params);

	#check for response and error codes
	if ((isset($results['response']['code']))
			&& ($results['response']['code'] > 0)
			&& (isset($results['response']['payload']['topic'])))
		return $results['response']['payload']['topic'];
	else
		return array();
}


function topic_getRelatedQuotes($topic_name, $topic_id, $start_time, $end_time, $sort, $offset, $limit) {
	global $sharedsecret, $accesskey, $publicapi_access_url;

	#get handle to public api
	$publicapi = new PublicApi($publicapi_access_url, $accesskey, $sharedsecret);

	#build params for article search
	$params = array();
	if (isset($topic_name)) $params['name'] = $topic_name;
	else if (isset($topic_id)) $params['topic_id'] = $topic_id;
	if (isset($start_time)) $params['start_time'] = $start_time;
	if (isset($end_time)) $params['end_time'] = $end_time;
	if (isset($sort)) $params['sort'] = $sort;
	if (isset($offset)) $params['offset'] = $offset;
	if (isset($limit)) $params['limit'] = $limit;

	#make the API call
	$results = $publicapi->topic_getRelatedQuotes($params);

	#check for response and error codes
	if ((isset($results['response']['code']))
			&& ($results['response']['code'] > 0)
			&& (isset($results['response']['payload']['quote'])))
		return $results['response']['payload']['quote'];
	else
		return array();
}


function renderHTML($info, $stories, $images, $topics, $quotes) {
	echo '';

	if ($info) {
		echo '

Topic Info
'; $info_html = '
%INFO_NAME% %INFO_TYPE%

'; foreach($info as $topic) { $html = $info_html; $html = str_replace("%INFO_NAME%", $topic['name'], $html); $html = str_replace("%INFO_TYPE%", "(" . $topic['type'] . ")", $html); $html = str_replace("%DAYLIFE_URL%", $topic['daylife_url'], $html); echo $html; } } if ($stories) { echo '
Related Stories
'; $single_story_html = '
%STORY_HEADLINE%
%STORY_EXCERPT%
%NEWSDOMAIN%published on %STORY_TIMESTAMP% by %STORY_SOURCE_NAME%

'; foreach($stories as $story) { $html = $single_story_html; $html = str_replace("%STORY_HEADLINE%", $story['headline'], $html); $html = str_replace("%STORY_EXCERPT%", $story['excerpt'], $html); $html = str_replace("%STORY_URL%", $story['url'], $html); $html = str_replace("%STORY_TIMESTAMP%", date("Y-m-d H:i:s",$story['timestamp_epoch']), $html); $html = str_replace("%STORY_SOURCE_NAME%", $story['source']['name'], $html); $html = str_replace("%STORY_SOURCE_URL%", $story['source']['url'], $html); if (isset($story['scores']) && isset($story['scores']['newsdomain'])) $html = str_replace("%NEWSDOMAIN%", "Category: " . $story['scores']['newsdomain'] . "
", $html); else $html = str_replace("%NEWSDOMAIN%", '', $html); echo $html; } } if ($images) { echo '
Related Images
'; $single_image_html = '

published on %IMAGE_TIMESTAMP% by %IMAGE_SOURCE_NAME%

'; foreach($images as $image) { $html = $single_image_html; $html = str_replace("%IMAGE_URL%", $image['url'], $html); $html = str_replace("%IMAGE_THUMB_URL%", $image['thumb_url'], $html); if ($image['caption'] != "NULL") $html = str_replace("%IMAGE_CAPTION%", $image['caption'], $html); else $html = str_replace("%IMAGE_CAPTION%", '', $html); $html = str_replace("%IMAGE_TIMESTAMP%", date("Y-m-d H:i:s",$image['timestamp_epoch']), $html); $html = str_replace("%IMAGE_SOURCE_NAME%", $image['source']['name'], $html); $html = str_replace("%IMAGE_SOURCE_URL%", $image['source']['url'], $html); echo $html; } } if ($topics) { echo '
Related Topics
'; $single_topic_html = '
%INFO_NAME% (%INFO_TYPE%)
%TOPIC_IMAGE%
'; $single_image_html = '
published on %IMAGE_TIMESTAMP% by %IMAGE_SOURCE_NAME%'; foreach($topics as $topic) { $html = $single_topic_html; $html = str_replace("%INFO_NAME%", $topic['name'], $html); $html = str_replace("%INFO_TYPE%", $topic['type'], $html); $html = str_replace("%DAYLIFE_URL%", $topic['daylife_url'], $html); if (isset($topic['image'])) { $image_html = $single_image_html; $image_html = str_replace("%IMAGE_URL%", $topic['image'][0]['url'], $image_html); $image_html = str_replace("%IMAGE_THUMB_URL%", $topic['image'][0]['thumb_url'], $image_html); if ($image['caption'] != "NULL") $image_html = str_replace("%IMAGE_CAPTION%", $topic['image'][0]['caption'], $image_html); else $image_html = str_replace("%IMAGE_CAPTION%", '', $image_html); $image_html = str_replace("%IMAGE_TIMESTAMP%", date("Y-m-d H:i:s",$topic['image'][0]['timestamp_epoch']), $image_html); $image_html = str_replace("%IMAGE_SOURCE_NAME%", $topic['image'][0]['source']['name'], $image_html); $image_html = str_replace("%IMAGE_SOURCE_URL%", $topic['image'][0]['source']['url'], $image_html); $html = str_replace("%TOPIC_IMAGE%", $image_html, $html); } else $html = str_replace("%TOPIC_IMAGE%", "", $html); echo $html; } } if ($quotes) { echo '
Related Quotes
'; $single_quote_html = '
"%QUOTE_TEXT%..."%QUOTE_ATTRIBUTION%
in %QUOTE_ARTICLE%

'; $single_article_html = '%ARTICLE_HEADLINE%
published on %ARTICLE_TIMESTAMP% by %ARTICLE_SOURCE_NAME%'; foreach($quotes as $quote) { $html = $single_quote_html; $html = str_replace("%QUOTE_TEXT%", $quote['quote_text'], $html); if ($quote['name'] != "") $html = str_replace("%QUOTE_ATTRIBUTION%", " said by " . $quote['name'], $html); else $html = str_replace("%QUOTE_ATTRIBUTION%", "", $html); $article_html = $single_article_html; $article_html = str_replace("%ARTICLE_URL%", $quote['article']['url'], $article_html); $article_html = str_replace("%ARTICLE_HEADLINE%", $quote['article']['headline'], $article_html); $article_html = str_replace("%ARTICLE_TIMESTAMP%", date("Y-m-d H:i:s",$quote['article']['timestamp_epoch']), $article_html); $article_html = str_replace("%ARTICLE_SOURCE_NAME%", $quote['source']['name'], $article_html); $article_html = str_replace("%ARTICLE_SOURCE_URL%", $quote['source']['url'], $article_html); $html = str_replace("%QUOTE_ARTICLE%", $article_html, $html); echo $html; } } echo ''; } class JsonDatasource { var $json_datasource_url = null; var $encoding_hook = null; public function __construct($json_datasource_url, $encoding_hook=null) { $this->json_datasource_url = $json_datasource_url; if (!$encoding_hook) { $encoding_hook = new DefaultEncodingHook(); } $this->encoding_hook = $encoding_hook; } /** * Magic method to invoke an json server method. */ public function __call($call, $args=array()) { if(count($args) != 0) { $args=$args[0]; } $file = $this->constructUrl($call, $args); $json = file_get_contents($file); return json_decode($json, true); } public function constructUrl($action, $params) { $url = $this->json_datasource_url . $action; $query = html_entity_decode($this->query_string_encode($params,null)); print($url . '?' . $query . "\n"); return $url . '?' . $query; } public function query_string_encode($data, $key) { $ret = array(); foreach ((array)$data as $k => $v) { if(!empty($key)) { $k = $key; } if (is_array($v) || is_object($v)) { array_push($ret, $this->query_string_encode($v,$k)); } else if (isset($v)) { $this->encoding_hook->encode($k, $v, &$ret); } } $sep = ini_get('arg_separator.output'); return implode($sep, $ret); } private function get_microsecs() { $time = gettimeofday(true); $seconds = $time['sec']; $micros = $time['usec']; return $seconds . "." . $micros; } } class DefaultEncodingHook { public function encode($k, $v, $ret) { array_push($ret, urlencode($k).'='.urlencode($v)); } } class PublicApiEncodingHook { var $access_key = null; var $shared_secret = null; var $id_params = array("topic_id", "quote_id", "image_id", "article_id", "source_id"); var $name_params = array("query", "name"); public function __construct($access_key, $shared_secret) { $this->access_key = $access_key; $this->shared_secret = $shared_secret; } public function encode($k, $v, $ret) { $is_id = false; $is_name = false; if ($is_id = in_array($k, $this->id_params) || $is_name = in_array($k, $this->name_params)) { if ($is_id && is_numeric($v)) { $v = encode_id($v); } $signature = md5($this->access_key . $this->shared_secret . $v); array_push($ret, urlencode("signature").'='.urlencode($signature)); array_push($ret, urlencode("accesskey").'='.urlencode($this->access_key)); } array_push($ret, urlencode($k).'='.urlencode($v)); } } class PublicApi extends JsonDatasource { public function __construct($json_datasource_url, $access_key, $shared_secret) { parent::__construct($json_datasource_url, new PublicApiEncodingHook($access_key, $shared_secret)); } } ?>
AttachmentSize
topic-sample.php_.zip3.24 KB