Old news class in PHP.

This code was used to read data from a newspaper headlines witch is no longer available, the client interface was done in Extjs.


class NewsFeeds{
function stripJson($json){
$strip = json_decode($json, true);
$res_arr = '';
foreach ($strip as $row){
if($row['entries'] != null){
foreach($row['entries'] as $data) {
$arr = array('titulo'=>$row['title'], 'title'=>$data['title'], 'publishedDate'=>$data['publishedDate'],'content'=> $data['content'], 'categories' =>$data['categories'][0]);
$res_arr .= Util::iif($res_arr == '', json_encode($arr, JSON_FORCE_OBJECT),','.json_encode($arr, JSON_FORCE_OBJECT));//concatenar o array numa string
}
}
}
return $res_arr;
}
//function to create the file and write to it
function writeToFile($result, $filename, $extension) {
$myFile = $filename.".".$extension;
if(file_exists($myFile)){
unlink($myFile);
}
$fn = fopen($myFile, 'w') or die('Error opening the file');
fwrite($fn, $result);
fclose($fn);
}

function getContentFeeds($url){
return file_get_contents($url);//function to get the contents of a given url
}
}

$Feeds = new NewsFeeds();

$result = $Feeds->getContentFeeds('http://www.google.com/uds/Gfeeds?callback=google.feeds.Feed.RawCompletion&context=0&num=25&hl=pt_PT&output=json&q=http%3A%2F%2Ffeeds.jn.pt%2FJN-ULTIMAS&key=notsupplied&v=1.0');
$result = str_replace("google.feeds.Feed.RawCompletion('0',", '', $result);
$result = str_replace("/* callback */", '', $result);
$result = str_replace(", 200, null, 200)", '', $result);

if (Util::isJson($result)){
echo $callback.'({"feed":['.$Feeds->stripJson($result).']});';//send the data to extjs
$Feeds->writeToFile($callback.'({"root":['.$Feeds->stripJson($result).']});',"feeds","json"); //write to file
} else {
echo 'The Json not valid. ';
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s