如何解決php中curl和soap方式請求服務逾時的問題

不言
發布: 2023-03-31 14:42:01
原創
2270 人瀏覽過

本篇文章主要介紹了php中curl和soap方式請求服務超時問題的解決,內容挺不錯的,現在分享給大家,也給大家做個參考。

公司中有不少服務是以curl或soap方式連接第三方公司做的服務來交互數據,最近新增加了個需求,就是第三方服務發版時候,連接不上對方伺服器時候要進行重試,其它原因導致的業務處理失敗,則按失敗處理,不會再進行呼叫。

思路就是判斷curl或soap連線不上對方伺服器時候,拋出TimeoutException異常,捕獲後做重試處理,其它錯誤導致的拋出的Exception則按失敗處理。

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 availd" 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"," reachirectionlimit , aborting", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "Didn't receive an x​​ml 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, NULLmsg, 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, 用這個來判斷

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

如何將PHP陣列存入到資料庫中

三種php遞迴函數的實作方法及實現數字的累加

以上是如何解決php中curl和soap方式請求服務逾時的問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!