PHP RESTful Web Service

Representational State Transfer

Standard design architecture for developing web API

REST is a stateless client-server relationship; 

There is no client context being stored server side (no Sessions).

Each request contains all the necessary for the server to authenticate the user, and any session state data.

In REST we have the ( Create, Read, Update, Delete) Methods

GET – Used for basic read requests to the server
PUT- Used to modify an existing object on the server
POST- Used to create a new object on the server
DELETE – Used to remove an object on the server

http://coreymaynard.com/blog/creating-a-restful-api-with-php/

-=-=-=-=-=-=-=-

 

https://shareurcodes.com/blog/creating%20a%20simple%20rest%20api%20in%20php

http://www.startupcto.com/backend-tech/php/using-soap-rest-web-services-in-php

URL:https://www.codeofaninja.com/2017/02/create-simple-rest-api-in-php.html

 

you can get the product information by visiting following URL.

http://localhost/projectname/callapi.php?name=pen

callapi.php is the interface for all that we want to do with  the server.

depending on the variable name that we pass it calls any php we want and gets the response from that other php and sends it back to browser.

=-=-=-=-

<?php #amirapi.php

header(“Content-Type:application/json”);

if(!empty($_GET[“name”]))
{

$name=$_GET[“name”];
#echo $name;
$price = get_price($name);
if(empty($price))
{
response(200,”price is empty”,NULL);
}
else
{
response(200,”Product Found”,$price);
}

}
else
{
response(400,”_GET[name] is empty”,NULL);
}

function get_price($name)
{

$products = [
“book”=>20,
“pen”=>60,
“pencil”=>5
];

foreach($products as $product=>$price)
{
if($product==$name)
{
return $price;
break;
}
}

}

function response($status,$status_message,$data)
{
header(“HTTP/1.1 “.$status);
$response[‘status’]=$status;
$response[‘\status_message’]=$status_message;
$response[‘data’]=$data;
$json_response = json_encode($response);
echo $json_response;
}

-=-=-=-=-=-=-

Rest Client callapi.php is the interface for all that we want to do with  the server. Depending on the variable name that we pass it calls any php we want and gets the response from that other php and sends it back to browser.

-=-=-=-=-=-=-=-=-=-=-=-=-=–=-==-

<?php   #callapi.php

$name=$_GET[“name”];
$url = “http://localhost/amirapi.php?name=”.$name;
echo $url;
$client = curl_init($url);
echo “<p>1”;
var_dump($client);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);
echo “<p>2”;
var_dump($response);
$result = json_decode($response);
echo “<p>3”;
var_dump($result);
echo “<p>4”;
echo $result->data;

?>

=-=-=-=-=-=–

Other stuff

=-=-=-=-=-=-=-

<?php
if(isset($_POST[‘submit’]))
{
$name = $_POST[‘name’];

$url = “http://localhost/projectname/api/”.$name;

$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);

$result = json_decode($response);

echo $result->data;
}
?>

 

-=-=-=-=-=-=-
<?php
/*
A domain Class to demonstrate RESTful web services
*/
Class Mobile {
private $mobiles = array(
1 => ‘Apple iPhone 6S’,
2 => ‘Samsung Galaxy S6’,
3 => ‘Apple iPhone 6S Plus’,
4 => ‘LG G4’,
5 => ‘Samsung Galaxy S6 edge’,
6 => ‘OnePlus 2’);
/*
you should hookup the DAO here
*/
public function getAllMobile(){
return $this->mobiles;
}
public function getMobile($id){

$mobile = array($id => ($this->mobiles[$id]) ? $this->mobiles[$id] : $this->mobiles[1]);
return $mobile;
}
}
?>

-=-=-=-=-

Create a Basic Web Service Using PHP, MySQL, XML, and JSON

=-=-=-=-=-=-

Server side for file upload: https://www.youtube.com/watch?v=mmNJMXfGZf4