Skip to content

Commit 31789de

Browse files
committed
Merge pull request #1 from shulard/feature/curl-reset
Use the curl_reset function to allow resource reusing
2 parents e7bd3b5 + b5cedf6 commit 31789de

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/CurlHttpClient.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class CurlHttpClient implements HttpClient
3333
use MessageFactoryAwareTemplate;
3434
use StreamFactoryAwareTemplate;
3535

36+
/**
37+
* cURL handle opened resource
38+
* @var resource
39+
*/
40+
private $handle;
41+
3642
/**
3743
* cURL handle configuration TODO change description
3844
*
@@ -73,6 +79,16 @@ public function __construct(
7379
);
7480
}
7581

82+
/**
83+
* Release resources if still active
84+
*/
85+
public function __destruct()
86+
{
87+
if (is_resource($this->handle)) {
88+
curl_close($this->handle);
89+
}
90+
}
91+
7692
/**
7793
* Return available options and there default values
7894
*
@@ -166,19 +182,27 @@ public function sendRequest(RequestInterface $request)
166182
*/
167183
protected function request($options, &$raw, &$info)
168184
{
169-
$ch = curl_init();
185+
if (is_resource($this->handle)) {
186+
curl_reset($this->handle);
187+
} else {
188+
$this->handle = curl_init();
189+
}
190+
170191
try {
171-
curl_setopt_array($ch, $options);
172-
$raw = curl_exec($ch);
192+
curl_setopt_array($this->handle, $options);
193+
$raw = curl_exec($this->handle);
173194

174-
if (curl_errno($ch) > 0) {
195+
if (curl_errno($this->handle) > 0) {
175196
throw new RuntimeException(
176-
sprintf('Curl error: (%d) %s', curl_errno($ch), curl_error($ch))
197+
sprintf(
198+
'Curl error: (%d) %s',
199+
curl_errno($this->handle),
200+
curl_error($this->handle)
201+
)
177202
);
178203
}
179-
$info = curl_getinfo($ch);
204+
$info = curl_getinfo($this->handle);
180205
} finally {
181-
curl_close($ch);
182206
}
183207
}
184208

0 commit comments

Comments
 (0)