mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 20:20:53 +00:00
Move download complete to unified method
This commit is contained in:
@@ -156,6 +156,7 @@ Curl::close()
|
||||
Curl::complete($callback)
|
||||
Curl::delete($url, $data = array())
|
||||
Curl::download($url, $mixed_filename)
|
||||
Curl::downloadComplete($fh)
|
||||
Curl::error($callback)
|
||||
Curl::exec($ch = null)
|
||||
Curl::get($url, $data = array())
|
||||
|
||||
+20
-15
@@ -158,23 +158,12 @@ class Curl
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
public function download($url, $mixed_filename)
|
||||
public function downloadComplete($fh)
|
||||
{
|
||||
$callback = false;
|
||||
if (is_callable($mixed_filename)) {
|
||||
$callback = $mixed_filename;
|
||||
$fh = tmpfile();
|
||||
} else {
|
||||
$filename = $mixed_filename;
|
||||
$fh = fopen($filename, 'wb');
|
||||
}
|
||||
|
||||
$this->setOpt(CURLOPT_FILE, $fh);
|
||||
$this->get($url);
|
||||
|
||||
if (!$this->error && $callback) {
|
||||
if (!$this->error && $this->download_complete_function) {
|
||||
rewind($fh);
|
||||
$this->call($callback, $fh);
|
||||
$this->call($this->download_complete_function, $fh);
|
||||
$this->download_complete_function = null;
|
||||
}
|
||||
|
||||
if (is_resource($fh)) {
|
||||
@@ -197,6 +186,22 @@ class Curl
|
||||
// responses as the return value of curl_exec(). Without this,
|
||||
// curl_exec() will revert to returning boolean values.
|
||||
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
|
||||
}
|
||||
|
||||
public function download($url, $mixed_filename)
|
||||
{
|
||||
$callback = false;
|
||||
if (is_callable($mixed_filename)) {
|
||||
$this->download_complete_function = $mixed_filename;
|
||||
$fh = tmpfile();
|
||||
} else {
|
||||
$filename = $mixed_filename;
|
||||
$fh = fopen($filename, 'wb');
|
||||
}
|
||||
|
||||
$this->setOpt(CURLOPT_FILE, $fh);
|
||||
$this->get($url);
|
||||
$this->downloadComplete($fh);
|
||||
|
||||
return ! $this->error;
|
||||
}
|
||||
|
||||
+1
-18
@@ -280,24 +280,7 @@ class MultiCurl
|
||||
|
||||
// Close open file handles and reset the curl instance.
|
||||
if (isset($this->curl_fhs[$ch->id])) {
|
||||
$fh = $this->curl_fhs[$ch->id];
|
||||
|
||||
if (!$ch->error && $ch->download_complete_function) {
|
||||
rewind($fh);
|
||||
$ch->call($ch->download_complete_function, $fh);
|
||||
}
|
||||
|
||||
if (is_resource($fh)) {
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
if (!defined('STDOUT')) {
|
||||
define('STDOUT', null);
|
||||
}
|
||||
|
||||
$ch->setOpt(CURLOPT_FILE, STDOUT);
|
||||
$ch->setOpt(CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$ch->downloadComplete($this->curl_fhs[$ch->id]);
|
||||
unset($this->curl_fhs[$ch->id]);
|
||||
}
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user