Create JSON API Using PHP and MYSQL

By | November 24, 2015

Now these day’s mobiles are getting more dearer than other gadget’s. APP’s are the important factor for making mobile your favorite gadget. Same as apps now these day’s everyone what their website to be mobile friendly and some of them create app also for their website. Now these days mobile app’s are more common for website owner’s and market for app’s are increasing day by day. Some of the app’s are based on the website and fetch the data from the server. So today we will tell you that how you can create the API of your website data so that you can also create the mobile app with the same data. Today’s article is all about Create JSON API using PHP.

Creating API for your website is quite easy and you can achieve it easily with some basic knowledge of PHP. In this tutorial we will create a simple application.

Step’s to do it.

  1. Create Database and add Dummy text to table

First of all we will create the database with the name of myrestaurant and add table “webcuisine” to it.

CREATE TABLE IF NOT EXISTS webcuisine (
  Id int(11) NOT NULL AUTO_INCREMENT,
  Cuisine_name varchar(50),
Date_added varchar(15),
Cuisine_image varchar(150) NOT NULL,
Status varchar(1),
  PRIMARY KEY (Id)
);

When you run the above code it will create a table named webcuisine. After creating the table insert some values to the table by using any form or directly through the mysql commands. Here we will add some of the dummy text for you using the mysql commands.

insert into webcuisine (Cuisine_name,Date_added,Cuisine_image,Status) values ('Pasta','24-Nov-2015','mypasta.jpg','1');
insert into webcuisine (Cuisine_name,Date_added,Cuisine_image,Status) values ('Maggie','23-Nov-2015','mymaggie.jpg','1');
insert into webcuisine (Cuisine_name,Date_added,Cuisine_image,Status) values ('Haka','22-Nov-2015','myhaka.jpg','1');
insert into webcuisine (Cuisine_name,Date_added,Cuisine_image,Status) values ('Pizza','21-Nov-2015','mypizza.jpg','0');

Now we have inserted some dummy data into the table. You will see that there is a column with “0” value in fourth line this is because we are going to show only those items in the API which are having “1” as status.

Retrieve data using the PHP

We create a function named “gerCuisineList”. Under this function we will retrieve all the list from table which is having 1 as status. If we dont found anything than will send the the result in JSON format so that our device will read it. If we found the details than we will just add the values to an array and return the whole array as JSON.

Here we have to create the array because JSON supports only Array as value.

We will create a class for this function named cuisineAPI.php

<?php
require_once( dirname( __FILE__ ) . '/../function/connectionClass.php' );
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of cuisineClass
 *
 * @author Vivek
 */
class cuisineAPI extends connectionClass {
 
public function getCuisineList(){
        $select="select * from webcuisine where Status='1'";
        $result=  mysqli_query($this->con, $select);
        $count=  mysqli_num_rows($result);
        if($count < 1){
            echo json_encode(array("status"=>"1","msg"=>"No cuisine found"));
        }
        else
        {
            $return_arr = array();
            while($row= mysqli_fetch_array($result)){
                $row_array['id'] = $row['Id'];
                $row_array['name'] = $row['Cuisine_name'];
                $row_array['img'] = "http://www.domain.com/cuisineIcons/".$row['Cuisine_image'];
                array_push($return_arr,$row_array);
            }
            mysqli_close($this->con);
            return json_encode(array("status"=>"2",$return_arr));
        }
    }
}

So now we have the values now the last part is remaining which is for sending the values to the device. We use POST method to receive the details from mobile to ask for the values from our table.

We will create a getCuisine.php.

error_reporting(E_ALL & ~E_NOTICE & ~E_USER_NOTICE);
header('Content-Type: application/json');
$key=$_POST["key"];

if($key!="YourKeyWillGoHere"){
    echo json_encode(array("status"=>"0"));
}
else
{
    include_once './cuisine/cuisineAPI.php';
    $cuisineclass=new cuisinePI();
    echo $cuisineclass->getCuisineList();
}

So if you call this page you will get the JSON result which you can use with your mobile device.

Hope you like our today’s tutorial “Create JSON API using PHP and MYSQL. if you have any comment or advice please let us know.

One thought on “Create JSON API Using PHP and MYSQL

Leave a Reply

Your email address will not be published. Required fields are marked *