I am facing to a parsing problem in PHP. Indeed, when I parse google suggest result in french, the url I am trying to parse is :
http://suggestqueries.google.com/complete/search?client=toolbar&hl=fr&q=exemple
I parse it with the following code:
$file = 'http://suggestqueries.google.com/complete/search?client=toolbar&hl=fr&q=exemple';
$xml =simplexml_load_string(utf8_encode(file_get_contents(urldecode($file ))));
print_r($xml);
And the results are oK.
When I do it in arabic, I replace lang and query. the code is following
$file = 'http://suggestqueries.google.com/complete/search?client=toolbar&hl=ar&q=من هو';
$xml =simplexml_load_string(utf8_encode(file_get_contents(urldecode($file ))));
print_r($xml);
The url is OK but I get a warning when I parse it
Warning: file_get_contents(http://suggestqueries.google.com/complete/search?client=toolbar&hl=ar&q=من هو): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
This problem has been solved thank to shukshin.ivan but I'm facing now to a displaying problem. The print_r return as following
SimpleXMLElement Object ( [CompleteSuggestion] => Array ( [0] => SimpleXMLElement Object ( [suggestion] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => ãä åæ ÇÑØÛÑá ) ) ) [1] => SimpleXMLElement Object ( [suggestion] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => ãä åæ ØÇÑÞ ÑãÖÇä ) ) ) [2] => SimpleXMLElement Object ( [suggestion] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => ãä åæ ãÍãæÏ ÇáÍÓäÇÊ ) ) ) [3] => SimpleXMLElement Object ( [suggestion] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => ãä åæ ÈÇÈÇ äæíá ) ) ) [4] => SimpleXMLElement Object ( [suggestion] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => ãä åæ ãÄÓÓ ÌæÌá ) ) ) [5] => SimpleXMLElement Object ( [suggestion] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => ãä åæ åãÇã ÍæÊ ) ) ) [6] => SimpleXMLElement Object ( [suggestion] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => ãä åæ Ðæ ÇáÞÑäíä ) ) ) [7] => SimpleXMLElement Object ( [suggestion] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => ãä åæ ÇÈæ Úáí ÇáÍÇßã ) ) ) [8] => SimpleXMLElement Object ( [suggestion] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => ãä åæ ÚãÑæ Èä ÚËãÇä ) ) ) [9] => SimpleXMLElement Object ( [suggestion] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => ãä åæ ÇÈæ ãÑÉ ) ) ) ) )
Is it encoding that is wrong? Meta charset of the page is utf8. When I do the same thing with the hardcoded xml file, everything is ok. Am I missing anything else?
If you are russian, I have the same problem with cyrillic character
$file = 'http://suggestqueries.google.com/complete/search?client=toolbar&hl=ru&q=' . urlencode('Я');
Thanks
You shouldn't urldecode
the whole url. You should urlencode
(encode, not decode) the string. The following code works fine.
$file = 'http://suggestqueries.google.com/complete/search?client=toolbar&hl=ar&q=' . urlencode('من هو');
$xml =simplexml_load_string(utf8_encode(file_get_contents($file )));
print_r($xml);