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.
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.
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.
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();
}
});
My database name is project
and the table that I have in my database is named users
. This database is hold on my phpmyadmin
.
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/
So please if you can also add some info to clearify these concepts I will be so thankful with all of you.
I'm really new in API REST and I can't see my error (I'm using a tutorial). Why it doesn't work?
EDIT: I tried the proposal of Abdo Adel but it also didn't work. It gives to me the following error.
Details
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
Trace
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: 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
to