問題描述
我想通過這個(weather)示例了解 SOAP 的基本用法.處理這些數據的價值如何?
I would like to learn the basic usage of SOAP through this (weather) example. How is it worthy to process this data?
請求:
POST /globalweather.asmx HTTP/1.1
Host: www.webservicex.net
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetWeather xmlns="http://www.webserviceX.NET">
<CityName>string</CityName>
<CountryName>string</CountryName>
</GetWeather>
</soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
回復:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetWeatherResponse xmlns="http://www.webserviceX.NET">
<GetWeatherResult>string</GetWeatherResult>
</GetWeatherResponse>
</soap12:Body>
</soap12:Envelope>
推薦答案
最簡單的方法是:
$requestParams = array(
'CityName' => 'Berlin',
'CountryName' => 'Germany'
);
$client = new SoapClient('http://www.webservicex.net/globalweather.asmx?WSDL');
$response = $client->GetWeather($requestParams);
print_r($response);
會輸出
stdClass Object
(
[GetWeatherResult] => <?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>Berlin-Tegel, Germany (EDDT) 52-34N 013-19E 37M</Location>
<Time>Jan 26, 2012 - 07:50 AM EST / 2012.01.26 1250 UTC</Time>
<Wind> from the SE (130 degrees) at 14 MPH (12 KT):0</Wind>
<Visibility> greater than 7 mile(s):0</Visibility>
<SkyConditions> mostly clear</SkyConditions>
<Temperature> 33 F (1 C)</Temperature>
<Wind>Windchill: 23 F (-5 C):1</Wind>
<DewPoint> 21 F (-6 C)</DewPoint>
<RelativeHumidity> 59%</RelativeHumidity>
<Pressure> 30.27 in. Hg (1025 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>
)
其余的可以用 SimpleXML 或類似的東西解析.
The rest can then be parsed with SimpleXML or something similar.
請注意,響應類型特定于此 Web 服務.有更好的 Web 服務,它們不只是返回一個 xml 字符串,而是在 WSDL 中提供響應結構.
Note, that the kind of response is specific to this web service. There are better web services out there, which do not simply return an xml string, but rather provide the response structure within the WSDL.
編輯更結構化"網絡服務的一個例子可能是同一站點上的 GeoIP 查找:
EDIT An example for a "more structured" webservice could be the GeoIP lookup on the same site:
$client = new SoapClient('http://www.webservicex.net/geoipservice.asmx?WSDL');
$result = $client->GetGeoIP(array('IPAddress' => '8.8.8.8'));
print_r($result);
這給你:
stdClass Object
(
[GetGeoIPResult] => stdClass Object
(
[ReturnCode] => 1
[IP] => 8.8.8.8
[ReturnCodeDetails] => Success
[CountryName] => United States
[CountryCode] => USA
)
)
現在您可以簡單地通過調用來訪問這些值
Now you can simply access the values by invoking
$country = $result->GetGeoIPResult->CountryName;
這篇關于如何在php中使用soap類(舉例)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!