|
4 | 4 |
|
5 | 5 | class Curl
|
6 | 6 | {
|
7 |
| - const VERSION = '3.4.3'; |
| 7 | + const VERSION = '3.4.4'; |
8 | 8 | const DEFAULT_TIMEOUT = 30;
|
9 | 9 |
|
10 | 10 | public $curl;
|
@@ -158,45 +158,49 @@ public function delete($url, $data = array())
|
158 | 158 | return $this->exec();
|
159 | 159 | }
|
160 | 160 |
|
161 |
| - public function download($url, $mixed_filename) |
| 161 | + public function downloadComplete($fh) |
162 | 162 | {
|
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) { |
176 | 164 | rewind($fh);
|
177 |
| - $this->call($callback, $fh); |
| 165 | + $this->call($this->download_complete_function, $fh); |
| 166 | + $this->download_complete_function = null; |
178 | 167 | }
|
179 | 168 |
|
180 | 169 | if (is_resource($fh)) {
|
181 | 170 | fclose($fh);
|
182 | 171 | }
|
183 | 172 |
|
184 | 173 | // 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". |
186 | 176 | if (!defined('STDOUT')) {
|
187 |
| - define('STDOUT', null); |
| 177 | + define('STDOUT', fopen('php://stdout', 'w')); |
188 | 178 | }
|
189 | 179 |
|
190 | 180 | // 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". |
194 | 182 | $this->setOpt(CURLOPT_FILE, STDOUT);
|
195 | 183 |
|
196 | 184 | // Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent
|
197 | 185 | // responses as the return value of curl_exec(). Without this,
|
198 | 186 | // curl_exec() will revert to returning boolean values.
|
199 | 187 | $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); |
200 | 204 |
|
201 | 205 | return ! $this->error;
|
202 | 206 | }
|
|
0 commit comments