Laravel "Malformed UTF-8 characters"

Asked

Viewed 962 times

0

I’m using Laravel in version 5.4 (PHP 5.6) to develop an API.

Recently, when the API was published in Umbler (Linux), when trying to access endpoints that return strings with special characters (more specifically, ã, í) and/or Uuids is released the following Exception

Malformed UTF-8 characters, possibly incorrectly encoded

The function of the controller being called is the following:

public function index(Request $request)
    {
         $response = $this->service->get($request->all());

         return response()->json([
             'message' => 'Success',
             'data' => $response['paginated']->getCollection(),
             'next_page' => $response['paginated']->nextPageUrl(),
             'prev_page' => $response['paginated']->previousPageUrl(),
             'current_page' => $response['paginated']->currentPage(),
             'page_count' => $response['paginated']->lastPage(),
         ]);
    }

I tried to use accessors of the Laravel, in the entities to return the data in the following ways:

public function getIdAttribute($value) {
    return utf8_encode(Uuid::import($value)->string);
}

public function getValorAttribute($value) {
    return utf8_encode($value);
}

But the problem persists.

I noticed that when climbing the Builtin Web Server (php artisan serve) on a Windows server the error does not occur, but when using Apache or Nginx, it happens again.

Obs.: On this same Windows server, when adding AddDefaultCharset UTF-8 apache settings, the problem has been fixed, but Ubuntu does not.

Somebody’s had that kind of trouble and they know how to fix it?

  • If the method that generates this information, I need to see it!

  • Okay, I just edited the question! :)

  • what returns getCollection()?

  • The Collection of the Entity in question, only paged. The problem occurs in Collections without paging as well, provided they have special characters.

  • First thing don’t need to put utf8 in the methods that already have problem, getCollection, tranform to array can be this.

  • Your problem is with the base! first do not need conversions within the model this way, in rare cases we do this, because, really do not need and I believe that your problem is to return this paginated data, if you can take everything and put simple and test!

  • I removed the accessors with utf8_encode(), it was only an attempt even, I did not keep in the code

  • I passed $Response->toArray() and got the same expcetion, the special characters are returned as follows: array('message' => 'Success', 'data' => array(array('id' => 'R9 Q fde,'...

  • the error is higher! is not in code ai! is in database, and environment settings! how is your database configured? and your connection.... as it is configured, see, the error is before! is not there

  • in config/database.php I have the following database settings & #Xa; 'sqlsrv' => [ 'driver' => 'sqlsrv', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '1433'), 'database' => env('DB_DATABASE', '----'), 'username' => env('DB_USERNAME', '----'), 'password' => env('DB_PASSWORD', '----'), 'charset' => 'utf8', 'collation' => 'Sql_latin1_general_cp1_ci_as', 'prefix' => ', ] The collation was copied from the database properties, which was generated via Migration.

  • Then check on the server if the settings are equal

  • I believe that in the hosting service I am currently using I do not have access to server settings, but they say that there Apache and PHP by default use UTF-8. When analyzing phpinfo() and comparing with my Windows Server I noticed that in the Registered PHP Streams section there is no option 'sqlsrv', this may have some influence?

  • Kaio maybe do not know, I think not, need to see how is configured this part of the bank. This you should check

  • 1

    Thank you so much for your help, Virgil! When I can solve it, I put here the answer.

Show 9 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.