PHP: moving over the addition of a setTimeout function for the APIClient

This commit is contained in:
James Ebentier 2015-02-06 11:16:16 -08:00
parent 73a702f91e
commit 8b4180ca4a

View File

@ -37,6 +37,16 @@ class APIClient {
$this->apiServer = $apiServer; $this->apiServer = $apiServer;
} }
/**
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
*/
public function setTimeout($seconds) {
if (!is_numeric($seconds)) {
throw new Exception('Timeout variable must be numeric.');
}
$this->curl_timout = $seconds;
}
/** /**
* @param string $resourcePath path to method endpoint * @param string $resourcePath path to method endpoint
@ -72,7 +82,9 @@ class APIClient {
$url = $this->apiServer . $resourcePath; $url = $this->apiServer . $resourcePath;
$curl = curl_init(); $curl = curl_init();
curl_setopt($curl, CURLOPT_TIMEOUT, 5); if ($this->curl_timout) {
curl_setopt($curl, CURLOPT_TIMEOUT, $this->curl_timout);
}
// return the result on success, rather than just TRUE // return the result on success, rather than just TRUE
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);