Simplexml_load_string()无法解析错误

时间:2010-05-24 18:26:48

标签: php simplexml google-weather-api

我正在尝试加载解析Google Weather API响应(中文回复)。

Here是API调用。

// This code fails with the following error
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=11791&hl=zh-CN');
  

(!)警告:simplexml_load_string()   [function.simplexml负载字符串]:   实体:第1行:解析器错误:输入   是不正确的UTF-8,表示编码   !字节:0xB6 0xE0 0xD4 0xC6 in   第11行的C:\ htdocs \ weather.php

为什么加载此响应会失败?

如何对响应进行编码/解码,以便simplexml正确加载?

编辑:以下是代码和输出。

<?php
$googleData = file_get_contents('http://www.google.com/ig/api?weather=11102&hl=zh-CN');
$xml = simplexml_load_string($googleData);
  

(!)警告:simplexml_load_string()   [function.simplexml负载字符串]:   实体:第1行:解析器错误:输入   是不正确的UTF-8,表示编码   !字节:0xB6 0xE0 0xD4 0xC6 in   第3行的C:\ htdocs \ test4.php调用   堆     时间记忆功能位置1 0.0020 314264 {main}(   ).. \ test4.php:0   2 0.1535 317520 simplexml_load_string   (string(1364)).. \ test4.php:3

     

(!)警告:simplexml_load_string()   [function.simplexml负载字符串]:   t_system   数据= “SI”/&GT;      

(!)警告:simplexml_load_string()   [function.simplexml-load-string]:^ in   第3行的C:\ htdocs \ test4.php调用   堆     时间记忆功能位置1 0.0020 314264 {main}(   ).. \ test4.php:0   2 0.1535 317520 simplexml_load_string   (string(1364)).. \ test4.php:3

5 个答案:

答案 0 :(得分:20)

这里的问题是SimpleXML没有查看HTTP标头来确定文档中使用的字符编码,只是假设它是UTF-8,即使Google的服务器确实将其宣传为

Content-Type: text/xml; charset=GB2312

您可以编写一个函数,使用超级机密魔术变量$http_response_header查看该标题,并相应地转换响应。这样的事情:

function sxe($url)
{   
    $xml = file_get_contents($url);
    foreach ($http_response_header as $header)
    {   
        if (preg_match('#^Content-Type: text/xml; charset=(.*)#i', $header, $m))
        {   
            switch (strtolower($m[1]))
            {   
                case 'utf-8':
                    // do nothing
                    break;

                case 'iso-8859-1':
                    $xml = utf8_encode($xml);
                    break;

                default:
                    $xml = iconv($m[1], 'utf-8', $xml);
            }
            break;
        }
    }

    return simplexml_load_string($xml);
}

答案 1 :(得分:5)

更新:我可以重现这个问题。此外,当我输出原始XML提要时,Firefox会自动将字符集嗅探为“简体中文”。 Google Feed正在提供不正确的数据(中文简体字符而不是UTF-8字符),或者在浏览器中未提取时提供不同的数据 - Firefox中的内容类型标题明确指出utf-8

将来自简体中文(GB18030,这是Firefox给我的)的传入Feed转换为UTF-8的工作原理:

 $incoming = file_get_contents('http://www.google.com/ig/api?weather=11791&hl=zh-CN');
 $xml = iconv("GB18030", "utf-8", $incoming);
 $xml = simplexml_load_string($xml);
但是,它还没有解释或解决潜在的问题。我现在没有时间仔细研究这个问题,也许其他人也会这样做。对我来说,看起来谷歌实际上是在提供不正确的数据(这会让我感到惊讶。我不知道他们犯了像我们凡人这样的错误。:P)

答案 2 :(得分:2)

刚刚遇到这个。 这似乎有效(我在网上找到的功能本身,只是更新了一点)。:

header('Content-Type: text/html; charset=utf-8'); 


function getWeather() {

$requestAddress = "http://www.google.com/ig/api?weather=11791&hl=zh-CN";
// Downloads weather data based on location.
$xml_str = file_get_contents($requestAddress,0);
$xml_str = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xml_str); 

$xml_str = iconv("GB18030", "utf-8", $xml_str);


// Parses XML
$xml = new SimplexmlElement($xml_str, TRUE);
// Loops XML
$count = 0;
echo '<div id="weather">';

foreach($xml->weather as $item) {

    foreach($item->forecast_conditions as $new) {

        echo "<div class=\"weatherIcon\">\n";
         echo "<img src='http://www.google.com/" .$new->icon['data'] . "'   alt='".$new->condition['data']."'/><br>\n";
        echo "<b>".$new->day_of_week['data']."</b><br>";
        echo "Low: ".$new->low['data']." &nbsp;High: ".$new->high['data']."<br>";
        echo "\n</div>\n";
        }

}

echo '</div>';
}


getWeather();

答案 3 :(得分:2)

这是我在php中用来解析Google Weather API的脚本。

 <?php

function sxe($url)
{
$xml = file_get_contents($url);
foreach ($http_response_header as $header)
{
if (preg_match('#^Content-Type: text/xml; charset=(.*)#i', $header, $m))
{
switch (strtolower($m[1]))
{

case 'utf-8':
// do nothing
break;

case 'iso-8859-1':
$xml = utf8_encode($xml);
break;

default:
$xml = iconv($m[1], 'utf-8', $xml);
}
break;
}
}
return simplexml_load_string($xml);
}


$xml = simplexml_load_file('http://www.google.com/ig/api?weather=46360&h1=en-us');
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast = $xml->xpath("/xml_api_reply/weather/forecast_conditions");


print "<br><br><center><div style=\"border: 1px solid; background-color: #dddddd; background-image: url('http://mc-pdfd-live.dyndns.org/images/clouds.bmp'); width: 450\">";


print "<br><h3>";
print $information[0]->city['data'] . "&nbsp;" . $information[0]->unit_system['data'] . "&nbsp;" .     $information[0]->postal_code['data'];
print "</h3>";
print "<div style=\"border: 1px solid; width: 320px\">";
print "<table cellpadding=\"5px\"><tr><td><h4>";
print "Now";
print "<br><br>";
print "<img src=http://www.google.com" . $current[0]->icon['data'] . ">&nbsp;";
print "</h4></td><td><h4>";
print "<br><br>";
print "&nbsp;" . $current[0]->condition['data'] . "&nbsp;";
print "&nbsp;" . $current[0]->temp_f['data'] . "&nbsp;°F";
print "<br>";
print "&nbsp;" . $current[0]->wind_condition['data'];
print "<br>";
print "&nbsp;" . $current[0]->humidity['data'];
print "<h4></td></tr></table></div>";




print "<table cellpadding=\"5px\"><tr><td>";


print "<table cellpadding=\"5px\"><tr><td><h4>";
print "Today";
print "<br><br>";
print "<img src=http://www.google.com" . $forecast[0]->icon['data'] . ">&nbsp;";
print "</h4></td><td><h4>";
print "<br><br>";
print  $forecast[0]->condition['data'];
print "<br>";
print  "High&nbsp;" . $forecast[0]->high['data'] . "&nbsp;°F";
print "<br>";
print  "Low&nbsp;" . $forecast[0]->low['data'] . "&nbsp;°F";
print "</h4></td></tr></table>";

print "<table cellpadding=\"5px\"><tr><td><h4>";
print  $forecast[2]->day_of_week['data'];
print "<br><br>";
print "<img src=http://www.google.com" . $forecast[2]->icon['data'] . ">&nbsp;";
print "</h4></td><td><h4>";
print "<br><br>";
print  "&nbsp;" . $forecast[2]->condition['data'];
print "<br>";
print  "&nbsp;High&nbsp;" . $forecast[2]->high['data'] . "&nbsp;°F";
print "<br>";
print  "&nbsp;Low&nbsp;" . $forecast[2]->low['data'] . "&nbsp;°F";
print "</h4></td></tr></table>";


print "</td><td>";


print "<table cellpadding=\"5px\"><tr><td><h4>";
print  $forecast[1]->day_of_week['data'];
print "<br><br>";
print "<img src=http://www.google.com" . $forecast[1]->icon['data'] . ">&nbsp;";
print "</h4></td><td><h4>";
print "<br><br>";
print  "&nbsp;" . $forecast[1]->condition['data'];
print "<br>";
print  "&nbsp;High&nbsp;" . $forecast[1]->high['data'] . "&nbsp;°F";
print "<br>";
print  "&nbsp;Low&nbsp;" . $forecast[1]->low['data'] . "&nbsp;°F";
print "</h4></td></tr></table>";

print "<table cellpadding=\"5px\"><tr><td><h4>";
print  $forecast[3]->day_of_week['data'];
print "<br><br>";
print "<img src=http://www.google.com" . $forecast[3]->icon['data'] . ">&nbsp;";
print "</h4></td><td><h4>";
print "<br><br>";
print  "&nbsp;" . $forecast[3]->condition['data'];
print "<br>";
print  "&nbsp;High&nbsp;" . $forecast[3]->high['data'] . "&nbsp;°F";
print "<br>";
print  "&nbsp;Low&nbsp;" . $forecast[3]->low['data'] . "&nbsp;°F";
print "</h4></td></tr></table>";


print "</td></tr></table>";


print "</div></center>";


?>

答案 4 :(得分:1)

尝试添加url查询参数eo = utf-8。在这种情况下,答案将是UTF-8编码。它帮助了我。

http://www.google.com/ig/api?weather=?????&degree=??????&oe=utf-8&hl=es