I need to do some search on a WS, I have it working, but I need to be able to know when the soap can not find anything with the word I'm looking.
For example, when I search for KE-5977 I get a response and all the data
<GetChoferesResult xmlns:a="http://schemas.datacontract.org/2004/07/asdf.DTO" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Choferes>
<a:ChoferesDTO>
<a:dc_patente>KE-5977</a:dc_patente>
<a:dc_patente_habitual>KE-5977</a:dc_patente_habitual>
<a:dc_rut_chofer>10165014-6</a:dc_rut_chofer>
<a:dc_tipo_equipo>4</a:dc_tipo_equipo>
<a:dc_tipo_transportista>E</a:dc_tipo_transportista>
<a:dg_nombre_chofer>JUAN SOTO</a:dg_nombre_chofer>
<a:dg_tipo_de_equipo>Camión</a:dg_tipo_de_equipo>
<a:dg_tipo_transportista>Externo (Se llama a transporte ocasionalmente)</a:dg_tipo_transportista>
<a:dn_ano_fabricacion>1988</a:dn_ano_fabricacion>
<a:dq_capacidad_equipo>30.0000</a:dq_capacidad_equipo>
</a:ChoferesDTO>
</a:Choferes>
</GetChoferesResult>
but if I put it wrong like this "KE-7759" I get nothing.
<GetChoferesResult xmlns:a="http://schemas.datacontract.org/2004/07/asdf.DTO" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Choferes/>
</GetChoferesResult>
</GetChoferesResponse>
</s:Body>
</s:Envelope>
I need to show a message "code not found" when I get an empty response from soap (soap request did not found anything with that code).
Please help me.
Update:
This is taken from SoapUI: http://i.stack.imgur.com/oy4lc.jpg
In PHP I do it like this:
I ask if $isarray is array just because the response from soap can have multiple responses, to get the values when i get multiple responses i have to ask for "$response->GetChoferesResult->Choferes->ChoferesDTO", if i get only one response, the values are in "$response->GetChoferesResult->Choferes".
$response = $sClient->GetChoferes(array("req"=>array("PatenteHabitual"=>$patente)));
$isarray = $response->GetChoferesResult->Choferes->ChoferesDTO;
$xml = $response->GetChoferesResult->Choferes;
if (is_array($isarray)){
foreach($xml as $chofer){
foreach($chofer as $chofer2){
$rut=$chofer2->dc_rut_chofer;
$nombre=$chofer2->dg_nombre_chofer;
?><option value="<?php echo strtoupper($rut); ?>" selected="selected"><?php echo strtoupper($nombre); ?></option><?php
}
}
}else{
foreach($xml as $chofer){
if($chofer->dc_patente && $chofer->dg_nombre_chofer && $chofer->dc_patente_habitual){
$rut=$chofer->dc_rut_chofer;
$nombre=$chofer->dg_nombre_chofer;
?><option value="<?php echo strtoupper($rut); ?>" selected="selected"><?php echo strtoupper($nombre); ?></option><?php
}else{
?><option value="" selected="selected">Patente no encontrada</option><?php
}
}
}
Also, i would like to know if i can order the responses by a value, in this case by dg_nombre_chofer (by name).
Thanks.
You can use strpos($response, '<a:Choferes/>')
http://php.net/strpos
I use simplexml and convert the object to array using:
function objectsIntoArray($arrObjData, $arrSkipIndices = array()){
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
$process = $response->RetrieveListingDataResult;
$process = utf8_encode($process);
$xml = '<?xml version="1.0" encoding="UTF-8"?>'.$process;
$xmlObj = simplexml_load_string($xml);
$arrXml = objectsIntoArray($xmlObj);