Search code examples
phpjsonlaravelutf-8mamp

Invalid UTF-8 sequence in argument with Laravel


I have a problem about encoding charset, the fact is, when I test my website in local (MAMP PHP 5.5.3) All works fine, my JSON object is returned without any problem. But in prod with server hosted by dreamhost (PHP 5.4) it doesn't work and throw me this error

json_encode(): Invalid UTF-8 sequence in argument.

(It's an error thrown by Laravel on file JsonResponse.php with the setData method).

So, How is it possible to have an error on my online servor and not on my local one?

EDIT:

This is my response header:

Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:1371
Content-Type:text/html; charset=UTF-8
Date:Thu, 02 Oct 2014 11:56:30 GMT
Expires:Thu, 02 Oct 2014 11:56:31 GMT
Keep-Alive:timeout=2, max=100
Server:Apache
Vary:Accept-Encoding,User-Agent

should be : Content-Type:application/json; charset=utf-8 ?

But I can not override the header.. This is not working :

$response->header('Content-Type', 'application/json');
$response->header('charset', 'utf-8');

Solution

  • I had setLocale('fr_FR') now I have setLocale('fr_FR.UTF8') and it solves the problem!

    Laravel doesn't throw any error anymore using json_encode() for any AJAX request. my strftime function return the right characters fo each letter with an accent.