PHP Rest API readAll返回空响应

时间:2014-08-19 13:25:14

标签: php android mysql .htaccess rest

这是我第一次创建restAPI。 API应该只能处理一个请求,这会从表中返回所有数据。我浏览了本教程http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/,了解如何在PHP中创建Rest API。

我遇到的问题是api调用返回空响应,我无法确定错误的位置。这里的代码:

的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]

当我在chrome高级Rest Client中测试请求时,它说: enter image description here

我还尝试过在创建数组的函数中放入一些回声,并在Rspnse函数中使用回声,但两者都没有显示

2 个答案:

答案 0 :(得分:0)

实际上我可能错了。

如果不是问题,这看起来像一个问题。

您的方法getAllFoods()会关闭stmt句柄$stmt->close();

然后将句柄传递回调用代码并尝试使用它处理所有结果。

在完成处理结果之前,不应关闭手柄。

答案 1 :(得分:0)

我发现了问题。 DbHandler中的行$foods = $stmt->get_result();引发了异常,因为未安装所需的驱动程序mysqlidb