Skip to content
Merged
Show file tree
Hide file tree
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: 28 additions & 5 deletions src/Curl/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ public function __construct() {
throw new \ErrorException('cURL library is not loaded');
}

$this->curl = curl_init();
$this->setUserAgent(self::USER_AGENT);
$this->setopt(CURLINFO_HEADER_OUT, TRUE);
$this->setopt(CURLOPT_HEADER, TRUE);
$this->setopt(CURLOPT_RETURNTRANSFER, TRUE);
$this->init();
}

public function get($url, $data = array()) {
Expand Down Expand Up @@ -125,6 +121,25 @@ public function close() {
curl_close($this->curl);
}
}

public function reset() {
$this->close();
$this->_cookies = array();
$this->_headers = array();
$this->error = FALSE;
$this->error_code = 0;
$this->error_message = NULL;
$this->curl_error = FALSE;
$this->curl_error_code = 0;
$this->curl_error_message = NULL;
$this->http_error = FALSE;
$this->http_status_code = 0;
$this->http_error_message = NULL;
$this->request_headers = NULL;
$this->response_headers = NULL;
$this->response = NULL;
$this->init();
}

public function _exec() {
$this->response = curl_exec($this->curl);
Expand Down Expand Up @@ -155,4 +170,12 @@ public function _exec() {
public function __destruct() {
$this->close();
}

private function init() {
$this->curl = curl_init();
$this->setUserAgent(self::USER_AGENT);
$this->setopt(CURLINFO_HEADER_OUT, TRUE);
$this->setopt(CURLOPT_HEADER, TRUE);
$this->setopt(CURLOPT_RETURNTRANSFER, TRUE);
}
}
22 changes: 22 additions & 0 deletions tests/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ public function testHeaders() {
'key' => 'HTTP_ACCEPT',
)) === 'application/json');
}

public function testReset()
{
$curl = $this->getMockBuilder(get_class($this->curl))->getMock();
$curl->expects($this->once())->method('reset')->with();
// lets make small request
$curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000);
$curl->get('http://1.2.3.4/');
$curl->reset();
$this->assertFalse($curl->error);
$this->assertSame(0, $curl->error_code);
$this->assertNull($curl->error_message);
$this->assertFalse($curl->curl_error);
$this->assertSame(0, $curl->curl_error_code);
$this->assertNull($curl->curl_error_message);
$this->assertFalse($curl->http_error);
$this->assertSame(0, $curl->http_status_code);
$this->assertNull($curl->http_error_message);
$this->assertNull($curl->request_headers);
$this->assertNull($curl->response_headers);
$this->assertNull($curl->response);
}

function create_png() {
// PNG image data, 1 x 1, 1-bit colormap, non-interlaced
Expand Down