mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-15349, add download function, which can download multi-file in paralle.
This commit is contained in:
parent
4e63912156
commit
b4362df095
1 changed files with 79 additions and 32 deletions
|
@ -5,6 +5,13 @@
|
||||||
* This is a wrapper class for curl, it is quite easy to use:
|
* This is a wrapper class for curl, it is quite easy to use:
|
||||||
*
|
*
|
||||||
* $c = new curl();
|
* $c = new curl();
|
||||||
|
* // enable cache
|
||||||
|
* $c = new curl(array('cache'=>true));
|
||||||
|
* // enable cookie
|
||||||
|
* $c = new curl(array('cookie'=>true));
|
||||||
|
* // enable proxy
|
||||||
|
* $c = new curl(array('proxy'=>true));
|
||||||
|
*
|
||||||
* // HTTP GET Method
|
* // HTTP GET Method
|
||||||
* $html = $c->get('http://example.com');
|
* $html = $c->get('http://example.com');
|
||||||
* // HTTP POST Method
|
* // HTTP POST Method
|
||||||
|
@ -13,7 +20,7 @@
|
||||||
* $html = $c->put('http://example.com/', array('file'=>'/var/www/test.txt');
|
* $html = $c->put('http://example.com/', array('file'=>'/var/www/test.txt');
|
||||||
*
|
*
|
||||||
* @author Dongsheng Cai <dongsheng@cvs.moodle.org>
|
* @author Dongsheng Cai <dongsheng@cvs.moodle.org>
|
||||||
* @version 0.2 dev
|
* @version 0.4 dev
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -26,7 +33,7 @@ class curl {
|
||||||
private $proxy_type = '';
|
private $proxy_type = '';
|
||||||
private $debug = false;
|
private $debug = false;
|
||||||
private $cookie = false;
|
private $cookie = false;
|
||||||
public $version = '0.2 dev';
|
public $version = '0.4 dev';
|
||||||
public $response = array();
|
public $response = array();
|
||||||
public $header = array();
|
public $header = array();
|
||||||
public $info;
|
public $info;
|
||||||
|
@ -205,24 +212,25 @@ class curl {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formatting HTTP Response Header
|
* Set options for individual curl instance
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
protected function request($url, $options = array()){
|
private function apply_opt($curl, $options) {
|
||||||
if (!preg_match('#^https?://#i', $url)) {
|
|
||||||
$this->error = 'Invalid protocol specified in url';
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Clean up
|
// Clean up
|
||||||
$this->cleanopt();
|
$this->cleanopt();
|
||||||
// create curl instance
|
// set cookie
|
||||||
$curl = curl_init($url);
|
if(!empty($this->cookie) || !empty($options['cookie'])) {
|
||||||
$this->setopt(array('url'=>$url));
|
$this->setopt(array('cookiejar'=>$this->cookie,
|
||||||
|
'cookiefile'=>$this->cookie
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// set proxy
|
||||||
|
if(!empty($this->proxy) || !empty($options['proxy'])) {
|
||||||
|
$this->setopt($this->proxy);
|
||||||
|
}
|
||||||
|
$this->setopt($options);
|
||||||
// reset before set options
|
// reset before set options
|
||||||
curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader'));
|
curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader'));
|
||||||
|
|
||||||
$this->setopt($options);
|
|
||||||
|
|
||||||
// set headers
|
// set headers
|
||||||
if(empty($this->header)){
|
if(empty($this->header)){
|
||||||
$this->setHeader(array(
|
$this->setHeader(array(
|
||||||
|
@ -233,16 +241,11 @@ class curl {
|
||||||
}
|
}
|
||||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
|
||||||
|
|
||||||
// set cookie
|
if($this->debug){
|
||||||
if(!empty($this->cookie)) {
|
echo '<h1>Options</h1>';
|
||||||
$this->setopt(array('cookiejar'=>$this->cookie,
|
var_dump($this->options);
|
||||||
'cookiefile'=>$this->cookie
|
echo '<h1>Header</h1>';
|
||||||
));
|
var_dump($this->header);
|
||||||
}
|
|
||||||
|
|
||||||
// set proxy
|
|
||||||
if(!empty($this->proxy)) {
|
|
||||||
$this->setopt($this->proxy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// set options
|
// set options
|
||||||
|
@ -252,14 +255,58 @@ class curl {
|
||||||
}
|
}
|
||||||
curl_setopt($curl, $name, $val);
|
curl_setopt($curl, $name, $val);
|
||||||
}
|
}
|
||||||
|
return $curl;
|
||||||
if($this->debug){
|
}
|
||||||
echo '<h1>Options</h1>';
|
/*
|
||||||
var_dump($this->options);
|
* Download mulit files in parallel
|
||||||
echo '<h1>Header</h1>';
|
* $c = new curl;
|
||||||
var_dump($this->header);
|
* $c->download(array(
|
||||||
|
* array('url'=>'http://localhost/', 'file'=>fopen('a', 'wb')),
|
||||||
|
* array('url'=>'http://localhost/20/', 'file'=>fopen('b', 'wb'))
|
||||||
|
* ));
|
||||||
|
*/
|
||||||
|
public function download($requests, $options = array()) {
|
||||||
|
$options['returntransfer'] = false;
|
||||||
|
return $this->mulit_request($requests, $options);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Mulit HTTP Requests
|
||||||
|
* This function could run mulit-request in parallel.
|
||||||
|
*/
|
||||||
|
protected function mulit_request($requests, $options = array()) {
|
||||||
|
$count = count($requests);
|
||||||
|
$handles = array();
|
||||||
|
$results = array();
|
||||||
|
$main = curl_multi_init();
|
||||||
|
for($i = 0; $i < $count; $i++) {
|
||||||
|
$url = $requests[$i];
|
||||||
|
foreach($url as $n=>$v){
|
||||||
|
$options[$n] = $url[$n];
|
||||||
|
}
|
||||||
|
$handles[$i] = curl_init($url['url']);
|
||||||
|
$this->apply_opt($handles[$i], $options);
|
||||||
|
curl_multi_add_handle($main, $handles[$i]);
|
||||||
}
|
}
|
||||||
|
$running = 0;
|
||||||
|
do {
|
||||||
|
curl_multi_exec($main, $running);
|
||||||
|
} while($running > 0);
|
||||||
|
for($i = 0; $i < $count; $i++)
|
||||||
|
{
|
||||||
|
$results[] = curl_multi_getcontent($handles[$i]);
|
||||||
|
curl_multi_remove_handle($main, $handles[$i]);
|
||||||
|
}
|
||||||
|
curl_multi_close($main);
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Single HTTP Request
|
||||||
|
*/
|
||||||
|
protected function request($url, $options = array()){
|
||||||
|
// create curl instance
|
||||||
|
$curl = curl_init($url);
|
||||||
|
$options['url'] = $url;
|
||||||
|
$this->apply_opt($curl, $options);
|
||||||
if($this->cache && $ret = $this->cache->get($this->options)) {
|
if($this->cache && $ret = $this->cache->get($this->options)) {
|
||||||
return $ret;
|
return $ret;
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue