Webservices in PHP

What is Webservices?
Webservices are used to allow user to access data from diffrent sources like Android app, IOS app, website etc from a centralized database.
We can create a webservice through two methods :

  1. SOAP (Simple Object Access Protocol)
  2. REST (Representational State Transfer)

SOAP (Simple Object Access Protocol)
SOAP : Simple Object Access Protocol. SOAP is easy to read because SOAP based on XML. SOAP is the XML based format  for sending and receiving data.
REST (Representational State Transfer)
REST : Representational State Transfer. It is an architectural style that run over HTTP. REST webservices generate status code response in JSON & XML.
REST support all HTTP methods GET, POST, PUT & DELETE.
GET : Retrieve particular resources by an id
POST :  Create a new resource.
PUT : Update a  particular resource by an id.
DELETE : Remove a particular  resource.
Example of POST 
Step 1 : Create a database ( create database your_databasename)
Step 2 : create a SQL table signup. signup table structure given below :

CREATE TABLE `signup` (
  `id` int(11) NOT NULL,
  `fullName` varchar(120) DEFAULT NULL,
  `gender` varchar(100) DEFAULT NULL,
  `contactNumber` char(10) DEFAULT NULL,
  `email` varchar(150) DEFAULT NULL,
  `password` varchar(200) DEFAULT NULL,
ALTER TABLE `signup`

Step 3 : Create a database connection file (config.php)

// DB credentials.
define('DB_NAME','database Name'); // put you database name here
// Establish database connection.
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
catch (PDOException $e)
exit("Error: " . $e->getMessage());

Step 4 : Create a requested url (http://localhost/ws/signup.php)

$sql="INSERT INTO signup(fullName,gender,contactNumber,email,password) VALUES(:fname,:gender,:contactno,:email,:pasword)";
$query = $dbh->prepare($sql);
$lastInsertId = $dbh->lastInsertId();
header("HTTP/1.1 200 OK");
header('Content-Type: text/plain');
$data=array("StatusCode"=>"200","StatusDescription"=>"Registration successful");
echo json_encode($data);
} else {
header("HTTP/1.1 400 Bad Request");
header('Content-Type: text/plain');
$msg['StatusDescription']="Something went wrong. Please try again.";

header(“Content-type:application/json”)  sends the http json header to the browser to inform him what the kind of a data he expects.
json_decode(file_get_contents(‘php://input’),true)  receive the RAW post data via the php://input IO stream.
We  retrieve the raw POST data from the php://input stream.
header(….) detect http response line.
header(“HTTP/1.1 200 OK”);  // for successful attempt
header(“HTTP/1.1 400 Bad Request”);  // for bad request
 header(‘Content-Type: text/plain’) indicates that the content is plain text and no special software is required to read the contents.
How to test this service in your local system

  1. Install postman chrome extension
  2.  Run postman chrome extension then a new request
  3. Choose http request method
  4. Enter the requested url
  5. Provide the requested data in JSON Format
  6.  Hit the send button

Input parameters will be like this
“fullName”: “John Doe”,
“password”: “Demo”
Output will be in JSON format 
For successful attempt 
{“StatusCode”:”200″,”StatusDescription”:”Registration successful”}
For unsuccessful attempt 
{“StatusCode”:”400″,”StatusDescription”:”Something went wrong. Please try again.}

WebSerivces in PHP(Download Source Code)
Size: 2.13 KB
Version: V1.0

How to run this script
1. Download and Unzip file on your local system.
2. Put this file inside root directory
3. Database Configuration
Database Configuration
Open phpmyadmin
Create Database webservices
Import database signup.sql (available inside zip package)
requested url for service http://localhost/ws/signup.php
After that use postman chrome extension to hit the WebService.
How to use postman to hit the Webservice

Recommended Tutorials for you


Anuj kumar

This is Anuj Kumar. I’m a professional web developer with 5+ years of experience. I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials. .

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.

Back to top button