Today in this article I will show how to convert your Blogspot or WordPress data make JSON API using PHP. this can help you create a mobile application in your blog.

Today every blogger needs her blog’s user able to read & use her blog on their smart phone. so you should make an app for your blog user. try this you can easily create a mobile application for your blog.

convert blog feed

Let’s Start Now

Now we create a PHP function this function can genaret you blog feed to JSON.
First Create Feed.php file Using any Editor Copy and past the flowing code you need to host this file on your own server.


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;

}

echo json_encode($json);



Now this file will genaret JSON output data.
Type in your browser http://yourdomain.com/feed.php see the result


{
title: "Android Studio Application Development Tutorials & Tips or Solutions"
description: "developing android apps using android studio, tips,tutorial,source,problem & solution for who loves developing android apps using android studio"
link: "http://androidstudiodevelop.blogspot.com/"
item: [100]
0:  {
title: "How to build Bangla language support android application "
}-
1:  {
description: "Today in this article I'm writing for who want to add Bangla language support to their android application.this project has a two extra file"
}-
2:  {
pubdate: "Wed, 13 Aug 2014 02:12:00 +0000"
}-
3:  {
guid: "tag:blogger.com,1999:blog-817035169776506119.post-5623054339772406832"
}-
4:  {
title: "Setting Up Wearable Apps Development Environment In AndroidStudio "
}

Now we need to change our URL structure http://yourdomain.com/feed.php  to change. http://yourdomain.com/feed.json
Because JSON API can need .json
To change this we need simple create a new PHP array out of it. add the flowing line file


//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);

Now type again http://yourdomain.com/feed.json in a browser.
Finally, .json a file will be created.
For API testing purpose, you can use chrome extension advance RestClient Application

In next Article, I will Show How to Create An Android Application Using this JSON API
Final feed.php File Hosted On Github 

Leave a Reply

avatar
  Subscribe  
Notify of