使用XML打印使用SOAP的特定元素

时间:2015-12-15 09:46:55

标签: php xml soap

我在PHP 5.3中使用了集成的SOAP客户端,并且我已经使用此Web服务进行了测试:http://www.webservicex.net/globalweather.asmx

我打电话给这种方法' GetWeather'并且XML请求如下所示:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetWeather xmlns="http://www.webserviceX.NET">
      <CityName>string</CityName>
      <CountryName>string</CountryName>
    </GetWeather>
  </soap:Body>
</soap:Envelope> 

和响应XML是这样的:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetWeatherResponse xmlns="http://www.webserviceX.NET">
      <GetWeatherResult>string</GetWeatherResult>
    </GetWeatherResponse>
  </soap:Body>
</soap:Envelope>

到目前为止,我已使用此代码成功获取数组:

<?php
    $client = new SoapClient('http://www.webservicex.net/globalweather.asmx?WSDL'); 
    $result = $client->GetWeather(array('CityName' => 'Barcelona', 'CountryName' => 'Spain'));
    print_r ($result);  

如何打印特定元素,让我们说出CityName的值?

所以,当我想要打印这样的城市时:

echo '<div> ' . $city . '</div>';

如何获取CityName的值?

0 个答案:

没有答案