Refactor Curl for MultiCurl.

Add Curl::id.
Use CURLOPT_HEADERFUNCTION in place of CURLOPT_WRITEHEADER for response headers.
Add Curl::setUrl().
Add Curl::headerCallback().
Make Curl::exec() public.
Make Curl::call() public.
This commit is contained in:
Zach Borboa
2015-02-09 19:42:09 -08:00
parent 2e8ccceae3
commit 79d099b9ab
2 changed files with 62 additions and 52 deletions
+36 -32
View File
@@ -146,36 +146,40 @@ $curl->get(array(
### Available Methods
```php
__construct()
__destruct()
beforeSend($function)
close()
complete($callback)
delete($url, $data = array())
download($url, $filename)
error($callback)
get($url_mixed, $data = array())
getOpt($option)
head($url, $data = array())
options($url, $data = array())
patch($url, $data = array())
post($url, $data = array())
put($url, $data = array())
setBasicAuthentication($username, $password = '')
setCookie($key, $value)
setCookieFile($cookie_file)
setCookieJar($cookie_jar)
setDefaultJsonDecoder()
setDefaultTimeout()
setDefaultUserAgent()
setHeader($key, $value)
setJsonDecoder($func)
setOpt($option, $value)
setReferer($referer)
setReferrer($referrer)
setTimeout($seconds)
setUserAgent($user_agent)
success($callback)
unsetHeader($key)
verbose($on = true)
Curl::__construct()
Curl::__destruct()
Curl::beforeSend($callback)
Curl::call($function)
Curl::close()
Curl::complete($callback)
Curl::delete($url, $data = array())
Curl::download($url, $filename)
Curl::error($callback)
Curl::exec($ch = null)
Curl::get($url, $data = array())
Curl::getOpt($option)
Curl::head($url, $data = array())
Curl::headerCallback($ch, $header)
Curl::options($url, $data = array())
Curl::patch($url, $data = array())
Curl::post($url, $data = array())
Curl::put($url, $data = array())
Curl::setBasicAuthentication($username, $password = '')
Curl::setCookie($key, $value)
Curl::setCookieFile($cookie_file)
Curl::setCookieJar($cookie_jar)
Curl::setDefaultJsonDecoder()
Curl::setDefaultTimeout()
Curl::setDefaultUserAgent()
Curl::setHeader($key, $value)
Curl::setJsonDecoder($func)
Curl::setOpt($option, $value)
Curl::setReferer($referer)
Curl::setReferrer($referrer)
Curl::setTimeout($seconds)
Curl::setURL($url, $data = array())
Curl::setUserAgent($user_agent)
Curl::success($callback)
Curl::unsetHeader($key)
Curl::verbose($on = true)
```
+26 -20
View File
@@ -21,6 +21,7 @@ class Curl
private $xml_pattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i';
public $curl;
public $id = null;
public $error = false;
public $error_code = 0;
@@ -38,6 +39,7 @@ class Curl
public $url = null;
public $request_headers = null;
public $response_headers = null;
public $raw_response_headers = '';
public $response = null;
public $raw_response = null;
@@ -48,10 +50,12 @@ class Curl
}
$this->curl = curl_init();
$this->id = 1;
$this->setDefaultUserAgent();
$this->setDefaultJsonDecoder();
$this->setDefaultTimeout();
$this->setOpt(CURLINFO_HEADER_OUT, true);
$this->setOpt(CURLOPT_HEADERFUNCTION, array($this, 'headerCallback'));
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
$this->headers = new CaseInsensitiveArray();
}
@@ -59,8 +63,7 @@ class Curl
public function get($url, $data = array())
{
$this->base_url = $url;
$this->url = $this->buildURL($url, $data);
$this->setOpt(CURLOPT_URL, $this->url);
$this->setURL($url, $data);
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
$this->setOpt(CURLOPT_HTTPGET, true);
return $this->exec();
@@ -299,11 +302,23 @@ class Curl
$this->complete_function = $callback;
}
public function setURL($url, $data = array())
{
$this->url = $this->buildURL($url, $data);
$this->setOpt(CURLOPT_URL, $this->url);
}
private function buildURL($url, $data = array())
{
return $url . (empty($data) ? '' : '?' . http_build_query($data));
}
public function headerCallback($ch, $header)
{
$this->raw_response_headers .= $header;
return strlen($header);
}
private function parseHeaders($raw_headers)
{
$raw_headers = preg_split('/\r\n/', $raw_headers, null, PREG_SPLIT_NO_EMPTY);
@@ -424,27 +439,18 @@ class Curl
return $data;
}
protected function exec($_ch = null)
public function exec($ch = null)
{
$this->call($this->before_send_function);
$target = $ch === null ? $this : $ch;
$response_headers_fh = fopen('php://memory', 'wb+');
// Fallback to storing data in a temporary file when storing data in
// memory errors with "Warning: curl_setopt(): cannot represent a stream
// of type MEMORY as a STDIO FILE*".
if (!@$this->setOpt(CURLOPT_WRITEHEADER, $response_headers_fh)) {
$response_headers_fh = fopen('php://temp', 'wb+');
$this->setOpt(CURLOPT_WRITEHEADER, $response_headers_fh);
if (!($ch === null)) {
$this->raw_response = curl_multi_getcontent($ch);
} else {
$this->call($this->before_send_function);
$this->raw_response = curl_exec($this->curl);
$this->curl_error_code = curl_errno($this->curl);
}
$this->raw_response = curl_exec($this->curl);
$this->curl_error_code = curl_errno($this->curl);
rewind($response_headers_fh);
$this->raw_response_headers = stream_get_contents($response_headers_fh);
fclose($response_headers_fh);
$this->curl_error_message = curl_error($this->curl);
$this->curl_error = !($this->curl_error_code === 0);
$this->http_status_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
@@ -475,7 +481,7 @@ class Curl
return $this->response;
}
private function call($function)
public function call($function)
{
if (is_callable($function)) {
call_user_func_array($function, array($this));