为什么我的 API REST 的 GET 函数不返回任何值? [英] Why my GET function of my API REST doesnt return any value?

查看:34
本文介绍了为什么我的 API REST 的 GET 函数不返回任何值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XAMPPPhpMyAdmin 来存储我的数据库,并且我正在尝试制作 API REST,但我没有得到好的结果.为此,我使用了 Slim 框架.

I'm using XAMPP with PhpMyAdmin to store my database and I'm trying to make an API REST, but I don't have good results. For that, I'm using Slim framework.

我将所有 Slim 框架文件放在 xampp 文件夹中的 /htdocs 中名为 project 的文件夹中,然后创建我的 connect.php 和我的 api.php 文件.我还更改了我的 index.php 以使用我之前提到的两个文件.

I put all Slim framework files in a folder named project inside /htdocs in the folder of xampp and I create my connect.php and my api.php files. I also change my index.php to use both files that I mentioned before.

我配置了我的 connect.phpindex.php 文件,并在 api.php 文件中创建了我的 GET 方法,但是,当我尝试获取数据库中的信息,但无法获取任何值.

I configurated my connect.php and index.php files and I created my GET method in the api.php file but, when I try to get the information that there is on the database I can't get any value.

这两个文件,我的意思是,connect.phpapi.php 都在一个名为 app 的文件夹中,而且它们在另一个文件夹中分别命名为 libsroutes.

Both files, I mean, connect.php and api.php are inside a folder named app and further they are in another folder named libs and routes respectively.

/project/app/libs/connect.php
/project/app/routes/api.php

我把所有的代码都放在这里了:

I put here all the code I have:

index.php

<?php

  require 'Slim/Slim.php'; 

  \Slim\Slim::registerAutoloader();

  $app = new \Slim\Slim();

  define("SPECIALCONSTANT",true);

  require 'app/libs/connect.php';
  require 'app/routes/api.php';

  $app->run();

?>

connect.php

<?php
if(!defined("SPECIALCONSTANT")) die("Access denied");

function getConnection()
{
    try{
        $db_username = "root";
        $db_password = "";
        $connection = new PDO("mysql:host=localhost;dbname=project",$db_username,$db_password);
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }catch(PDOException $e)
    {
        echo "Error: " . $e->getMessage();
    }

    return $connection;
}
?>

api.php

<?php
if(!defined("SPECIALCONSTANT")) die("Access denied");

$app->get("/users/",function() use($app)
{
    try{
        $connection = getConnection();
        $dbh = $connection->prepare("SELECT * FROM users");
        $dbh->execute();
        $users = $dbh->fetchAll();
        $connection = null;

        $app->response->headers->set("Content-type","application/json");
        $app->response->status(200);
        $app->response->body(json_encode($users));

    }catch(PDOException $e)
    {
        echo "Error: " . $e->getMessage();
    }
});

我的数据库名称是 project,我的数据库中的表名为 users.这个数据库保存在我的 phpmyadmin 上.

My database name is project and the table that I have in my database is named users. This database is hold on my phpmyadmin.

然后,我尝试使用我的 get 方法返回我在这个表上的值,并放入我的 url localhost/project/users/ 但它没有工作.我刚看到一个空白页,我在 Internet 上看到它必须提供我表中的值.

Then, I try to return the values that I have on this table with my get method putting in my url localhost/project/users/ but it doesn't work. I just saw a blank page and I saw on the Internet that it would have to give me the values that I have in my table.

我认为我的问题可能出在 headersstatusbody 上,因为我无法正确理解它们,所以我搜索了它们在文档中,但对我帮助不大.我搜索的页面是:

I though that my problem could be on headers, status and body because I couldn't understand them very properly and I search them in the documentation but it didn't help me a lot. The pages that I search were:

http://docs.slimframework.com/response/headers/http://docs.slimframework.com/response/status/ http://docs.slimframework.com/response/body/

所以,如果您还可以添加一些信息来阐明这些概念,我将非常感谢你们所有人.

So please if you can also add some info to clearify these concepts I will be so thankful with all of you.

我是 API REST 的新手,我看不到我的错误(我正在使用教程).为什么它不起作用?

I'm really new in API REST and I can't see my error (I'm using a tutorial). Why it doesn't work?

我尝试了 Abdo Adel 的建议,但也没有奏效.它给了我以下错误.

I tried the proposal of Abdo Adel but it also didn't work. It gives to me the following error.

详情

Type: ErrorException
Code: 8
Message: Undefined variable: users
File: C:\xampp\htdocs\proyecto\app\routes\api.php
Line: 21 --> Here it's the line when I have echo sentence

跟踪

0 C:\xampp\htdocs\project\app\routes\api.php(21): Slim\Slim::handleErrors(8, 'Undefined varia...', 'C:\\xampp\\htdocs...', 21, Array)
1 [internal function]: {closure}()
2 C:\xampp\htdocs\project\Slim\Route.php(468): call_user_func_array(Object(Closure), Array)
3 C:\xampp\htdocs\project\Slim\Slim.php(1357): Slim\Route->dispatch()
4 C:\xampp\htdocs\project\Slim\Middleware\Flash.php(85): Slim\Slim->call()
5 C:\xampp\htdocs\project\Slim\Middleware\MethodOverride.php(92): Slim\Middleware\Flash->call()
6 C:\xampp\htdocs\project\Slim\Middleware\PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()
7 C:\xampp\htdocs\project\Slim\Slim.php(1302): Slim\Middleware\PrettyExceptions->call()
8 C:\xampp\htdocs\project\index.php(19): Slim\Slim->run()
9 {main} 

现在我知道问题出在 json_encode 上,因为它给了我错误:JSON_ERROR_UTF8 当我使用 时json_last_error() .此刻我无法解决它.

Now I know that the problem it's on the json_encode because it gives me the error: JSON_ERROR_UTF8 when I use json_last_error(). By the moment I couldn't solve it.

推荐答案

change

$app->response->headers->set("Content-type","application/json");
$app->response->status(200);
$app->response->body(json_encode($users));

header("HTTP/1.1 200");
header("Content-Type:application/json; charset=utf-8");
echo json_encode($users);

这篇关于为什么我的 API REST 的 GET 函数不返回任何值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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