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 :
• SOAP (Simple Object Access Protocol)
• REST (Representational State Transfer)
https://phpgurukul.com/webservices-in-php/
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.
https://phpgurukul.com/webservices-in-php/
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,
`regDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `signup`
ADD PRIMARY KEY (`id`);
https://phpgurukul.com/webservices-in-php/
<?php
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','database Name'); // put you database name here
// Establish database connection.
try
{
$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());
}
?>
https://phpgurukul.com/webservices-in-php/
<?php
include_once("config.php");
header("Content-type:application/json");
$jsdata=json_decode(file_get_contents('php://input'),true);
$fname=$jsdata['fullName'];
$gender=$jsdata['gender'];
$contactno=$jsdata['number'];
$email=$jsdata['email'];
$password=$jsdata['password'];
$sql="INSERT INTO
signup(fullName,gender,contactNumber,email,password)
VALUES(:fname,:gender,:contactno,:email,:pasword)";
https://phpgurukul.com/webservices-in-php/
$query = $dbh->prepare($sql);
$query->bindParam(':fname',$fname,PDO::PARAM_STR);
$query->bindParam(':gender',$gender,PDO::PARAM_STR);
$query->bindParam(':contactno',$contactno,PDO::PARAM_STR);
$query->bindParam(':email',$email,PDO::PARAM_STR);
$query->bindParam(':pasword',$password,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
if($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['StatusCode']="400";
$msg['StatusDescription']="Something went wrong. Please try again.";
print(json_encode($msg));
}
https://phpgurukul.com/webservices-in-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.
https://phpgurukul.com/webservices-in-php/
How to test this service in your local system
Install postman chrome extension
Run postman chrome extension then a new
request
Choose http request method
Enter the requested url
Provide the requested data in JSON Format
Hit the send button
https://phpgurukul.com/webservices-in-php/
Input parameters will be like this
{
“fullName”: “John Doe”,
“gender”:”Male”,
“number”:”7556645345342″,
“email”:”john@anc.com”,
“password”: “Demo”
}
Output will be in JSON format
{“StatusCode”:”200″,”StatusDescription”:”Registration
successful”}
For unsuccessful attempt
{“StatusCode”:”400″,”StatusDescription”:”Something went
wrong. Please try again.}
https://phpgurukul.com/webservices-in-php/
More Details Visit
https://phpgurukul.com/webservices-in-
php/
https://phpgurukul.com/webservices-in-php/