Created
February 12, 2014 01:09
-
-
Save schleumer/8947953 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Octopus { | |
public $results = array(); | |
public $urls = array(); | |
public $chs = array(); | |
public $mh = null; | |
public function __construct() { | |
$this->mh = curl_multi_init(); | |
} | |
public function addUrl($url, array $headers = array()) { | |
$id = uniqid(); | |
$this->urls[] = $url; | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 60); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36'); | |
foreach ($headers as $opt => $val) { | |
curl_setopt($ch, $opt, $val); | |
} | |
$this->chs[$id] = $ch; | |
return $id; | |
} | |
public function run($callback = null) { | |
foreach ($this->chs as $ch) { | |
curl_multi_add_handle($this->mh, $ch); | |
} | |
$mh = $this->mh; | |
$active = null; | |
do { | |
$mrc = curl_multi_exec($mh, $active); | |
} while ($mrc == CURLM_CALL_MULTI_PERFORM); | |
if (version_compare(PHP_VERSION, '5.4.0', '<')) { | |
while ($active && $mrc == CURLM_OK) { | |
if ($callback) { | |
call_user_func($callback); | |
} | |
if (curl_multi_select($mh) != -1) { | |
do { | |
$mrc = curl_multi_exec($mh, $active); | |
} while ($mrc == CURLM_CALL_MULTI_PERFORM); | |
} | |
} | |
} else { | |
do { | |
if ($callback) { | |
call_user_func($callback); | |
} | |
curl_multi_exec($mh, $active); | |
} while ($active > 0); | |
} | |
foreach ($this->chs as $id => &$ch) { | |
$content = curl_multi_getcontent($ch); | |
$this->results[$id] = array( | |
'content' => $content, | |
'id' => $id, | |
'md5' => md5($content), | |
'info' => curl_getinfo($ch) | |
); | |
curl_multi_remove_handle($mh, $ch); | |
} | |
curl_multi_close($this->mh); | |
} | |
public function getResult($id) { | |
return $this->results[$id]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment