2015-04-19 2 views
1
//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; 
} 

Используя Еогеасп я получаю все строки в $value какзначение доступа от ответа клиента SOAP

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

Я не уверен в вашем вопросе? вы хотите разбить строку на массив? – Sumit

+0

Да в массив. как '' значения тега в одном массиве и '' в другом. –

+1

'$ response-> GetCitiesByCountryResult' содержит эту строку XML. Используйте XML-парсер для его обработки. – Barmar

ответ

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 в комментарии).