Last Update November 8, 2020: We improved the overall article.

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;

Advertisment

$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 

Advertisement
Previous articleHow to build bangla language support android application
Next articlehow to add google analytics code on OpenX delivery engine
Abdullah Al Mahamud has always been fascinated by the world and is a writer because he wants to share her love of travel with others. He has written for thedevline since 2016, specializing in modern real-life writing about fashion, arts & culture, technology tips, and more! When not writing articles for us at The dev line (which doesn't happen often!), he enjoys exploring both scenic beauty around the globe as well as intellectual topics that help other people become happier. like productivity hacks or ways to save on your grocery bill.