This is the first time I am creating a restAPI. The API should only be able to process one request, which gives back all the data from an table. I went through this tutorial http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/ for learning how to create an Rest API in PHP.
The Problem I am experiencing is that the api call returns an empty response and I have trouble figuring out where the error is. here the codes:
Index.php
<?php
/**
* Created by IntelliJ IDEA.
* User: Jakob Abfalter
* Date: 19.08.14
* Time: 14:17
*/
require_once '../include/DbHandler.php';
require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
/**
* Echoing json response to client
* @param String $status_code Http response code
* @param Int $response Json response
*/
function echoRespnse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
/**
* Listing all foods
* method GET
* url /foods
*/
$app->get('/foods', function() {
$response = array();
$db = new DbHandler();
// fetching all foods
$result = $db->getAllFoods();
$response["error"] = false;
$response["foods"] = array();
// looping through result and preparing tasks array
while ($food = $result->fetch_assoc()) {
$tmp = array();
$tmp["id"] = $food["id"];
$tmp["name"] = $food["name"];
$tmp["img"] = $food["img"];
$tmp["description"] = $food["description"];
array_push($response["foods"], $tmp);
}
echoRespnse(200, $response);
});
$app->run();
?>
DbHandler.php
<?php
/**
* Created by IntelliJ IDEA.
* User: Jakob Abfalter
* Date: 19.08.14
* Time: 14:28
*/
class DbHandler
{
private $conn;
function __construct()
{
require_once dirname(__FILE__) . './DbConnect.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
/**
* Fetching all foods from database
*/
public function getAllFoods() {
$stmt = $this->conn->prepare("SELECT * FROM foods");
$stmt->execute();
$foods = $stmt->get_result();
$stmt->close();
return $foods;
}
}
DbConnect.php:
<?php
/**
* Created by IntelliJ IDEA.
* User: Jakob Abfalter
* Date: 19.08.14
* Time: 14:27
*/
class DbConnect {
private $conn;
function __construct() {
}
/**
* Establishing database connection
* @return database connection handler
*/
function connect() {
include_once dirname(__FILE__) . './Config.php';
// Connecting to mysql database
$this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// returing connection resource
return $this->conn;
}
}
?>
.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
When I test the request in chrome advanced Rest Client it say:
I also tried already to put some echos in the function where the array is created and an echo in the Rspnse function, but both didnt show up
I found the problem. The line
$foods = $stmt->get_result();
in the DbHandler threw an exception because the required drivermysqlidb
wasn`t installed.Oh actually I may have been wrong.
This looks like A problem if not THE problem.
Your method
getAllFoods()
closes the stmt handle$stmt->close();
You then pass the handle back to the calling code and attempt to process all the results using it.
You should not close the handle until you are finished processing the results from it.