2

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

0

1 Answer 1

0

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);
5
  • Thank you but the problem is that character displayed by the print_r are wrong... Which encoding do I have to use to display it on a page? Feb 6, 2018 at 10:06
  • Well, I've helped with the problem set: http 400 error. Feb 6, 2018 at 10:36
  • Let me check it. Feb 6, 2018 at 10:48
  • Do you think it could be my browser? Feb 6, 2018 at 11:02
  • Well, kind of encoding issue, I see correct russian suggestions in my browser. Anyway, you'd better split the problem into two tasks. The first one (http problem) is solved and my answer should be accepted as right. The second problem(encoding) should be investigated in different question and need some debugging work. Feb 6, 2018 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.