Solution to the Time out Problem of curl and soap Request Service in php

  • 2021-10-15 09:56:43
  • OfStack

Many services in the company are connected to the services made by the third-party company in curl or soap to exchange data. Recently, a new requirement has been added, that is, when the third-party service is released, it should be retried when it cannot connect to the other server. If the business processing fails due to other reasons, it will be treated as failure and will not be called again.

The idea is to judge that when curl or soap can't connect to the other server, throw TimeoutException exception, retry after capture, and throw Exception caused by other errors as failure.

curl processing


  $ch = curl_init($url);
    $options = array(
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_CONNECTTIMEOUT => 5, //5 Second connection time 
      CURLOPT_TIMEOUT    => 30, //30 Seconds request wait time 
    );
    
    curl_setopt_array($ch, $options);
    $response = curl_exec($ch);
    if ($no = curl_errno($ch)) {
      $error = curl_error($ch);
      curl_close($ch);
      //$no Error code 7 To be unable to connect, 28 Timeout for connection but request return result 
      if(in_array(intval($no), [7, 28], true)) {
        throw new TimeoutException(' Connection or request timeout ' . $error, $no);
      }
    }
    curl_close($ch);

soap treatment

php document does not write in detail soap timeout or connection can not return the specific code, business processing failure or connection can not be successful, will throw an SoapFault exception, see the next php source found, or 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 you can't connect, you will return an HTTP code. soap doesn't have a specific code like curl, which can distinguish the two. Only use this code to judge whether it is a network problem such as timeout or poor connection

The specific code is as follows


ini_set('default_socket_timeout', 30); // Define the response timeout as 30 Seconds 

    try {
      $options = array(
        'cache_wsdl' => 0,
        'connection_timeout' => 5, // Define the connection timeout as 5 Seconds 
      );
      libxml_disable_entity_loader(false);
      $client = new \SoapClient($url, $options);
      return $client->__soapCall($function_name, $arguments);

    } catch (\SoapFault $e) {
      // Timeout, unable to connect 
      if($e->faultcode == 'HTTP'){
        throw new TimeoutException(' Connection or request timeout ', $e->getCode());
      }
    }

You can connect to the soap service, but there is a problem on the client or server $e- > faultcode will return WSDL, which is used to judge


Related articles: