//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;
}
By using foreach i am getting all string in a $value
like
SpainFuerteventura / Aeropuerto
SpainHierro / Aeropuerto
...
How do i can get all country names in one array and all city names in another array?
sample: 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)
This is not a very clean, but a short and simple solution, since there always is just one country in the response, I suppose (if not, you could use preg_split
instead of explode
below):
// 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);
The SoapClient PHP reference might be handy. If the XML structure were more complex, than SimpleXML or simplehtmldom parser would be universal solution (as Barmar suggests in the comment).