Simple Web services in PHP

Sunday, 9 August 2015



Hello friends, in this post we will learn about very important concept i.e Web Services and the implementation of a simple Web Service. First I will give a small example then I will tell in details. 
You may hear about IRCTCmakemytrip.comyatra.comcleartrip.com and many more, now my question is "how these websites works ?". They don't have their personal data bases for storing all information. Now their is no need to maintain those many databases for a number of websites, because the functionalities are same i.e to providing the services for railway reservation and many many services. In such cases the use of Web Services came into picture. 

What is Web Services 

A Web service is a method of communication between two electronic devices over a network. It is a software function provided at a network address over the Web with the service always on as in the concept of utility computing.

Web Services is used to maintain interoperability, that means by using web services we can able to make the communication and data exchange between the different software applications made up of different programming languages and technologies. This means for an example if we have an application in PHP, then we can use the output of that application in the applications made up of different programming languages and technologies such as C, C++, Java, .Net, Pearl, Android, ect.

This means by using web service we can make a application as technology and language independent.

How is it possible to make application as technology and language independent 


There are few other technologies are there such as XML, JSON, those can be understand by the all programming languages, I mean to say that we can manipulate JSON and XML data using any programming language. 

So now you can easily understand that how is it possible to make a technology and language independent and how web service works .

So any web service application gives the output as XML or JSON, and then other applications can consume on manipulate it .

Following is a simple architecture to understand the concept of web services,





A simple web service using PHP

Now we are going implement a simple web service application using PHP and MySql. This application is basically for getting the result of a student of a university and this application will take the student id as a input and will give the result of the student as a output.

First create a table in your database by name student_result, you can use following SQL query for creating this table,



        CREATE TABLE Student_Result(
            SID INT( 5 ) NOT NULL ,
            NAME VARCHAR( 15 ) ,
            MATH VARCHAR( 6 ) ,
            ENG VARCHAR( 6 ) ,
            SCE VARCHAR( 6 ) ,
            TOTAL VARCHAR( 6 ) ,
            DIVISION VARCHAR( 10 ) ,
            STATUS VARCHAR( 4 ) ,
            PRIMARY KEY ( SID )
        )

Then insert some dummy data to the table, by using following SQL query,



      INSERT INTO  `devinfo99`.`student_result` (
          `SID` ,
          `NAME` ,
          `MATH` ,
          `ENG` ,
          `SCE` ,
          `TOTAL` ,
          `DIVISION` ,
          `STATUS`
      )
      VALUES (
          '11101', 'Jhon',  '60',  '70',  '50',  '180',  'FIRST',  'PASS'
      ), (
          '11102', 'Smith',  '12',  '20',  '30',  '62',  'FAIL',  'FAIL'
      ),
      ..........
      ..........
      ..........
      ;

Following is the code for this web service application, just create get_student_status_ws.php.php file and write the following code,


<?php

     // PHP Webservices to get Student Status based on their Roll No / ID
 
     // DB info
 
     $host_name = 'localhost'; // your host name
     $db_user = 'root'; // your db user name
     $db_paswd = ''; // your db password
     $db_name = 'devinfo99'; // your db name
 
     $response_array['response'] = array();
     header('Content-type: application/json');
     if(isset($_REQUEST['rollNo'])  &&  $_REQUEST['rollNo']  !=  NULL){
          $roll_no = $_REQUEST['rollNo'];
          mysql_connect($host_name, $db_user, $db_paswd);
          mysql_select_db($db_name);
          $sql_query = "SELECT * FROM student_result WHERE SID = '$roll_no'";
          $query = mysql_query($sql_query);
          while($rec  = mysql_fetch_assoc($query)){
              $response_array['response'] = $rec;
          }
          echo json_encode($response_array); // return output as JSON
     }else{
          $response_array['response'] = 'undefined';
          echo json_encode($response_array);
     }
?>

Now try to hit the URL (http://localhost/get_student_status_ws.php?rollNo=11102) on the browser you will get following result


{"response":{"SID":"11102","NAME":"Smith","MATH":"12","ENG":"20","SCE":"30","TOTAL":"62","DIVISION":"FAIL","STATUS":"FAIL"}}

How can use this application

As earlier I told that we can manipulate the JSON data by using any programming language and technology, so we can consume this above application by using any programming language and technology and we can display result to our users.

Note :

This is not a complete tutorial on Web Service, this is a basic about web service. Keep on visiting this site, I will post more advanced concepts on Web Service. Thank You :) .

No comments:

Post a Comment

 
About Contact Career Advertise Job Support Online Training Acardemic Projects Internship
Copyright © 2016. [ info ].
Design by Herdiansyah Hamzah. & Distributed by Free Blogger Templates
Creative Commons License