0

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

响应如下所示:

<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.

我究竟做错了什么?

4

1 回答 1

0

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

不管怎么说,还是要谢谢你!

于 2013-09-29T16:11:05.623 回答