|
<?php |
|
$countryMasterArray = array('ae','ar','at','au','be','bg','br','ca','ch','cn','co','cu','cz','de','eg','fr','gb','gr','hk','hu','id','ie','il','in','it','jp','kr','lt','lv','ma','mx','my','ng','nl','no','nz','ph','pl','pt','ro','rs','ru','sa','se','sg','si','sk','th','tr','tw','ua','us','ve','za'); |
|
$categoryMasterArray = array('business','entertainment','general','health','science','sports','technology'); |
|
define('API_KEY', 'd01247434ac64da3bb65cf98fac7f36d'); |
|
define('API_URI','https://newsapi.org/v2/'); |
|
define('REDIS_HOST','localhost'); |
|
define('REDIS_PORT','6379'); |
|
header('Content-Type: application/json'); |
|
if($_REQUEST){ |
|
$country = isset($_REQUEST['country']) ? $_REQUEST['country'] : ''; |
|
$category = isset($_REQUEST['category']) ? $_REQUEST['category'] : ''; |
|
$keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : ''; |
|
if($country && $country!=''){ |
|
if(in_array($country, $countryMasterArray)){ |
|
$getData['country'] = $country; |
|
}else{ |
|
$getData['country']=''; |
|
$data['status']=0110; |
|
$data['msg']='Please provide country in the 2-letter ISO 3166-1 code, Example are showing in the country object response'; |
|
$data['country'] = $countryMasterArray; |
|
print_r(json_encode($data)); |
|
die(); |
|
} |
|
} |
|
if($category && $category!=''){ |
|
if(in_array($category, $categoryMasterArray)){ |
|
$getData['category'] = $category; |
|
}else{ |
|
$getData['category']=''; |
|
$data['status']=0110; |
|
$data['msg']='Please provide category, Example are showing in the category object response'; |
|
$data['category'] = $categoryMasterArray; |
|
print_r(json_encode($data)); |
|
die(); |
|
} |
|
} |
|
if($keyword!=''){ |
|
$type='everything'; |
|
$getData['keyword'] = $keyword; |
|
$resultNew = GetData($type,$getData); |
|
$OutPutData['status']['code'] = 200; |
|
$OutPutData['status']['msg'] = 'success'; |
|
$OutPutData['data'] = $resultNew; |
|
print_r(json_encode($OutPutData)); |
|
die(); |
|
}else{ |
|
$getData['keyword']=''; |
|
$type='top-headlines'; |
|
$resultNew = GetData($type,$getData); |
|
$OutPutData['status']['code'] = 200; |
|
$OutPutData['status']['msg'] = 'success'; |
|
$OutPutData['data'] = $resultNew; |
|
print_r(json_encode($OutPutData)); |
|
die(); |
|
} |
|
} |
|
// function to get the data from redis if present oterwise get from source and save it in redis |
|
function GetData($type,$getData){ |
|
$redis = new Redis(); |
|
$redis->connect(REDIS_HOST, REDIS_PORT); |
|
$redis->setOption(Redis::OPT_PREFIX, 'NewsApi:'); |
|
$redisKey = md5($type.'--'.$getData['country'].'--'.$getData['category'].'--'.$getData['keyword']); |
|
if(!$redis->exists($redisKey)){ |
|
// Get data from source and save it in redis |
|
$qurStr='?apiKey='.API_KEY; |
|
if($getData['keyword'] && $getData['keyword']!=''){ |
|
$qurStr .= '&q='.$getData['keyword']; |
|
} |
|
if($getData['country'] && $getData['country']!=''){ |
|
$qurStr .= '&country='.$getData['country']; |
|
} |
|
if($getData['category'] && $getData['category']!=''){ |
|
$qurStr .= '&category='.$getData['category']; |
|
} |
|
$uri = API_URI.$type.$qurStr; |
|
$resultNew = getApiResponseByCurl($uri); |
|
$masterNews = array(); |
|
foreach($resultNew['articles'] as $news){ |
|
$data=array(); |
|
$data['country'] = $getData['country']; |
|
$data['category'] = $getData['category']; |
|
$data['keyword'] = $getData['keyword']; |
|
$data['Title'] = $news['title']; |
|
$data['url'] = $news['url']; |
|
$data['Description'] = TrimContent($news['content'],100); |
|
array_push($masterNews, $data); |
|
} |
|
$redis->set($redisKey, json_encode($masterNews)); |
|
$redis->expire($redisKey,600); |
|
} |
|
$data = $redis->get($redisKey); |
|
return json_decode($data); |
|
} |
|
// function to trim content to first 100 words not char but response is smaller then 100 words now using char . |
|
function TrimContent($phrase, $max_words) { |
|
// this code is to trim the |
|
// $phrase_array = explode(' ',$phrase); |
|
// if(count($phrase_array) > $max_words && $max_words > 0) |
|
// $phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'...'; |
|
// return $phrase; |
|
$pos=strpos($phrase, ' ', $max_words); |
|
return substr($phrase,0,$pos ); |
|
} |
|
// function to call the newsapi.org end point with curl |
|
function getApiResponseByCurl($url=''){ |
|
$result = array(); |
|
if(!empty($url)){ |
|
$ch = curl_init(); |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
curl_setopt($ch, CURLOPT_URL,$url); |
|
$result=curl_exec($ch); |
|
curl_close($ch); |
|
$result = json_decode($result,true); |
|
return $result; |
|
} |
|
return $result; |
|
} |
|
|
|
?> |
Server is closed.