公司中有很多服務是以curl或者soap方式鏈接第三方公司作的服務來交互數據,最近新增長了個需求,就是第三方服務發版時候,鏈接不上對方服務器時候要進行重試,其它緣由致使的業務處理失敗,則按失敗處理,不會再進行調用。
思路就是判斷curl或者soap鏈接不上對方服務器時候,拋出TimeoutException異常,捕獲後作重試處理,其它錯誤致使的拋出的Exception則按失敗處理。php
$ch = curl_init($url); $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 5, //5秒鏈接時間 CURLOPT_TIMEOUT => 30, //30秒請求等待時間 ); curl_setopt_array($ch, $options); $response = curl_exec($ch); if ($no = curl_errno($ch)) { $error = curl_error($ch); curl_close($ch); //$no錯誤碼7爲鏈接不上,28爲鏈接上了但請求返回結果超時 if(in_array(intval($no), [7, 28], true)) { throw new TimeoutException('鏈接或請求超時' . $error, $no); } } curl_close($ch);
php文檔並沒詳細寫soap超時或者鏈接不上返回的具體代碼,業務處理失敗或者鏈接不上等全部不成功,都會拋出一個SoapFault異常,看了下php的源碼發現,仍是有定義的服務器
php源文件位置 /ext/soap/php_http.c 定義錯誤代碼內容 add_soap_fault(this_ptr, "HTTP", "Unable to parse URL", NULL, NULL); add_soap_fault(this_ptr, "HTTP", "Unknown protocol. Only http and https are allowed.", NULL, NULL); add_soap_fault(this_ptr, "HTTP", "SSL support is not available in this build", NULL, NULL); add_soap_fault(this_ptr, "HTTP", "Could not connect to host", NULL, NULL); add_soap_fault(this_ptr, "HTTP", "Failed Sending HTTP SOAP request", NULL, NULL); add_soap_fault(this_ptr, "HTTP", "Failed to create stream??", NULL, NULL); add_soap_fault(this_ptr, "HTTP", "Error Fetching http headers", NULL, NULL); add_soap_fault(this_ptr, "HTTP", "Error Fetching http body, No Content-Length, connection closed or chunked data", NULL, NULL); add_soap_fault(this_ptr, "HTTP", "Redirection limit reached, aborting", NULL, NULL); add_soap_fault(this_ptr, "HTTP", "Didn't receive an xml document", NULL, err); add_soap_fault(this_ptr, "HTTP", "Unknown Content-Encoding", NULL, NULL); add_soap_fault(this_ptr, "HTTP", "Can't uncompress compressed response", NULL, NULL); add_soap_fault(this_ptr, "HTTP", http_msg, NULL, NULL);
從代碼裏能夠看出來,鏈接不上都會返回一個HTTP碼,soap並沒像curl那樣有具體的代碼能夠區分兩者,只利用這個碼能夠判斷是超時或者鏈接不上等網絡問題
具體代碼以下網絡
ini_set('default_socket_timeout', 30); //定義響應超時爲30秒 try { $options = array( 'cache_wsdl' => 0, 'connection_timeout' => 5, //定義鏈接超時爲5秒 ); libxml_disable_entity_loader(false); $client = new \SoapClient($url, $options); return $client->__soapCall($function_name, $arguments); } catch (\SoapFault $e) { //超時、鏈接不上 if($e->faultcode == 'HTTP'){ throw new TimeoutException('鏈接或請求超時', $e->getCode()); } }
能夠鏈接上soap服務,但客戶端或者服務端出問題 $e->faultcode 會返回WSDL, 用這個來判斷curl
以上爲php使用soap和curl捕獲請求超時和鏈接超時的方法。socket