Skip to content

Use the curl_reset function to allow resource reusing #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2015
Merged
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
38 changes: 31 additions & 7 deletions src/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class CurlHttpClient implements HttpClient
use MessageFactoryAwareTemplate;
use StreamFactoryAwareTemplate;

/**
* cURL handle opened resource
* @var resource
*/
private $handle;

/**
* cURL handle configuration TODO change description
*
Expand Down Expand Up @@ -73,6 +79,16 @@ public function __construct(
);
}

/**
* Release resources if still active
*/
public function __destruct()
{
if (is_resource($this->handle)) {
curl_close($this->handle);
}
}

/**
* Return available options and there default values
*
Expand Down Expand Up @@ -166,19 +182,27 @@ public function sendRequest(RequestInterface $request)
*/
protected function request($options, &$raw, &$info)
{
$ch = curl_init();
if (is_resource($this->handle)) {
curl_reset($this->handle);
} else {
$this->handle = curl_init();
}

try {
curl_setopt_array($ch, $options);
$raw = curl_exec($ch);
curl_setopt_array($this->handle, $options);
$raw = curl_exec($this->handle);

if (curl_errno($ch) > 0) {
if (curl_errno($this->handle) > 0) {
throw new RuntimeException(
sprintf('Curl error: (%d) %s', curl_errno($ch), curl_error($ch))
sprintf(
'Curl error: (%d) %s',
curl_errno($this->handle),
curl_error($this->handle)
)
);
}
$info = curl_getinfo($ch);
$info = curl_getinfo($this->handle);
} finally {
curl_close($ch);
}
}

Expand Down