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

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為連接上了但請求返回結(jié)果超時
if(in_array(intval($no), [7, 28], true)) {
throw new TimeoutException('連接或請求超時' . $error, $no);
}
}
curl_close($ch);soap處理
php文檔并沒詳細寫soap超時或者連接不上返回的具體代碼,業(yè)務處理失敗或者連接不上等所有不成功,都會拋出一個SoapFault異常,看了下php的源碼發(fā)現(xiàn),還是有定義的
php源文件位置 /ext/soap/php_http.c
定義錯誤代碼內(nèi)容
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那樣有具體的代碼可以區(qū)分二者,只利用這個碼可以判斷是超時或者連接不上等網(wǎng)絡問題
具體代碼如下
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());
}
}看完上述內(nèi)容,你們對php中curl和soap方式請求服務超時如何解決有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)站題目:php中curl和soap方式請求服務超時如何解決-創(chuàng)新互聯(lián)
文章位置:http://www.chinadenli.net/article30/dodiso.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、外貿(mào)建站、標簽優(yōu)化、服務器托管、商城網(wǎng)站、網(wǎng)站設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)