Reputation: 626
This is the XML i get on my request to a server. It is a XML with some information about the current user. I want to check if it is more than one respons with TotalNumberOfResults. If that one is bigger than one i want to iterate and find the **Results->ResultItem->ResultType = Person**.
Each Result excict in a ResultItem. If it matches i want to pick rest of the information of that ResultItem(FirstName, LastName, Zip, etc).
This is the XML i get back from the request:
<SearchResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://secret.link”>
<ExtensionData/>
<Results>
<ResultItem>
<ExtensionData/>
<Addresses>
<Address_Search>
<ExtensionData/>
<AddressType>Visiting</AddressType>
<City>Oslo</City>
<Country>Norway</Country>
<FormattedAddress>The whole adress</FormattedAddress>
<HouseNumber>13</HouseNumber>
<MunicipalityCode>1234</MunicipalityCode>
<Street>Streetadress</Street>
<StreetId>12313</StreetId>
<Zip>1234</Zip>
</Address_Search>
</Addresses>
<BirthDate xsi:nil="true"/>
<Collection>Contacts</Collection>
<ContactPoints>
<ContactPoint_Search>
<ExtensionData/>
<Address>2312313</Address>
<ContactPointType>Mobile</ContactPointType>
<IsMain>true</IsMain>
</ContactPoint_Search>
<ContactPoint_Search>
<ExtensionData/>
<Address>[email protected]</Address>
<ContactPointType>Email</ContactPointType>
<IsMain>false</IsMain>
</ContactPoint_Search>
<ContactPoint_Search>
<ExtensionData/>
<Address>123123</Address>
<ContactPointType>Landline</ContactPointType>
<IsMain>false</IsMain>
</ContactPoint_Search>
<ContactPoint_Search>
<ExtensionData/>
<Address/>
<ContactPointType>Mobile</ContactPointType>
<IsMain>false</IsMain>
</ContactPoint_Search>
</ContactPoints>
<FirstName>Name</FirstName>
<ItemId>123123</ItemId>
<LastName>Name’sen</LastName>
<ResultType>Person</ResultType>
</ResultItem>
</Results>
<TotalNumberOfResults>1</TotalNumberOfResults>
</SearchResponse>
This is some of the server-php i have with this request:
$xml = simplexml_load_file($the_url_to_the_request);
$result = [];
if ($xml->TotalNumberOfResults > 1) {
foreach ($xml as $key => $value) {
if ($value->Results->ResultItem->ResultType == "Person") {
//var_dump("It's a match");
//var_dump($value->Results->ResultItem->ResultType);exit;
$result = array("Name" => $value->Results->ResultItem->FirstName . "", "Surname" => $value->Results->ResultItem->LastName . "", "ZIP" => $value->Results->ResultItem->Addresses->Address_Search->Zip . "", "By" => $value->Results->ResultItem->Addresses->Address_Search->City . "", "City" => $value->Results->ResultItem->Addresses->Address_Search->City . "");
echo($_GET['callback'] . "(" . json_encode($result) . ");");
}
}
}else{
$result = array("Name" => $xml->Results->ResultItem->FirstName . "", "Surname" => $xml->Results->ResultItem->LastName . "", "ZIP" => $xml->Results->ResultItem->Addresses->Address_Search->Zip . "", "City" => $xml->Results->ResultItem->Addresses->Address_Search->City . "", "By" => $xml->Results->ResultItem->Addresses->Address_Search->City . "");
echo($_GET['callback'] . "(" . json_encode($result) . ");");
}
The question: It's working just fine with one result, but nothing happends with multiple results.
Upvotes: 0
Views: 34
Reputation: 7205
You have to change element collection you iterate with foreach
loop. It should be $xml->Results
, not $xml
:
$xml = simplexml_load_file($the_url_to_the_request);
$result = [];
if ($xml->TotalNumberOfResults > 1) {
foreach ($xml->Results as $key => $value) {
if ($value->ResultItem->ResultType == "Person") {
//var_dump("It's a match");
//var_dump($value->ResultItem->ResultType);exit;
$result = array("Name" => $value->ResultItem->FirstName . "", "Surname" => $value->ResultItem->LastName . "", "ZIP" => $value->ResultItem->Addresses->Address_Search->Zip . "", "By" => $value->ResultItem->Addresses->Address_Search->City . "", "City" => $value->ResultItem->Addresses->Address_Search->City . "");
echo($_GET['callback'] . "(" . json_encode($result) . ");");
}
}
}
else {
$result = array("Name" => $xml->Results->ResultItem->FirstName . "", "Surname" => $xml->Results->ResultItem->LastName . "", "ZIP" => $xml->Results->ResultItem->Addresses->Address_Search->Zip . "", "City" => $xml->Results->ResultItem->Addresses->Address_Search->City . "", "By" => $xml->Results->ResultItem->Addresses->Address_Search->City . "");
echo($_GET['callback'] . "(" . json_encode($result) . ");");
}
Upvotes: 1