From d279e4b598ac04071eaa442fb3e6d8d09bae6290 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 1 Apr 2015 09:58:14 -0700 Subject: [PATCH 1/4] Fix curl file handle not being correctly unset; Clean up --- src/Curl/MultiCurl.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Curl/MultiCurl.php b/src/Curl/MultiCurl.php index ddfdadb..5cc0b2a 100644 --- a/src/Curl/MultiCurl.php +++ b/src/Curl/MultiCurl.php @@ -281,17 +281,24 @@ 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) { + + if (!$ch->error && $ch->download_complete_function) { rewind($fh); $ch->call($ch->download_complete_function, $fh); } + if (is_resource($fh)) { fclose($fh); } - defined('STDOUT') || define('STDOUT', null); + + if (!defined('STDOUT')) { + define('STDOUT', null); + } + $ch->setOpt(CURLOPT_FILE, STDOUT); $ch->setOpt(CURLOPT_RETURNTRANSFER, true); - unset($fh); + + unset($this->curl_fhs[$ch->id]); } break; } From 34d2b95d016a3251dd2fc29bb185322c1477aab2 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 1 Apr 2015 10:38:37 -0700 Subject: [PATCH 2/4] 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; From 502b6aa432de07051d78ae4eb65e0ee5fac84d88 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 1 Apr 2015 10:54:56 -0700 Subject: [PATCH 3/4] Fix "Warning: curl_setopt(): supplied argument is not a valid File-Handle resource" --- src/Curl/Curl.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 7001f82..656c48c 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -171,15 +171,14 @@ class Curl } // Fix "PHP Notice: Use of undefined constant STDOUT" when reading the - // PHP script from stdin. + // PHP script from stdin. Using null causes "Warning: curl_setopt(): + // supplied argument is not a valid File-Handle resource". if (!defined('STDOUT')) { - define('STDOUT', null); + define('STDOUT', fopen('php://stdout', 'w')); } // Reset CURLOPT_FILE with STDOUT to avoid: "curl_exec(): CURLOPT_FILE - // resource has gone away, resetting to default". Using null causes - // "curl_setopt(): supplied argument is not a valid File-Handle - // resource". + // resource has gone away, resetting to default". $this->setOpt(CURLOPT_FILE, STDOUT); // Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent From 4f5fa06d9918687260cb77404a7854ddd0a1bfc4 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 1 Apr 2015 10:56:33 -0700 Subject: [PATCH 4/4] Bump patch version --- composer.json | 2 +- src/Curl/Curl.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index ca8668c..d24e2eb 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "name": "Zach Borboa" } ], - "version": "3.4.3", + "version": "3.4.4", "require": { "php": ">=5.3", "ext-curl": "*" diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 656c48c..06a5b94 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -4,7 +4,7 @@ namespace Curl; class Curl { - const VERSION = '3.4.3'; + const VERSION = '3.4.4'; const DEFAULT_TIMEOUT = 30; public $curl;