0

I'm trying to make an REST API with my GET function and I'm trying to retrieve the information of my database that I have in phpMyAdmin. I'm using Slim Framework.

The problem is when I try to retrieve the information of my database (that is encoded in utf8_general_ci) with json_encode. When I try to access to this GET function by URL, I just see a blank page. I found where the problem is, because I used a var_dump($users); at the line before the json_encode and it retrieved me the information that I have stored in my database.

I searched and I found that I could get the error that was giving my json_encode with json_last_error function so I did it.

echo json_encode($users);
echo json_last_error();

And it returns to me a 5, that means: JSON_ERROR_UTF8.

Then I searched and I saw that it is a problem of the encoding, that maybe it's made wrong but I couldn't get any solution to solve my problem.

My GET method of my REST API is:

<?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();
    }
});

What I tried:

  1. Change this:

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

    to this:

    header("HTTP/1.1 200");
    header("Content-Type:application/json; charset=utf-8");
    echo json_encode($users);
    
  2. Change the charset to utf8mb4 but it doesn't work.

  3. Tried to use json_encode($users, JSON_UNESCAPED_UNICODE ); but the result was the same.

I think the encode could be wrong because I use spanish characters on my database like ñ or ó é á ú but I saw that the recomended encode for my database with spanish characters was utf8_general_ci.

Does utf8_general_ci have a special charset? Do you know another way to retrieve the information from my database with json_encode? With all the possible solutions that I tried before I got a blank page.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • Check the content type that your database connection is using. In the Slim framework in particular. Perhaps it is pulling with a different charset, because json_encode should work with utf8, but may fail if the data presented to it is not encoded. – MiltoxBeyond Jun 01 '15 at 19:05
  • @MiltoxBeyond How can I check it? – Francisco Romero Jun 01 '15 at 19:07
  • Did you write the code yourself for the connection or use a ready made component. If you can post your connection code, without the actual login details, that would help. The mysql/mysqli creation is usually where you specify content type, but you can also do it afterwards like: http://php.net/manual/en/mysqli.set-charset.php – MiltoxBeyond Jun 01 '15 at 19:09
  • In other words: figure out how to set your ***database connection encoding*** to `utf8`. – deceze Jun 01 '15 at 19:10
  • @MiltoxBeyond I'm using a connection.php file. This is the structure of my API REST (in another question that I put before) http://stackoverflow.com/questions/30562186/why-my-get-function-of-my-api-rest-doesnt-return-any-value Here you can see these files. But for my database I'm using phpmyadmin. I noticed that I had the database with a different encoded that the tables inside of it, but I changed it and it also didn't work. – Francisco Romero Jun 01 '15 at 19:25
  • 1
    Your solution is here: http://stackoverflow.com/questions/4361459/php-pdo-charset-set-names. – MiltoxBeyond Jun 01 '15 at 19:35
  • @MiltoxBeyond Thank you man! Now it gives to me the information! But it gives me the string `Espa\u00f1a` instead of `España`. And it should work with utf8. Why? I saw in a lot of entires that I have to use utf8 in spanish characters. – Francisco Romero Jun 01 '15 at 20:51
  • 1
    It does that to prevent problems in transferring. You can use the JSON_UNESCAPED_UNICODE flag to return automatically the correct values as long as you are using PHP 5.3+ – MiltoxBeyond Jun 01 '15 at 21:34
  • @MiltoxBeyond Man, you are the best! Thank you for all! Now it works perfectly! – Francisco Romero Jun 01 '15 at 21:52

0 Answers0