What is Webservices?

Webservices are used to allow users to access data from different sources like the Android app, IOS app, website etc from a centralized database.
We can create a web service 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.

Click : https://phpgurukul.com/webservices-in-php/

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”,
“gender”:”Male”,
“number”:”7556645345342″,
“email”:”[email protected]”,
“password”: “Demo”
}
Output will be in JSON format 
For successful attempt 
{“StatusCode”:”200″,”StatusDescription”:”Registration successful”}
For unsuccessful attempt 
Case 1
{“StatusCode”:”205″,”StatusDescription”:”Email id already exist. Please try with diffrent email.”}
Case 2
{“StatusCode”:”400″,”StatusDescription”:”Something went wrong. Please try again.}

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.

Video Link : https://www.youtube.com/watch?v=FdLWiOLe_Bg

 

PHP Gurukul

Welcome to PHPGurukul. We are a web development team striving our best to provide you with an unusual experience with PHP. Some technologies never fade, and PHP is one of them. From the time it has been introduced, the demand for PHP Projects and PHP developers is growing since 1994. We are here to make your PHP journey more exciting and useful.

Website : https://phpgurukul.com