PHP REST API ReadAll方法还给空响应 [英] PHP Rest API readAll gives back empty response

查看:207
本文介绍了PHP REST API ReadAll方法还给空响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次创造了RESTAPI。该API应该只能够处理一个请求,这使回的所有数据从一个表。我通过这个教程<一去href="http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/" rel="nofollow">http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/学习如何在PHP中的REST API。

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.

问题我遇到的是,API调用返回一个空的响应,我有麻烦找出错误所在。这里的C $ CS $:

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

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

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:

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:

.htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

当我测试的镀铬的要求先进的REST客户端也说:

When I test the request in chrome advanced Rest Client it say:

我也试过已经把一些回声在创建阵列的功能,并在Rspnse功能回音,但都没有显示了。

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

推荐答案

我发现这个问题。 该行 $食品= $ stmt-&GT; get_result(); 在DbHandler抛出一个异常,因为所需的驱动程序 mysqlidb wasn`t安装。

I found the problem. The line $foods = $stmt->get_result(); in the DbHandler threw an exception because the required driver mysqlidb wasn`t installed.

这篇关于PHP REST API ReadAll方法还给空响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆