Back to Top

How to use WebService in PHP


In this post, I am going to explain, how to call web service using PHP. Nowadays every website is using android or iOS application. To use web data to the application web services are used. We can simply say web services are the bridge to connect web data with the mobile application.

Earlier I have explained how to use Webservices in WordPress but not written about how to call using core PHP So, I thought it’s right the time to write one now.

Web Service means some data comes at PHP side as Request, We serve the request in PHP and did some manipulation with the database and send Response.

For Testing a Web service, We can use extensions provided in chrome which is known as Advanced Rest Client.You can install the addon and check your Webservices.Same addon available for Mozilla also.

Following Add-ons link for Mozilla

Following Extension link for Chrome

Download addon and install it in your browser.That’s it. You are ready to use web services.

Let’s start to check how to create, test and use web service with example.

First of all, Create one file and set configuration of the database and physical path and relative path setting in it.

Next, create mysql class file for insert,update,delete function which is unique functionality for insert,delete,update any table data.

Here, I have created login.php which will check user is a register or not.

Step 1 This is request data in json format

webdata={"email":"[email protected]","password":"test123"}

Step 2 At PHP side we get data as

$_POST['webdata']='{"email":"[email protected]","password":"123456"}';

Step 3 Then You can apply


Step 4 Apply data manipulation to check user is exist or not.

Step 5 Then You will get generated Response.

Here, I am going to explaining using login example.

if(isset($_POST['webdata']) && $_POST['webdata']!='')
	$string 	= $_POST['webdata'];
	$data 		= json_decode($string); // decode json data
	$email 		= $data->email;
	$password 	= md5($data->password);
	$resultsetuser = processlogin($email,$password); //Data manipulation check user is exit or not
	if(count($resultsetuser)> 0)
		$msgtype = "success";
		$updcode = "1";
		$msgcode = 'User login successfully';
	}else {
		$msgtype = "fail";
		$updcode = "0";
		$msgcode = 'Username and password does not match.';
		$result['result'][$msgtype] = $updcode;
		$result['result']['message'] = mysql_real_escape_string($msgcode);
		header('Content-type: application/json');
		echo json_encode($result);
		$result['result']['message'] = 'Requested data is not found in PHP script.';
		header('Content-type: application/json');
		echo json_encode($result); //Generated Response

function processlogin($email,$vpassword)
	global $obj;
	$sqltypecond = '';
	$db_sql = "select email,password,username from user where email = '".$email."' AND password='".$vpassword."'";
	$result = $obj->select($db_sql);
	return $result;


Here login.php file will check web data is set when data requested via POST. If get data, decode that data. Next check decoded data with process login function that data exists in the database or not.

If data found, return success status with message or failure if not found. You need to pass data after encoding it so use json_encode to encoding a data.

Isn’t very easy to use web services with PHP? If you know any other better method, please share.If you have any comments or suggestion about this post, I would really appreciate it.

Comments (2)

  1. Can you plz tell me how to run above web service code. Is it necessary to use Advanced RESTClient extension..?

    1. Yeah, You can use Advanced RESTClient extension.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts


Posted on 2 years ago


Really! PHP touches the 200 Million Mark

Posted on 6 years ago

John Pitt

How to set 301 Redirection in PHP

Posted on 4 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago