今天就跟大家聊聊有關php中curl和soap方式請求服務超時如何解決,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

curl處理
$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);soap處理
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());
}
}看完上述內容,你們對php中curl和soap方式請求服務超時如何解決有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創新互聯行業資訊頻道,感謝大家的支持。
網站題目:php中curl和soap方式請求服務超時如何解決-創新互聯
文章位置:http://www.yijiale78.com/article30/dodiso.html
成都網站建設公司_創新互聯,為您提供網站改版、外貿建站、標簽優化、服務器托管、商城網站、網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯