Skip to content

Instantly share code, notes, and snippets.

@aamsuzon
Created August 15, 2014 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aamsuzon/7defb84093359ad39f71 to your computer and use it in GitHub Desktop.
Save aamsuzon/7defb84093359ad39f71 to your computer and use it in GitHub Desktop.
How to convert blog feed in Json API using php
<?php
header('Content-Type: application/json; Charset=UTF-8');
$feed = new DOMDocument();
$feed->load('http://androidstudiodevelop.blogspot.com/feeds/posts/default?alt=rss');// put your feed url here
$json = array();
$json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');
$json['item'] = array();
$i = 0;
foreach($items as $item) {
$title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
$guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;
$json['item'][$i++]['title'] = $title;
$json['item'][$i++]['description'] = $description;
$json['item'][$i++]['pubdate'] = $pubDate;
$json['item'][$i++]['guid'] = $guid;
//create a new array out of it
file_put_contents('feed.json', json_encode($json));
$json_string = json_encode($json);
$file = 'feed.json';
file_put_contents($file, $json_string);
}
echo json_encode($json);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment