2
I’m using the Laravel 3
in a given system.
Sometimes I’m having a problem with the json_encode
, returning false
in some cases.
In this code, I upload an external page and, with the DomDocument
, make a foreach
in the meta tags and capture the value of the content, saving in a array
.
That one array
I use the Response::json
of Laravel
, that internally uses the json_encode
.
Sort of like this:
$url = Input::get('url');
$html = file_get_contents($url);
$dom = new DOMDocument();
@$dom->loadHtml('<?xml encoding="UTF-8" version="1.0"?>' . $html);
$dados = array();
foreach ($dom->getElementsByTagName('meta') as $element) {
$name = trim($element->getAttribute('property'));
if (! $name || strpos($name, 'og:') === false) continue;
$dados[$name] = $element->getAttribute('content');
}
return Response::json($dados);
When I use Response::json
, is returning me empty, in some cases.
So I did the following check to find out what was wrong:
$json = json_encode($dados);
if ($json === false) {
echo json_last_error_msg();
}
And he returned:
Malformed UTF-8 characters, possibly incorrectly encoded
I checked the contents of the variable $dados
, and she was like this:
Array
(
[og:title] => **Removido**
[og:description] => Os Dez Mandamentos: chuva de granizo e fogo � a sétima praga a castigar o Egito
[og:image] => **Removido**
)
It seems that the problem is being generated because of this character �
.
Does anyone know how to get around this problem?
Updating
I did the test of trying to print the html content with DomDocument
, using $dom->saveHTML()
and I was returned this mistake:
output Conversion failed due to conv error, bytes 0xE9 0x20 0x61 0x20
The strange thing is that the lyrics
é
appears insétima
, but after the wordfogo
is returning the�
– Wallace Maxters