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 XML response and perform insert, update, delete and select operation using php and MySQL.

Start WAMP or XAMPP 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 page in your folder located in C:\xampp\htdocs\. 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"];

  

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

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

    if (!$result)

    {

        header('Content-type: text/xml');

        echo '<Result>Error while inserting.!!</Result>';

    }

    else

    {

        header('Content-type: text/xml');

        echo '<Result>User Added Successfully..!!</Result>';

    }

    @mysqli_close($con);

?>

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

  • In above code header('Content-type: text/xml') is used to tell php that page will return XML.
  • $_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 Added successfully..!" and if there are any problem then it will display "Error while inserting..!" 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));

   

    $query = "select * from tblregistration";

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

    $sample = array();

    if (mysqli_num_rows($result))

    {

        while ($tablregistration = mysqli_fetch_assoc($result))

        {

            $sample[] = array(

                'tablregistration' => $tablregistration

            );

        }

    }

  

    header('Content-type: text/xml');

    echo '<sample>';

    foreach ($sample as $index => $tablregistration)

    {

        if (is_array($tablregistration))

        {

            foreach ($tablregistration as $key => $value)

            {

                echo '<', $key, '>';

                if (is_array($value))

                {

                    foreach ($value as $tag => $val)

                    {

                        echo '<', $tag, '>', htmlentities($val), '</', $tag, '>';

                    }

                }

                echo '</', $key, '>';

            }

        }

    }

    echo '</sample>';

    @mysqli_close($con);

?>

Run service using url i.e "http://localhost/Services/XML/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"];

    

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

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

    if (!$result)

    {

        header('Content-type: text/xml');

        echo '<Result>Error while updating..!!</Result>';

    }

    else

    {

        header('Content-type: text/xml');

        echo '<Result>User updated successfully..!!</Result>';

    }

    @mysqli_close($con);

?>

Run url "http://localhost/Services/XML/updateservice.php?fname=demo1&email=demo1@gmail.com&password=demo@123&gender=male&hobbies=cricket&uid=4"

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"];

    

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

    $result = $con->query($query);

    if (!$result)

    {

        header('Content-type: text/xml');

        echo '<Result>Error while deleting..!!</Result>';

    }

    else

    {

        header('Content-type: text/xml');

        echo '<Result>User deleted successfully..!!</Result>';

    }

    @mysqli_close($con);

?>

Run url "http://localhost/Services/XML/deleteservice.php?uid=5"

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.

I hope you are liking my blogs. Please comment to ask queries or to post blogs on new topic.