Preparing to multi-account search

This commit is contained in:
magnolia-fan
2011-04-02 18:57:17 +04:00
parent d411680b1b
commit 41efeface3
13 changed files with 160 additions and 460 deletions
@@ -118,4 +118,51 @@ class RemoteFile {
public static function setHeaders($headers) {
self::$_headers = $headers;
}
/**
* Массововое получение заголовков по списку файлов
*
* @param array $fules Ассоциативный массив id => url
* @return array Ассоциативный массив id => headers
* @author chez
**/
public static function getHeadersMT($files) {
$mh = curl_multi_init();
$headers_all = array();
$handlers = array();
foreach($files as $i => $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $ch);
$handlers[$i] = $ch;
}
$running=null;
do
{
curl_multi_exec($mh, $running);
usleep (250000);
} while ($running > 0);
foreach($handlers as $i => $ch) {
$content = curl_multi_getcontent($handlers[$i]);
curl_multi_remove_handle($mh, $handlers[$i]);
$content = explode("\r\n", $content);
$headers = array();
foreach($content as $header) {
if (substr($header, 0, 5) == 'HTTP/') {
$header = explode(' ', $header);
$headers['http'] = $header[1];
} elseif ($header != '') {
preg_match('/^([a-z0-9\-]*):\s(.*)$/Ui', $header, $m);
$headers[strtolower(str_replace('-', '_', $m[1]))] = $m[2];
}
}
$headers_all[$i] = $headers;
}
curl_multi_close($mh);
return $headers_all;
}
}