From 34d2b95d016a3251dd2fc29bb185322c1477aab2 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 1 Apr 2015 10:38:37 -0700 Subject: [PATCH] Move download complete to unified method --- README.md | 1 + src/Curl/Curl.php | 35 ++++++++++++++++++++--------------- src/Curl/MultiCurl.php | 19 +------------------ 3 files changed, 22 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 27fde27..963aa14 100644 --- a/README.md +++ b/README.md @@ -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()) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index da218ed..7001f82 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -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; } diff --git a/src/Curl/MultiCurl.php b/src/Curl/MultiCurl.php index 5cc0b2a..892ea0f 100644 --- a/src/Curl/MultiCurl.php +++ b/src/Curl/MultiCurl.php @@ -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;