diff --git a/src/CurlHttpClient.php b/src/CurlHttpClient.php index 6bced45..154abe6 100644 --- a/src/CurlHttpClient.php +++ b/src/CurlHttpClient.php @@ -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 * @@ -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 * @@ -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); } }