Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Httpful/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public function hasTimeout()
return isset($this->timeout);
}

/**
* @return bool does the request have a connection timeout?
*/
public function hasConnectionTimeout()
{
return isset($this->connection_timeout);
}

/**
* @return bool has the internal curl request been initialized?
*/
Expand Down Expand Up @@ -167,6 +175,23 @@ public function timeoutIn($seconds)
return $this->timeout($seconds);
}

/**
* Specify a HTTP connection timeout
* @param float|int $timeout seconds to timeout the HTTP connection
* @return Request
* @throws Exception
*/
public function setConnectionTimeout($connection_timeout)
{
if (!preg_match('/^\d+(\.\d+)?/', $connection_timeout)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like an odd way to do this. I think I would prefer that this actually checked if it was an int/float and not support strings.

throw new \InvalidArgumentException(
"Invalid connection timeout provided: " . var_export($connection_timeout, true)
);
}
$this->connection_timeout = $connection_timeout;
return $this;
}

/**
* If the response is a 301 or 302 redirect, automatically
* send off another request to that location
Expand Down Expand Up @@ -875,6 +900,14 @@ public function _curlPrep()
}
}

if ($this->hasConnectionTimeout()) {
if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connection_timeout * 1000);
} else {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connection_timeout);
}
}

if ($this->follow_redirects) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects);
Expand Down