Skip to content

Commit 343252c

Browse files
committed
Merge pull request php-curl-class#160 from zachborboa/master
Fix "Warning: curl_setopt(): supplied argument is not a valid File-Handle resource" (php-curl-class#159)
2 parents 512b514 + 4f5fa06 commit 343252c

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Curl::close()
156156
Curl::complete($callback)
157157
Curl::delete($url, $data = array())
158158
Curl::download($url, $mixed_filename)
159+
Curl::downloadComplete($fh)
159160
Curl::error($callback)
160161
Curl::exec($ch = null)
161162
Curl::get($url, $data = array())

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"name": "Zach Borboa"
1010
}
1111
],
12-
"version": "3.4.3",
12+
"version": "3.4.4",
1313
"require": {
1414
"php": ">=5.3",
1515
"ext-curl": "*"

src/Curl/Curl.php

+25-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Curl
66
{
7-
const VERSION = '3.4.3';
7+
const VERSION = '3.4.4';
88
const DEFAULT_TIMEOUT = 30;
99

1010
public $curl;
@@ -158,45 +158,49 @@ public function delete($url, $data = array())
158158
return $this->exec();
159159
}
160160

161-
public function download($url, $mixed_filename)
161+
public function downloadComplete($fh)
162162
{
163-
$callback = false;
164-
if (is_callable($mixed_filename)) {
165-
$callback = $mixed_filename;
166-
$fh = tmpfile();
167-
} else {
168-
$filename = $mixed_filename;
169-
$fh = fopen($filename, 'wb');
170-
}
171-
172-
$this->setOpt(CURLOPT_FILE, $fh);
173-
$this->get($url);
174-
175-
if (!$this->error && $callback) {
163+
if (!$this->error && $this->download_complete_function) {
176164
rewind($fh);
177-
$this->call($callback, $fh);
165+
$this->call($this->download_complete_function, $fh);
166+
$this->download_complete_function = null;
178167
}
179168

180169
if (is_resource($fh)) {
181170
fclose($fh);
182171
}
183172

184173
// Fix "PHP Notice: Use of undefined constant STDOUT" when reading the
185-
// PHP script from stdin.
174+
// PHP script from stdin. Using null causes "Warning: curl_setopt():
175+
// supplied argument is not a valid File-Handle resource".
186176
if (!defined('STDOUT')) {
187-
define('STDOUT', null);
177+
define('STDOUT', fopen('php://stdout', 'w'));
188178
}
189179

190180
// Reset CURLOPT_FILE with STDOUT to avoid: "curl_exec(): CURLOPT_FILE
191-
// resource has gone away, resetting to default". Using null causes
192-
// "curl_setopt(): supplied argument is not a valid File-Handle
193-
// resource".
181+
// resource has gone away, resetting to default".
194182
$this->setOpt(CURLOPT_FILE, STDOUT);
195183

196184
// Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent
197185
// responses as the return value of curl_exec(). Without this,
198186
// curl_exec() will revert to returning boolean values.
199187
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
188+
}
189+
190+
public function download($url, $mixed_filename)
191+
{
192+
$callback = false;
193+
if (is_callable($mixed_filename)) {
194+
$this->download_complete_function = $mixed_filename;
195+
$fh = tmpfile();
196+
} else {
197+
$filename = $mixed_filename;
198+
$fh = fopen($filename, 'wb');
199+
}
200+
201+
$this->setOpt(CURLOPT_FILE, $fh);
202+
$this->get($url);
203+
$this->downloadComplete($fh);
200204

201205
return ! $this->error;
202206
}

src/Curl/MultiCurl.php

+2-12
Original file line numberDiff line numberDiff line change
@@ -280,18 +280,8 @@ public function start()
280280

281281
// Close open file handles and reset the curl instance.
282282
if (isset($this->curl_fhs[$ch->id])) {
283-
$fh = $this->curl_fhs[$ch->id];
284-
if (!$ch->error) {
285-
rewind($fh);
286-
$ch->call($ch->download_complete_function, $fh);
287-
}
288-
if (is_resource($fh)) {
289-
fclose($fh);
290-
}
291-
defined('STDOUT') || define('STDOUT', null);
292-
$ch->setOpt(CURLOPT_FILE, STDOUT);
293-
$ch->setOpt(CURLOPT_RETURNTRANSFER, true);
294-
unset($fh);
283+
$ch->downloadComplete($this->curl_fhs[$ch->id]);
284+
unset($this->curl_fhs[$ch->id]);
295285
}
296286
break;
297287
}

0 commit comments

Comments
 (0)