lundi 25 janvier 2016

How properly use utf8_decode at Laravel controller json response?

I'm in a struggle working with Laravel JSON response.

What I'm trying to do, is create a CURL request to Laravel controller.

So this is the CURL code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://dev.laravel/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

... and this is the controller code:

$data = array(
    'code' => ($this->code ? $this->code : 0),
    'message' => 'àèìòù',
    'data' => ''
);
return response()->json($data);

The problem is the message accentuation . But if I only return an string and use utf8_decode($output) the accentuation work as well, here's an example:

// curl
echo utf8_decode($output);

// laravel controller
return 'àèìòù';

[UPDATE]

Another example that doesn't work:

$response = array(
    'code' => 200,
    'message' => 'àèìòù',
    'data' => array()
);
return response()->json($response, 200, [], JSON_UNESCAPED_UNICODE);

{"code":200,"message":"à èìòù","data":[]} // result



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1PfteWe
via IFTTT

Aucun commentaire:

Enregistrer un commentaire