What is web service?

Web service is basically a bridge between two programming platforms. It provides communication between two platforms or you can say programming languages. It means you can now access your database from web, desktop and mobile application using web services. Web services has become a most important part of development now a days, which provides facility to access the data from different sources like web, desktop and mobile application over a network. Database has been centralized at some place. Web service is easy to create and use. Any platform can access it easily using url.

In this blog we are going to create web service that provides JSON response and perform insert, update, delete and select operation using php and MySQL.

I hope you already have WAMP or XAMPP started in your pc. Create database and table with columns RegId, RegFullName, RegEmail, RegPassword, RegGender, RegHobbies. Also create proper folder in C:\xampp\htdocs\ to save all pages.

Insert service

Step 1. Create insertservice.php file in folder which is you created to store all pages. Below is the code to insert data. 

insertservice.php

<?php

    /* Connection with database */

    $con = new mysqli("localhost","root","","sample") or die(mysqli_error($con));

   

    /* Get values from query string */

    $fname = $_REQUEST["fname"];

    $email = $_REQUEST["email"];

    $password = $_REQUEST["password"];

    $gender = $_REQUEST["gender"];

    $hobbies = $_REQUEST["hobbies"];

  

    $sql = "insert into tblregistration(RegFullName,RegEmail,RegPassword,RegGender,RegHobbies) values('".$fname."','".$email."','".$password."','".$gender."','".$hobbies."')";

    $qur = $con->query($sql);

    if($qur)

    {

$json = array("status" => 1, "msg" => "User inserted successfully..!");

    }

    else

    {

$json = array("status" => 0, "msg" => "Error while inserting user..!");

    }

  

    @mysqli_close($conn);

  

    /* Output header */

    header('Content-type: application/json');

    echo json_encode($json);

?>

To run a web service you need to type a url in browser. Your url can be like "http://localhost/Services/JSON/insertservice.php?fname=demo&email=demo@gmail.com&password=demo123&gender=male&hobbies=Cricket,Music"

  • In above code header('Content-type: application/json') is used to tell php that page will return json.
  • json_encode($json) used to encode our data into json format.
  • $_REQUEST["fname"] is used to get a value from query string which can be pass from any platform. Right now we are passing values from browser. 

So basically query string is used to pass values to web services and $_REQUEST["keyname"] is used to get values from query string.

When you run service and if it is appropriate then it will display message "User inserted successfully..!" and if there are any problem then it will display "Error while inserting user..!" on browser.

Select service

Step 1. Create a new page selecservice.php to retrieve data.

selectservice.php

<?php

    /* Connection with database */

    $con = new mysqli("localhost","root","","sample") or die(mysqli_error($con));

  

    $sql = "select * from tblregistration";

    $res = $con->query($sql);

    $result = array();

    while($row = mysqli_fetch_array($res))

{

    array_push($result,

    array('RegId'=>$row["RegId"],

    'RegFullName'=>$row["RegFullName"],

    'RegEmail'=>$row["RegEmail"],

    'RegPassword'=>$row["RegPassword"],

    'RegGender'=>$row["RegGender"],

    'RegHobbies'=>$row["RegHobbies"]

    ));

}

   

    echo json_encode(array("result"=>$result)); 

    mysqli_close($con);

?>

Run service using url i.e "http://localhost/Services/JSON/selectservice.php"

If you want to use where condition and values would be dynamic than pass values in query string and get values using $_REQUEST["keyname"]. Same as we did in insert service.

Update service

Step 1. Create a page with updateservice.php in your folder.

updateservice.php

<?php

    /* Connection with database */

    $con = new mysqli("localhost","root","","sample") or die(mysqli_error($con));

  

    /* Get values from query string */

    $uid = $_REQUEST["uid"];

    $fname = $_REQUEST["fname"];

    $email = $_REQUEST["email"];

    $password = $_REQUEST["password"];

    $gender = $_REQUEST["gender"];

    $hobbies = $_REQUEST["hobbies"];

  

    $sql = "update tblregistration set RegFullName='".$fname."',RegEmail='".$email."',RegPassword='".$password."',RegGender='".$gender."',RegHobbies='".$hobbies."' where RegId='".$uid."'";

    $qur = $con->query($sql) or die(mysqli_error($con));

    if($qur)

    {

$json = array("status" => 1, "msg" => "User updated successfully..!");

    }

    else

    {

$json = array("status" => 0, "msg" => "Error while updating user..!");

    }

  

    @mysqli_close($conn);

  

    /* Output header */

    header('Content-type: application/json');

    echo json_encode($json);

?>

Run url "http://localhost/Services/JSON/updateservice.php?fname=demo1&email=demo1@gmail.com&password=demo@123&gender=female&hobbies=Cricket&uid=3". Output will show appropriate message "User updated successfully..!" or "Error while updating user..!".

Delete service

Step 1. Create a page deletedervice.php and write below code to perform delete opration.

deleteservice.php

<?php

    /*Connection with database*/

    $con = new mysqli("localhost","root","","sample") or die(mysqli_error($con));

 

    /* Get values from query string */

    $uid = $_REQUEST["uid"];

  

    $sql = "delete from tblregistration where RegId='".$uid."'";

    $qur = $con->query($sql) or die(mysqli_error($con));

    if($qur)

    {

$json = array("status" => 1, "msg" => "User deleted successfully..!");

    }

    else

    {

$json = array("status" => 0, "msg" => "Error while deleting user..!");

    }

    

    @mysqli_close($conn);

  

    /* Output header */

    header('Content-type: application/json');

    echo json_encode($json);

?>

Run url "http://localhost/Services/JSON/deleteservice.php?uid=1". It will display appropriate message "User deleted successfully..!” or “Error while deleting user..!".

As I said web service help us to communicate with different platform. So while working with multiple platform, just use our url to call services, pass values to query string and rest will be done by services itself.

Hope you will like my blog. Comment below to ask questions.