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 :

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

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

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

Anuj Kumar

I'm Anuj kumar a web developer with 4+ year 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 is provide php , jquery , mysql , phpoops and other web development tutorials. I am trying best effort to make PHPGurukul useful for every single moment spend on this website. If you think this website is useful to visit please share with your friend and buddies.

Leave a Reply

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