Monday, July 20, 2015

How to create a Restful Service in Php and MySQL with JSON data as output? (In Simple Steps)




Step 1 : 

Go to phpMyAdmin ,
select an existing database or create one

Step 2 : 

Create a sample Table (for eg: customers)

Step 3 : 

Insert a few records into the table

Step 4 : 

Create a new php page under www folder (or public_html folder)
Give name for your page (for eg: Get_Customers.php)

Step 5 : 

In the php page, follow these steps
              

             1. Connect to your database

           
 <?php
           
 $con = mysqli_connect("localhost", "<user name>" , "<password>", "<db name>") or die('could not connect');

//Write a query
$qry = "SELECT * FROM  Customers"

//Execute the query
$result = mysqli_query($con,$qry);
?>

               2. Put the data into array

<?php

$json = array();

//Loop through each record in the database and read the values
while($row=mysqli_fetch_array($result))
  {
      $arrcustomers = array
      (
         'customer_id' => $row['customer_id'],
         'customer_name' => $row['customer_name'],
         'shop_name' => $row['shop_name'],
         'loyalty_points' => $row['loyalty_points']
      );
           //push each row into an array
         array_push($json, $arrcustomers);
  }
?>


               3. Output the array as json


<?php

$json1['Customers']=$json;
echo json_encode($json1);

?>

Step 6 : 

Call Php Page in browser (for eg: http://localhost/Get_Customers.php)

Step 7 : 

Get the Result in Web Page in JSON format

Result:

{"Customers":[{"customer_id":"1","customer_name":"sam","shop_name":"SamMedicals","loyalty_points":"50"},{"customer_id":"2","customer_name":"sandy","shop_name":"upahar_gruha","loyalty_points":"100"},{"customer_id":"3","customer_name":"vishal","shop_name":"vishal_panshop","loyalty_points":"30"},{"customer_id":"4","customer_name":"sunny","shop_name":"sunnys shop","loyalty_points":"40"}]}


I hope this article was helpful for beginners trying to create a simple php RESTful service.
I am Shruti Kulkarni and I am a beginner myself with one month of total experience working at Nanite Solutions as an iOS developer.

If you have further questions about RESTful services please feel free to write to me at shruti@nanitesol.com

Thanks

Guided by: Praveen Kumar, Nanite Solutions

6 comments:

  1. good but you can add more features to your blog which are inbuilt check this how i have added to my blog

    http://saishborkar.blogspot.in

    ReplyDelete
  2. Really very helpful. Just superb.

    ReplyDelete
  3. Its really awesome. But let me know what is the main difference between giving output in json and xml. http://www.phptutorials.club

    ReplyDelete
  4. Think of JSON as Fat-free version of xml. Which makes it good and bad.

    for JSON
    - less markup. Transmission is faster because less data to be carried and it is compact
    - easy to read

    for XML
    - you can have a better data structure and schema.
    - You can use XPATH which is very powerful query engine
    - Transformations: You can use XSL for transforming XML without needing languages ilke java or ruby etc

    ReplyDelete