Skip to content

Instantly share code, notes, and snippets.

@fguillot
Created July 18, 2013 23:18
Show Gist options
  • Save fguillot/6033905 to your computer and use it in GitHub Desktop.
Save fguillot/6033905 to your computer and use it in GitHub Desktop.
Re-use curl instance
<?php
namespace PicoFeed\Clients;
use \PicoFeed\Logging;
class Curl extends \PicoFeed\Client
{
private $body = '';
private $body_length = 0;
private $headers = array();
private $headers_counter = 0;
private static $curl = null;
private function getCurlInstance()
{
if (self::$curl === null) {
self::$curl = curl_init();
}
return self::$curl;
}
public function readBody($ch, $buffer)
{
$length = strlen($buffer);
$this->body_length += $length;
if ($this->body_length > $this->max_body_size) return -1;
$this->body .= $buffer;
return $length;
}
public function readHeaders($ch, $buffer)
{
$length = strlen($buffer);
if ($buffer === "\r\n") {
$this->headers_counter++;
}
else {
if (! isset($this->headers[$this->headers_counter])) {
$this->headers[$this->headers_counter] = '';
}
$this->headers[$this->headers_counter] .= $buffer;
}
return $length;
}
public function doRequest()
{
$request_headers = array('Connection: close');
if ($this->etag) $request_headers[] = 'If-None-Match: '.$this->etag;
if ($this->last_modified) $request_headers[] = 'If-Modified-Since: '.$this->last_modified;
$t = microtime(true);
$ch = $this->getCurlInstance();
//$ch = curl_init();
var_dump(microtime(true) - $t);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // For auto-signed certificates...
curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, 'readBody'));
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'readHeaders'));
curl_exec($ch);
Logging::log('cURL total time: '.curl_getinfo($ch, CURLINFO_TOTAL_TIME));
Logging::log('cURL dns lookup time: '.curl_getinfo($ch, CURLINFO_NAMELOOKUP_TIME));
Logging::log('cURL connect time: '.curl_getinfo($ch, CURLINFO_CONNECT_TIME));
Logging::log('cURL speed download: '.curl_getinfo($ch, CURLINFO_SPEED_DOWNLOAD));
if (curl_errno($ch)) {
Logging::log('cURL error: '.curl_error($ch));
//curl_close($ch);
return false;
}
//curl_close($ch);
list($status, $headers) = $this->parseHeaders(explode("\r\n", $this->headers[$this->headers_counter - 1]));
return array(
'status' => $status,
'body' => $this->body,
'headers' => $headers
);
}
}
# No-reuse of curl instance
$ phpunit tests/CurlTest.php
PHPUnit 3.6.12 by Sebastian Bergmann.
.float(3.1948089599609E-5)
.float(4.5061111450195E-5)
.float(1.0967254638672E-5)
.float(1.0967254638672E-5)
.float(3.0040740966797E-5)
Time: 47 seconds, Memory: 6.50Mb
OK (5 tests, 11 assertions)
# Re-use of curl instance
$ phpunit tests/CurlTest.php
PHPUnit 3.6.12 by Sebastian Bergmann.
.float(4.9114227294922E-5)
.float(5.9604644775391E-6)
.float(7.1525573730469E-6)
.float(7.1525573730469E-6)
.float(5.0067901611328E-6)
Time: 49 seconds, Memory: 6.50Mb
OK (5 tests, 11 assertions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment