X
    Categories: PHP

PHP RESTful web services

PHP RESTful web services

REST (representational state transfer). We need RESTapi for request and response json datas. The response will be in Json format as well as XM format. PHP frameworks by default have RESTful format to  respond api request. Lets see how to implement Simple RESTful api in php using classes and functions.

My files are :

SimpleRest.php => Which will handle the Http headers and Http status messages.
Db.inc.php => Which will handle the database connection process.
User.php => Which will handle the user table queries.
UserRestHandler.php => Which will handle the User api process.
RestController.php => Which will handle the api routes
.htaccess => Which will handle the api Clean URL’s

Here We have used ‘Advanced Rest Client’ to check API requests locally.

Lets see how it will work:

Let us will connect the database with our application. I have a database called restful and have one table called users.

Db.inc.php

Here we will connect the database with our application.

<?php 
class Database {

	public $conn;
	private $engine;
	private $host;
	private $username;
	private $database;
	private $password;

	public function __construct() {

		$this->engine 	= 'mysql';
		$this->host 	= 'localhost';
		$this->database = 'restful';
		$this->username = 'root';
		$this->password = 'root';
		$dns = $this->engine . ":" . "host=" . $this->host . ";dbname=" . $this->database ;

		try {
			$this->conn = new \PDO($dns, $this->username, $this->password);
		}
		catch ( PDOException $e ) {
			$e->getMessage();
		}
		if($this->conn) {
			return $this->conn;
		}
	}

}

It will connect our application with database else it will return the error message.

SimpleRest.php


Here we have written the functions to handle the php header functions and http status message.

<?php 

class SimpleRest  {
	
	private $httpVersion = "HTTP/1.1";

	public function setHttpHeaders($contentType, $statusCode){
		
		$statusMessage = $this -> getHttpStatusMessage($statusCode);
		
		header($this->httpVersion. " ". $statusCode ." ". $statusMessage);		
		header("Content-Type:". $contentType);
	}
	
	public function getHttpStatusMessage($statusCode){
		$httpStatus = array(
			100 => 'Continue',  
			101 => 'Switching Protocols',  
			200 => 'OK',
			201 => 'Created',  
			202 => 'Accepted',  
			203 => 'Non-Authoritative Information',  
			204 => 'No Content',  
			205 => 'Reset Content',  
			206 => 'Partial Content',  
			300 => 'Multiple Choices',  
			301 => 'Moved Permanently',  
			302 => 'Found',  
			303 => 'See Other',  
			304 => 'Not Modified',  
			305 => 'Use Proxy',  
			306 => '(Unused)',  
			307 => 'Temporary Redirect',  
			400 => 'Bad Request',  
			401 => 'Unauthorized',  
			402 => 'Payment Required',  
			403 => 'Forbidden',  
			404 => 'Not Found',  
			405 => 'Method Not Allowed',  
			406 => 'Not Acceptable',  
			407 => 'Proxy Authentication Required',  
			408 => 'Request Timeout',  
			409 => 'Conflict',  
			410 => 'Gone',  
			411 => 'Length Required',  
			412 => 'Precondition Failed',  
			413 => 'Request Entity Too Large',  
			414 => 'Request-URI Too Long',  
			415 => 'Unsupported Media Type',  
			416 => 'Requested Range Not Satisfiable',  
			417 => 'Expectation Failed',  
			500 => 'Internal Server Error',  
			501 => 'Not Implemented',  
			502 => 'Bad Gateway',  
			503 => 'Service Unavailable',  
			504 => 'Gateway Timeout',  
			505 => 'HTTP Version Not Supported');
		return ($httpStatus[$statusCode]) ? $httpStatus[$statusCode] : $status[500];
	}
}
?>

User.php

Which is going to handle all the users table queries here. Here we have two functions. One is to get all the users table records and another one is to get the user record based in id.

<?php 

require_once("Db.inc.php");
	/**
	* 
	*/
	class User  extends Database
	{
		public $conn;

		public function __construct()
		{
			$this->conn = parent::__construct();
		}
		public function getUsers() {
			$query = "Select * from users";
			$stmt =  $this->conn->prepare($query);
			$stmt->execute();
			return $stmt->fetchAll();
		}

		public function getUser( $id ) {
			$query = "Select * from users where id = :id ";
			$stmt =  $this->conn->prepare($query);
			$stmt->bindParam(':id', $id, PDO::PARAM_INT);
			$stmt->execute();
			return $stmt->fetchObject();
		}
	}
?>

UserRestHandler.php

Which will handle all user request api process and return the result as per user request. The response will be Json, XML or HTML. By default html is a browser view. API will request only json or XML. Here we will get Http request content type and will respond as per.

<?php
require_once("SimpleRest.php");
require_once("User.php");


class UserRestHandler extends SimpleRest {

	function getUsers() {	

		$user = new User();
		$rawData = $user->getUsers();

		if(empty($rawData)) {
			$statusCode = 404;
			$rawData = array('error' => 'No Users found!');		
		} else {
			$statusCode = 200;
		}

		$requestContentType = $_SERVER['HTTP_ACCEPT'];
		$this ->setHttpHeaders($requestContentType, $statusCode);
				
		if(strpos($requestContentType,'application/json') !== false){
			$response = $this->encodeJson($rawData);
			echo $response;
		} else if(strpos($requestContentType,'text/html') !== false){
			$response = $this->encodeHtml($rawData);
			echo $response;
		} else if(strpos($requestContentType,'application/xml') !== false){
			$response = $this->encodeXml($rawData);
			echo $response;
		}
	}
	
	public function encodeHtml($responseData) {
	
		$htmlResponse = "<table border='1'>";
		foreach($responseData as $key=>$value) {
    			$htmlResponse .= "<tr><td>". $key. "</td><td>". $this->encodeJson($value). "</td></tr>";
		}
		$htmlResponse .= "</table>";
		return $htmlResponse;		
	}
	
	public function encodeJson($responseData) {
		$jsonResponse = json_encode($responseData);
		return $jsonResponse;		
	}
	
	public function encodeXml($responseData) {
		// creating object of SimpleXMLElement
		$xml = new SimpleXMLElement('<?xml version="1.0"?><user></user>');
		foreach($responseData as $key=>$value) {
			$xml->addChild($key, $value);
		}
		return $xml->asXML();
	}
	
	public function getUser($id) {

		$user = new User();
		$rawData = $user->getUser($id);

		if(empty($rawData)) {
			$statusCode = 404;
			$rawData = array('error' => 'No Users found!');		
		} else {
			$statusCode = 200;
		}

		$requestContentType = $_SERVER['HTTP_ACCEPT'];
		$this ->setHttpHeaders($requestContentType, $statusCode);
				
		if(strpos($requestContentType,'application/json') !== false){
			$response = $this->encodeJson($rawData);
			echo $response;
		} else if(strpos($requestContentType,'text/html') !== false){
			$response = $this->encodeHtml($rawData);
			echo $response;
		} else if(strpos($requestContentType,'application/xml') !== false){
			$response = $this->encodeXml($rawData);
			echo $response;
		}
	}
}
?>

RestController.php

Here we will map the request URL and will respond as per.

<?php
require_once("UserRestHandler.php");
		
$view = "";
if(isset($_GET["view"]))
	$view = $_GET["view"];

switch($view){

	case "all":
		// to handle REST Url /mobile/list/
		$userRestHandler = new UserRestHandler();
		$userRestHandler->getUsers();
		break;
		
	case "single":
		$userRestHandler = new UserRestHandler();
		$userRestHandler->getUser($_GET["id"]);
		break;

	case "" :
		//404 - not found;
		break;
}
?>

.htaccess

Mobile API will respond only for clean url’s. Here we will write the htaccess to clean the url\

# Turn rewrite engine on
Options +FollowSymlinks
RewriteEngine on

# map neat URL to internal URL
RewriteRule ^users/list/$   RestController.php?view=all [nc,qsa]
RewriteRule ^users/list/([0-9]+)/$   RestController.php?view=single&id=$1 [nc,qsa]

How to Test API Request :

Add ‘Advanced Rest Client’ chrome extension to your browser and open it.

1.Enter your URL :
For example : http://localhost:8888/Marimuthu/vue-php/Rest/users/list/

2. Select method as : GET

3. Under Raw headers Add “Accept: application/json”

4. Click Send button

Here you can see your result.

DEMO URLS :

To get all users : http://localhost:8888/Marimuthu/vue-php/Rest/users/list/

To get User by ID : http://localhost:8888/Marimuthu/vue-php/Rest/users/list/1/

Browser URL’s :

http://localhost:8888/Marimuthu/vue-php/Rest/RestController.php?view=all

http://localhost:8888/Marimuthu/vue-php/Rest/RestController.php?view=single&id=1

Hope it will help someone.

Thanks for reading this article if you like this don’t forget to share and comment.

Marimuthu: