//Create the client object
$soapclient = new SoapClient( 'http://www.webservicex.net/globalweather.asmx?WSDL' );
// Use the functions of the client, the params of the function are in the associative array.
$params = array( 'CountryName' => 'Spain' );
$response = $soapclient->GetCitiesByCountry( $params );
var_dump( $response );
foreach ( $response as $key => $value ) {
echo $value;
}
通过使用foreach,我得到$value
中的所有字符串
SpainFuerteventura / Aeropuerto
SpainHierro / Aeropuerto
...
如何在一个数组中获取所有国家/地区名称,在另一个数组中获取所有城市名称?
示例:var_dump( $response );
object(stdClass)[2]
public 'GetCitiesByCountryResult' => string '<NewDataSet>
<Table>
<Country>Spain</Country>
<City>Fuerteventura / Aeropuerto</City>
</Table>
<Table>
<Country>Spain</Country>
<City>Hierro / Aeropuerto</City>
</Table>
<Table>
<Country>Spain</Country>
<City>La Palma / Aeropuerto</City>
</Table>
<Table>
<Country>Spain</Country>
<City>Las Palmas De Gran Canaria / Gando</City>
</Table>
<Table>
<Country>Spain</Country>
<City>Lanzarote / Aeropuerto</City>
</Table>
<Table>
<Country>Spain</Country'... (length=4383)
答案 0 :(得分:0)
这不是一个非常干净,而是一个简短而简单的解决方案,因为响应中只有一个国家/地区,我想(如果没有,您可以使用preg_split
代替explode
):
// get the response as a string, strip all tags but <Country>
$raw = strip_tags($response->GetCitiesByCountryResult,"<Country>");
// use the Country tag as delimiter
$cities = explode("<Country>$params[CountryName]</Country>",$raw);
// get rid of heading and trailing spaces
$cities = array_map("trim",$cities);
SoapClient PHP reference可能很方便。如果XML结构更复杂,那么SimpleXML或simplehtmldom解析器将是通用解决方案(正如Barmar在评论中所建议的那样)。