0
我打電話給天氣Web服務,但確實收到響應,但我無法從響應中讀取一個元素。使用SimpleXML解析Web服務響應失敗
的迴應是這樣的:
<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
。
我在做什麼錯?
您定義了'$ sUrl'並試圖在下面進一步使用'$ url'。 –
什麼是JSON編碼和解碼?和數組鑄造?你應該能夠自己加載該字符串,並獲取元素對象。 – halfer
@Amal:更正了我的複製和粘貼錯字。這不是問題。 – AntonSack