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 'àèìòù';



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire