首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在php中使用soap类(举例说明)?

如何在php中使用soap类(举例说明)?
EN

Stack Overflow用户
提问于 2012-01-26 20:40:28
回答 1查看 66.7K关注 0票数 20

我想通过这个(weather)示例来学习SOAP的基本用法。如何处理这些数据呢?

请求:

代码语言:javascript
复制
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

响应:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-26 21:16:35

最简单的方法是:

代码语言:javascript
复制
$requestParams = array(
    'CityName' => 'Berlin',
    'CountryName' => 'Germany'
);

$client = new SoapClient('http://www.webservicex.net/globalweather.asmx?WSDL');
$response = $client->GetWeather($requestParams);

print_r($response);

将输出

代码语言:javascript
复制
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或类似的工具解析其余部分。

请注意,响应的类型特定于此web服务。有更好的web服务,它们不是简单地返回xml字符串,而是在WSDL中提供响应结构。

编辑一个“更结构化”的the服务的例子可以是同一站点上的GeoIP查找:

代码语言:javascript
复制
$client = new SoapClient('http://www.webservicex.net/geoipservice.asmx?WSDL');
$result = $client->GetGeoIP(array('IPAddress' => '8.8.8.8'));

print_r($result);

这为您提供了:

代码语言:javascript
复制
stdClass Object
(
    [GetGeoIPResult] => stdClass Object
        (
            [ReturnCode] => 1
            [IP] => 8.8.8.8
            [ReturnCodeDetails] => Success
            [CountryName] => United States
            [CountryCode] => USA
        )

)

现在,您可以简单地通过调用

代码语言:javascript
复制
$country = $result->GetGeoIPResult->CountryName;
票数 50
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9018236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档