2012-07-26 144 views
1

我有興趣通過php的soapClient將肥皂呼叫到Web服務,以便從監控站獲取水位。我想處理在執行過程中發生的兩個soapfaults。第一個錯誤如下:如何在php soap客戶端請求期間處理多個SOAP錯誤?

SoapFault exception: [soapenv:Server.userException] java.rmi.RemoteException: We are sorry, but no data is available from this station at this time in C:\xampp\htdocs\NOAA\LogWriter.php:214 Stack trace: #0 C:\xampp\htdocs\NOAA\LogWriter.php(214): SoapClient->__soapCall('getWaterLevelRa...', Array, Array) #1 C:\xampp\htdocs\NOAA\LogWriter.php(188): getLevel('8531680', '20120726 15:19') #2 {main} 

如果某段時間的數據不可用,則預期會在腳本中發生多次此錯誤。我需要抓住這個錯誤,以便讓腳本在新的時間再次嘗試。我使用了一個catch塊來做到這一點。

我還需要捕獲第二個錯誤,如果webservice沒有加載wsdl文件或服務器超時。爲了測試這個已經給我的腳本faultly位置產生以前我曾收到了同樣的錯誤,這是如下:

Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://opendap.co-ops.nos.noaa.gov/axis/services/WaterLevelRawOneMin?wsdl' : Extra content at the end of the document in C:\xampp\htdocs\NOAA\LogWriter.php:210 Stack trace: #0 C:\xampp\htdocs\NOAA\LogWriter.php(210): SoapClient->SoapClient('http://opendap....', Array) #1 C:\xampp\htdocs\NOAA\LogWriter.php(171): getLevel('8531680', '20120726 12:35') #2 {main} thrown in C:\xampp\htdocs\NOAA\LogWriter.php on line 210 

第二個錯誤仍然未捕獲並終止我的腳本。不過,我需要捕捉它並顯示一條消息。

我已經發布了我的php函數,使得下面的肥皂調用。

任何人都可以給我任何想法如何做到這一點?只有當你調用SoapClient的可發生

function getLevel($id, $date) { 

    $client = new SoapClient("http://opendap.co-ops.nos.noaa.gov/axis/services/WaterLevelRawOneMin?wsdl", array('trace' => false)); 

    $Parameters = array("stationId" => $id, "beginDate" => $date, "endDate" => $date, "datum" => "MLLW", 
         "unit"  => 1, "timeZone" => 1); 

    try { 
     return $client->__soapCall(
      "getWaterLevelRawOneMin", array('Parameters' => $Parameters), 
      array('location' => "http://opendap.co-ops.nos.noaa.gov/axis/services/WaterLevelRawOneMin") 
     ); 
    } catch (SoapFault $e) { 
     if (
      $e->faultcode == "soapenv:Server.userException" 
      and $e->faultstring == "java.rmi.RemoteException: We are sorry, but no data is available from this station at this time" 
     ) { 
      return "FAULT"; 
     } else { 
      echo "Could not connect to the server"; 
     } 
    } // end of catch blocK 
}// end of function 

回答

1

關於打破WSDL異常:構造函數,以便

try { 
    $client= new SoapClient($wsdlUrl ,array('trace'=>false)); 
}catch(Exception $e) { 
    // your loging regarding this case 
} 
當你犯了一個Web服務都讓可發生

的SOAPFault例外:

try { 
    $client= new SoapClient($wsdlUrl ,array('trace'=>false)); 
    try { 
     return $client->_call('....'); 
    } catch (SoapFault $sp) { 
     //your logic rearding soap fault 
    } 
}catch(Exception $e) { 
    // your loging regarding this case 
} 
return false; 
+0

謝謝你它很好地解決了我的問題。我是新來的SOAP調用,所以我被拋出的消息說未捕獲的soapfault異常。再次感謝你。 – 2012-07-28 02:52:56

+0

很高興聽到 - 伊山。 – mrok 2012-07-28 19:22:47