[英]php - do something on soap empty response or not found
我需要在WS上進行一些搜索,並且可以正常工作,但是我需要能夠知道肥皂何時找不到我所尋找的單詞。
例如,當我搜索KE-5977時,我會得到響應和所有數據
<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>
但是,如果我這樣說錯了“ KE-7759”,我什么也得不到。
<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>
當我收到來自Soap的空響應時,我需要顯示一條消息“未找到代碼”(Soap請求未找到帶有該代碼的任何內容)。
請幫我。
更新:
這取自SoapUI: http : //i.stack.imgur.com/oy4lc.jpg
在PHP中,我這樣做:
我問$ isarray是否是數組,僅僅是因為soap的響應可以有多個響應,如果我得到多個響應,要獲取值,我只能要求“ $ response-> GetChoferesResult-> Choferes-> ChoferesDTO”一個響應,值在“ $ 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
}
}
}
另外,我想知道是否可以按值排序響應,在這種情況下,可以按dg_nombre_chofer(按名稱)排序。
謝謝。
可以使用strpos($response, '<a:Choferes/>')
http://php.net/strpos
我使用simplexml並使用以下方法將對象轉換為數組:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.