Webservices in PHP (How to create user registration/signup Webservice)

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.
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)
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.}

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

Showing 1 reaction

Please check your e-mail for a link to activate your account.
  • php gurukul
    published this page in Submit Content 2022-06-23 09:32:31 -0700

connect