A REST-style architectures consist of clients and servers.
Clients initiate requests to servers; servers process requests and return
appropriate responses. Requests and responses are built around the transfer of
representations of resources. Requests utilize HTTP verbs (GET, POST, PUT,
DELETE, etc.) which are used to manage the state of resources. The responses
can be sent as XML or JSON. Since REST uses HTTP, it can be used practically
for any programming language and is easy to test.
PHP Example
Step 1. Create a MySQL table contact
under database test.
CREATE TABLE IF NOT EXISTS `contact` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`email` varchar(255) CHARACTER SET utf8 NOT
NULL,
`address` text CHARACTER SET utf8 NOT NULL,
`feedback` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT
CHARSET=latin1;
Step 2. Dump some data into table
contact
INSERT INTO `contact` (`id`, `name`,
`email`, `address`, `feedback`) VALUES
(13, 'bnvnbvnn', 'vvn@gmail.com', 'Potts
Point, New South Wales, Australia', 'cvc
xvxcvxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'),
(14, 'vnbvnvnbvnbvnv', 'nvvnvvn@gmail.com',
'Flemington, Homebush West, New South Wales, Australia',
'hfvnvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv'),
(15, 'gncb bnc bc', 'gcvhv@gmail.com',
'Remise Havenstraat GVB, Havenstraat, Amsterdam, Netherlands', 'xcv
cxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
Step 3. Create a controller file
rest.php under with the following source code
<?php
header('Content-Type: application/json');
if(isset($_GET['id'])){
$var = $_GET['id'];
$con=mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL:
" . mysqli_connect_error();
}
$sql="select * from contact where id =
$var";
if($result = mysqli_query($con, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo json_encode($row, JSON_PRETTY_PRINT);
}
}
}
}
?>
Step 4. Run on wamp server link: http://localhost/restdemo/rest.php?id=13
Output:
{
"0": "13",
"id": "13",
"1": "bnvnbvnn",
"name": "bnvnbvnn",
"2": "vvn@gmail.com",
"email": "vvn@gmail.com",
"3": "Potts Point, New South Wales, Australia",
"address": "Potts Point, New South Wales, Australia",
"4": "cvc xvxcvxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"feedback": "cvc xvxcvxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
"0": "13",
"id": "13",
"1": "bnvnbvnn",
"name": "bnvnbvnn",
"2": "vvn@gmail.com",
"email": "vvn@gmail.com",
"3": "Potts Point, New South Wales, Australia",
"address": "Potts Point, New South Wales, Australia",
"4": "cvc xvxcvxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"feedback": "cvc xvxcvxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
Step 5. Check Json file is valid or not
link: https://jsonlint.com/
Step 6. Consuming Web service Create a
controller file client.php under with the following source code
<?php
if(isset($_GET['id'])){
$url =
file_get_contents("http://localhost/restdemo/rest.php?id=".$_GET['id']);
$var = json_decode($url,true);
echo "<table>";
foreach($var as $k=>$v)
echo
"<tr><td>$k</td><td>$v</td></tr>";
echo "</table>";
}
?>
Step 7. Run on wamp server link: http://localhost/restdemo/client.php?id=13
Output:
0
|
13
|
id
|
13
|
1
|
bnvnbvnn
|
name
|
bnvnbvnn
|
2
|
vvn@gmail.com
|
email
|
vvn@gmail.com
|
3
|
Potts Point, New South Wales,
Australia
|
address
|
Potts Point, New South Wales,
Australia
|
4
|
cvc
xvxcvxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
feedback
|
cvc
xvxcvxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
Thanks for reading.