使用 SimpleXML 解析 Web 服务响应失败


Parsing web service response with SimpleXML fails

我正在调用天气网络服务,我确实得到了响应,但我无法从响应中读取一个元素。

响应如下所示:

<string xmlns="http://www.webserviceX.NET">
<?xml version="1.0" encoding="utf-16"?>
    <CurrentWeather>
        <Location>Hamburg-Finkenwerder,Germany (EDHI) 53-32N 009-50E 13M</Location> 
        <Time>Sep 28, 2013 - 03:35 AM EDT / 2013.09.28 0735 UTC</Time>
        <Wind> Variable at 1 MPH (1 KT):0</Wind>
        <Visibility> less than 1 mile:0</Visibility>
        <SkyConditions> mostly cloudy</SkyConditions>
        <Temperature> 44 F (7 C)</Temperature>
        <DewPoint> 44 F (7 C)</DewPoint>
        <RelativeHumidity> 100%</RelativeHumidity>
        <Pressure> 30.03 in. Hg (1017 hPa)</Pressure>
        <Status>Success</Status>
    </CurrentWeather>
</string>

我的代码如下所示:

$url = "http://www.webservicex.com/globalweather.asmx/GetWeather?CityName=Hamburg&CountryName=Germany";
$options = array (CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "xml", // handle compressed
CURLOPT_USERAGENT => "test", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10 ); // stop after 10 redirects
$ch = curl_init ( $url );
curl_setopt_array ( $ch, $options );
$response = curl_exec ( $ch );
$err = curl_errno ( $ch );
$errmsg = curl_error ( $ch );
$header = curl_getinfo ( $ch );
$httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
curl_close ( $ch );
$xml = json_decode(json_encode((array) simplexml_load_string($response)), 1);
print_r($xml->SkyConditions);

基本上,我想读取SkyConditions的元素值。

$xml->SkyConditions返回 " 以及 $xml[0]->SkyConditions

我做错了什么?

好的,我搜索了更多,发现评论说这个 Web 服务根本没有返回有用的 xml 响应。我现在正在使用雅虎服务,这就像魅力一样。

无论如何,谢谢!