Skip to content

Commit f9b83d3

Browse files
Restful API php android
0 parents  commit f9b83d3

7 files changed

+242
-0
lines changed

Mobile.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
require_once("dbcontroller.php");
3+
/*
4+
A domain Class to Demonstrate RESTful web services
5+
*/
6+
Class Mobile{
7+
private $mobiles = array();
8+
9+
public function getAllMobile(){
10+
$query = "SELECT * FROM tbl_mobile";
11+
$dbcontroller = new DBController();
12+
$this->mobiles = $dbcontroller->executeSelectQuery($query);
13+
return $this->mobiles;
14+
}
15+
}
16+
?>

MobileRestHanler.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
require_once("SimpleRest.php");
3+
require_once("Mobile.php");
4+
5+
6+
class MobileRestHandler extends SimpleRest {
7+
8+
function getAllMobiles(){
9+
$mobile = new Mobile();
10+
$rawData = $mobile->getAllMobile();
11+
12+
if(empty($rawData)){
13+
$statusCode = 404;
14+
$rawData = array('error' => 'No Mobile Found!');
15+
}else{
16+
$statusCode = 200;
17+
}
18+
19+
$requestContentType = 'application/json'; //$_POST['HTTP_ACCEPT'];
20+
$this->setHttpHeaders($requestContentType,$statusCode);
21+
22+
$result["output"] = $rawData;
23+
if(strpos($requestContentType,'application/json') !== false){
24+
$response = $this->encodeJson($result);
25+
echo $response;
26+
27+
}else{
28+
$response = $this->encodeJson("ERROR");
29+
echo $response;
30+
}
31+
}
32+
33+
public function encodeJson($responseData){
34+
$jsonResponse = json_encode($responseData);
35+
return $jsonResponse;
36+
37+
}
38+
}
39+
?>

RestController.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
require_once("MobileRestHanler.php");
3+
4+
$view = "";
5+
6+
if(isset($_GET["view"]))
7+
{
8+
$view = $_GET['view'];
9+
10+
// Controls the RESTful Services URL mapping
11+
switch($view)
12+
{
13+
case "all":
14+
// to handle REST URL /mobile/list/
15+
$mobileResHandler = new MobileRestHandler();
16+
$mobileResHandler->getAllMobiles();
17+
break;
18+
19+
case "":
20+
// 404 Not Found
21+
break;
22+
}
23+
24+
}
25+
?>

SimpleRest.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/*
3+
A simple RESTful webservices base class
4+
Use this as a template and build upon it
5+
*/
6+
class SimpleRest {
7+
8+
private $httpVersion = "HTTP/1.1";
9+
10+
public function setHttpHeaders($contentType, $statusCode){
11+
12+
$statusMessage = $this -> getHttpStatusMessage($statusCode);
13+
14+
header($this->httpVersion. " ". $statusCode ." ". $statusMessage);
15+
header("Content-Type:". $contentType);
16+
}
17+
18+
public function getHttpStatusMessage($statusCode){
19+
$httpStatus = array(
20+
100 => 'Continue',
21+
101 => 'Switching Protocols',
22+
200 => 'OK',
23+
201 => 'Created',
24+
202 => 'Accepted',
25+
203 => 'Non-Authoritative Information',
26+
204 => 'No Content',
27+
205 => 'Reset Content',
28+
206 => 'Partial Content',
29+
300 => 'Multiple Choices',
30+
301 => 'Moved Permanently',
31+
302 => 'Found',
32+
303 => 'See Other',
33+
304 => 'Not Modified',
34+
305 => 'Use Proxy',
35+
306 => '(Unused)',
36+
307 => 'Temporary Redirect',
37+
400 => 'Bad Request',
38+
401 => 'Unauthorized',
39+
402 => 'Payment Required',
40+
403 => 'Forbidden',
41+
404 => 'Not Found',
42+
405 => 'Method Not Allowed',
43+
406 => 'Not Acceptable',
44+
407 => 'Proxy Authentication Required',
45+
408 => 'Request Timeout',
46+
409 => 'Conflict',
47+
410 => 'Gone',
48+
411 => 'Length Required',
49+
412 => 'Precondition Failed',
50+
413 => 'Request Entity Too Large',
51+
414 => 'Request-URI Too Long',
52+
415 => 'Unsupported Media Type',
53+
416 => 'Requested Range Not Satisfiable',
54+
417 => 'Expectation Failed',
55+
500 => 'Internal Server Error',
56+
501 => 'Not Implemented',
57+
502 => 'Bad Gateway',
58+
503 => 'Service Unavailable',
59+
504 => 'Gateway Timeout',
60+
505 => 'HTTP Version Not Supported');
61+
return ($httpStatus[$statusCode]) ? $httpStatus[$statusCode] : $status[500];
62+
}
63+
}
64+
?>

dbcontroller.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
class DBController {
3+
private $conn = "";
4+
private $host = "localhost";
5+
private $user = "root";
6+
private $password = "";
7+
private $database = "testpage";
8+
9+
function __construct() {
10+
$conn = $this->connectDB();
11+
if(!empty($conn)) {
12+
$this->conn = $conn;
13+
}
14+
}
15+
16+
function connectDB() {
17+
$conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
18+
return $conn;
19+
}
20+
21+
function executeSelectQuery($query) {
22+
$result = mysqli_query($this->conn,$query);
23+
//$a = mysqli_fetch_assoc($result);
24+
while($row=mysqli_fetch_assoc($result)) {
25+
$resultset[] = $row;
26+
}
27+
if(!empty($resultset))
28+
return $resultset;
29+
}
30+
}
31+
?>
47.1 KB
Binary file not shown.

tbl_mobile.sql

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
-- phpMyAdmin SQL Dump
2+
-- version 4.2.11
3+
-- http://www.phpmyadmin.net
4+
--
5+
-- Host: 127.0.0.1
6+
-- Generation Time: Feb 08, 2017 at 12:46 PM
7+
-- Server version: 5.6.21
8+
-- PHP Version: 5.6.3
9+
10+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11+
SET time_zone = "+00:00";
12+
13+
14+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
15+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
16+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
17+
/*!40101 SET NAMES utf8 */;
18+
19+
--
20+
-- Database: `blog_samples`
21+
--
22+
23+
-- --------------------------------------------------------
24+
25+
--
26+
-- Table structure for table `tbl_mobile`
27+
--
28+
29+
CREATE TABLE IF NOT EXISTS `tbl_mobile` (
30+
`id` int(11) NOT NULL,
31+
`name` varchar(255) NOT NULL
32+
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
33+
34+
--
35+
-- Dumping data for table `tbl_mobile`
36+
--
37+
38+
INSERT INTO `tbl_mobile` (`id`, `name`) VALUES
39+
(1, 'Apple iPhone 6S'),
40+
(2, 'Samsung Galaxy S6'),
41+
(3, 'Apple iPhone 6S Plus'),
42+
(4, 'LG G4'),
43+
(5, 'Moto G4 Plus'),
44+
(6, 'Panasonic p81');
45+
46+
--
47+
-- Indexes for dumped tables
48+
--
49+
50+
--
51+
-- Indexes for table `tbl_mobile`
52+
--
53+
ALTER TABLE `tbl_mobile`
54+
ADD PRIMARY KEY (`id`);
55+
56+
--
57+
-- AUTO_INCREMENT for dumped tables
58+
--
59+
60+
--
61+
-- AUTO_INCREMENT for table `tbl_mobile`
62+
--
63+
ALTER TABLE `tbl_mobile`
64+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7;
65+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
66+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
67+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

0 commit comments

Comments
 (0)