为什么我的REST API的我GET功能犯规返回任何价值? [英] Why my GET function of my API REST doesnt return any value?
问题描述
我用 XAMPP
与 phpMyAdmin的
来存储我的数据库,我试图让一个REST API ,但我不会有好结果。对于这一点,我使用超薄框架。
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.
我把所有修身框架文件命名为项目
文件夹里面 / htdocs目录
<$ C的文件夹中$ C> XAMPP 和创建我的 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.php
和的index.php
文件和我创造了我GET方法 api.php
文件,但,当我试图得到的信息是有数据库,我不能得到任何价值的。
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.php
和 api.php
是一个名为文件夹内的应用
进一步它们在库
和路线
分别命名为另一个文件夹。
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
我把这里所有的code我有:
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();
}
});
我的数据库名称为项目
和我在我的数据库中的表被命名为用户
。这个数据库是坚持我的的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
方法在我的网址把值本地主机/项目/用户/
,但它不工作。我刚才看到一个空白页,我在网上看到,这不得不给我,我有我的表中的值。
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.
不过,我觉得我的问题可能是在标题
,状态
和体
,因为我无法很正确理解他们,我寻找他们的文档中,但它并没有帮助我很多。我搜索的网页是:
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/
所以,请,如果你还可以添加一些信息到clearify这些概念,我将与大家很感激。
So please if you can also add some info to clearify these concepts I will be so thankful with all of you.
我在REST API真正的新的,我不能看到我的错误(我使用的教程)。为什么它不工作?
I'm really new in API REST and I can't see my error (I'm using a tutorial). Why it doesn't work?
编辑:我试图阿卜杜阿德尔的建议,但它也没有工作。它给我下面的错误。
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}
EDIT2:现在我知道,这是对这个问题的 json_en code
,因为它给我的错误: 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.
推荐答案
改变
$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);
这篇关于为什么我的REST API的我GET功能犯规返回任何价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!