PHP SoapClient Timeout
Have a look at
- Timing Out PHP Soap Calls by Robert; 21 Oct 2009
if you are comfortable and your environment allows you to extend classes.
It basically extends the SoapClient
class, replaces the HTTP transport with curl which can handle the timeouts:
class SoapClientTimeout extends SoapClient
{
private $timeout;
public function __setTimeout($timeout)
{
if (!is_int($timeout) && !is_null($timeout))
{
throw new Exception("Invalid timeout value");
}
$this->timeout = $timeout;
}
public function __doRequest($request, $location, $action, $version, $one_way = FALSE)
{
if (!$this->timeout)
{
// Call via parent because we require no timeout
$response = parent::__doRequest($request, $location, $action, $version, $one_way);
}
else
{
// Call via Curl and use the timeout
$curl = curl_init($location);
curl_setopt($curl, CURLOPT_VERBOSE, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
$response = curl_exec($curl);
if (curl_errno($curl))
{
throw new Exception(curl_error($curl));
}
curl_close($curl);
}
// Return?
if (!$one_way)
{
return ($response);
}
}
}
PHP SoapClient timeout error handler
You can put it in a try/catch
try {
$time_start = microtime(true);
$this->client = new SoapClient($clienturl,array('trace' => 1,
'exceptions'=> 1,
'connection_timeout'=> 15
));
} catch (Exception $e) {
$time_request = (microtime(true)-$time_start);
if(ini_get('default_socket_timeout') < $time_request) {
//Timeout error!
} else {
//other error
//$error = $e->getMessage();
}
}
SOAP client timeouts during function execution
According to your update and the XML configuration file provided with the example this Webservice appears to be using WS-Addressing
so the regular SOAPClient will not work and requires it to be extended in order to support WS-Addressing
.
This is the line that gives it away
<textMessageEncoding messageVersion="Soap11WSAddressing10" />
I have yet to use WS-Addressing
but I was recently analysing an API for project we will be working on in the future that requires it.
Please take note of this project or this other project which may be useful (it's easy to search for more PHP WS-Addressing
).
Again, I have only done research and do not have any hands-on experience to help you with actual code :)
[EDIT: Obsolete answer after the update]
First of all, you might be being mislead by the use of the variable proxy
in the example code. They are probably referring to HTTP Basic Authentication and not a proxy.
Try replacing proxy_login
and proxy_password
with login
and password
.
However, having said that, if you are getting the WSDL it means that at least it's connecting and obtaining the information about the service (which is good).
In normal situations you do not need to specify location
in SoapClient as it should be defined by the WSDL file. By setting the location
parameter you are overriding what is set in the WSDL file and you may be pointing it to a location that does not exist.
Try ommiting the location
and soap_version
from the SoapClient constructor and let the library handle those parameter automatically:
$connection = new SoapClient($wsdl_url, array('connection_timeout'=> 600,
'proxy_login' => "my_login", 'proxy_password' => "my_password"));
On the other hand, perhaps you are dealing with an extremely slow Web Service. There are many parameters in PHP that may be affecting the time it takes to timeout and most likely they are well below your connection_timeout
parameter:
- default_socket_timeout
- max_execution_time
SoapClient / Zend_Soap_Client with timeout
I doubt that dynamically setting the timeout
option is possible.
However, can you try this method?
$this->_soap->setSoapClient(
new SoapClient(
$this->_wsdl,
array(
'soap_version' => SOAP_1_1,
'connection_timeout' => intval($timeout)
)
)
);
Hope it helps.
Thanks
Related Topics
Asynchronous Processing or Message Queues in PHP (Cakephp)
What Rss Parser Should I Use in PHP
How to Display All the Images Stored Inside a Database
Empty Values Passed to Zend Framework 2 Validators
PHP - Count Frequency of Array Values
Pros and Cons of Interface Constants
PHP Loop: Add a Div Around Every Three Items Syntax
Php, Failed Connecting to Database
PHP - Getsqlvaluestring Function
Is There Open-Source Code for Making 'Link Preview' Text and Icons, Like in Facebook
PHP Allocate Color Without Image Resource
5.4 Dereferencing to Valid 5.3 Array Call
PHP Call_User_Func VS. Just Calling Function
How to Pass Param from Controller to Layout in Yii2