doubinei1457
doubinei1457
2016-12-15 03:32

xml simplexml_load_file不解析特殊或其他语言字符

已采纳

I am using below google suggestion query and it return xml format output, in this output it contains special or other language characters.

http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=facebook+e

It gives below xml format output...

<toplevel>
<CompleteSuggestion>
<suggestion data="facebook en español"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="facebook emojis"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="facebook event photo size"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="facebook emoticons"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="facebook email"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="facebook events"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="facebook earnings"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="facebook earnings date"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="facebook email address"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="facebook event banner size"/>
</CompleteSuggestion>
</toplevel>

When I use below code to load this xml file foreach I am getting error ...

foreach ($xml->children() as $child) {
foreach ($child->suggestion->attributes() as $dta) {
echo "<br>";
echo $dta;
}
}

output is getting below error message...

Warning: simplexml_load_file():
http://suggestqueries.google.com/complete/search?
output=toolbar&hl=en&q=facebook.'%20'.e:1: parser error : Input is not  
proper UTF-8, indicate encoding ! Bytes: 0xF1 0x6F 0x6C 0x22 in 

Warning: simplexml_load_file(): es"/></CompleteSuggestion>
<CompleteSuggestion><suggestion data="facebook en espa in 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doumi1937 doumi1937 5年前

    You should encode in UTF-8 the response of google suggestion query before trying to parse it.

    $encodedResponse = utf8_encode(file_get_contents('http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=facebook+e'));
    $xml = simplexml_load_string($encodedResponse);
    
    点赞 评论 复制链接分享