Merge pull request #160 from zachborboa/master

Fix "Warning: curl_setopt(): supplied argument is not a valid File-Handle resource" (#159)
This commit is contained in:
Zach Borboa
2015-04-01 11:12:04 -07:00
4 changed files with 36 additions and 41 deletions
+1
View File
@@ -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())
+1 -1
View File
@@ -9,7 +9,7 @@
"name": "Zach Borboa"
}
],
"version": "3.4.3",
"version": "3.4.4",
"require": {
"php": ">=5.3",
"ext-curl": "*"
+32 -28
View File
@@ -4,7 +4,7 @@ namespace Curl;
class Curl
{
const VERSION = '3.4.3';
const VERSION = '3.4.4';
const DEFAULT_TIMEOUT = 30;
public $curl;
@@ -158,11 +158,40 @@ class Curl
return $this->exec();
}
public function downloadComplete($fh)
{
if (!$this->error && $this->download_complete_function) {
rewind($fh);
$this->call($this->download_complete_function, $fh);
$this->download_complete_function = null;
}
if (is_resource($fh)) {
fclose($fh);
}
// Fix "PHP Notice: Use of undefined constant STDOUT" when reading the
// PHP script from stdin. Using null causes "Warning: curl_setopt():
// supplied argument is not a valid File-Handle resource".
if (!defined('STDOUT')) {
define('STDOUT', fopen('php://stdout', 'w'));
}
// Reset CURLOPT_FILE with STDOUT to avoid: "curl_exec(): CURLOPT_FILE
// resource has gone away, resetting to default".
$this->setOpt(CURLOPT_FILE, STDOUT);
// Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent
// 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)) {
$callback = $mixed_filename;
$this->download_complete_function = $mixed_filename;
$fh = tmpfile();
} else {
$filename = $mixed_filename;
@@ -171,32 +200,7 @@ class Curl
$this->setOpt(CURLOPT_FILE, $fh);
$this->get($url);
if (!$this->error && $callback) {
rewind($fh);
$this->call($callback, $fh);
}
if (is_resource($fh)) {
fclose($fh);
}
// Fix "PHP Notice: Use of undefined constant STDOUT" when reading the
// PHP script from stdin.
if (!defined('STDOUT')) {
define('STDOUT', null);
}
// 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".
$this->setOpt(CURLOPT_FILE, STDOUT);
// Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent
// responses as the return value of curl_exec(). Without this,
// curl_exec() will revert to returning boolean values.
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
$this->downloadComplete($fh);
return ! $this->error;
}
+2 -12
View File
@@ -280,18 +280,8 @@ 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) {
rewind($fh);
$ch->call($ch->download_complete_function, $fh);
}
if (is_resource($fh)) {
fclose($fh);
}
defined('STDOUT') || define('STDOUT', null);
$ch->setOpt(CURLOPT_FILE, STDOUT);
$ch->setOpt(CURLOPT_RETURNTRANSFER, true);
unset($fh);
$ch->downloadComplete($this->curl_fhs[$ch->id]);
unset($this->curl_fhs[$ch->id]);
}
break;
}