Home  >  Article  >  Backend Development  >  How to solve the problem of curl and soap request service timeout in PHP

How to solve the problem of curl and soap request service timeout in PHP

不言
不言Original
2018-06-11 14:00:272258browse

This article mainly introduces the solution to the problem of curl and soap request service timeout in PHP. The content is quite good. I will share it with you now and give it as a reference.

Many services in the company use curl or soap to connect to services provided by third-party companies to exchange data. Recently, a new requirement has been added, that is, when the third-party service is released, it cannot connect to the other party's server. To retry, if the business processing fails due to other reasons, it will be handled as a failure and will not be called again.

The idea is to judge that curl or soap cannot connect to the other party's server, throw a TimeoutException exception, and retry it after catching it. Exceptions thrown due to other errors will be treated as failures.

curl processing

  $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 processing

The php document does not write in detail about soap timeout or failure to connect The specific code returned will throw a SoapFault exception if the business processing fails or the connection fails. After looking at the source code of php, I found that there is still a defined

php source file location /ext/soap /php_http.c

Define error code content

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) ;

It can be seen from the code that if the connection fails, an HTTP code will be returned. soap does not have a specific code to distinguish the two like curl. You can only use this code to determine It is a network problem such as timeout or failure to connect.

The specific code is as follows

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());
      }
    }


You can connect to the soap service, but the client or server fails. The problem $e->faultcode will return WSDL, use this to judge

The above is the entire content of this article, I hope it will be helpful to everyone's learning, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

How to store PHP arrays into the database

How to implement three PHP recursive functions And realize the accumulation of numbers

#

The above is the detailed content of How to solve the problem of curl and soap request service timeout in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn