mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-09-11 10:56:29 +00:00
Add ability to specify a callback for MultiCurl::download()
This commit is contained in:
+2
-1
@@ -31,6 +31,7 @@ class Curl
|
||||
public $raw_response = null;
|
||||
|
||||
public $before_send_function = null;
|
||||
public $download_complete_function = null;
|
||||
private $success_function = null;
|
||||
private $error_function = null;
|
||||
private $complete_function = null;
|
||||
@@ -169,7 +170,7 @@ class Curl
|
||||
|
||||
if (!$this->error && $callback) {
|
||||
rewind($fh);
|
||||
$callback($this, $fh);
|
||||
$this->call($callback, $fh);
|
||||
}
|
||||
|
||||
if (is_resource($fh)) {
|
||||
|
||||
+21
-4
@@ -35,11 +35,21 @@ class MultiCurl
|
||||
return $curl;
|
||||
}
|
||||
|
||||
public function addDownload($url, $filename)
|
||||
public function addDownload($url, $mixed_filename)
|
||||
{
|
||||
$curl = new Curl();
|
||||
$curl->setURL($url);
|
||||
$fh = fopen($filename, 'wb');
|
||||
|
||||
$callback = false;
|
||||
if (is_callable($mixed_filename)) {
|
||||
$callback = $mixed_filename;
|
||||
$curl->download_complete_function = $callback;
|
||||
$fh = tmpfile();
|
||||
} else {
|
||||
$filename = $mixed_filename;
|
||||
$fh = fopen($filename, 'wb');
|
||||
}
|
||||
|
||||
$curl->setOpt(CURLOPT_FILE, $fh);
|
||||
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
|
||||
$curl->setOpt(CURLOPT_HTTPGET, true);
|
||||
@@ -234,11 +244,18 @@ class MultiCurl
|
||||
|
||||
// Close open file handles and reset the curl instance.
|
||||
if (isset($this->curl_fhs[$ch->id])) {
|
||||
fclose($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($this->curl_fhs[$ch->id]);
|
||||
unset($fh);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -491,7 +491,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
|
||||
PHPUnit_Framework_Assert::assertEquals('stream', get_resource_type($fh));
|
||||
PHPUnit_Framework_Assert::assertGreaterThan(0, strlen(stream_get_contents($fh)));
|
||||
PHPUnit_Framework_Assert::assertEquals(0, strlen(stream_get_contents($fh)));
|
||||
fclose($fh);
|
||||
PHPUnit_Framework_Assert::assertTrue(fclose($fh));
|
||||
});
|
||||
$this->assertTrue($callback_called);
|
||||
|
||||
|
||||
@@ -1670,4 +1670,60 @@ class MultiCurlTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('foo', $get_1->response);
|
||||
$this->assertEquals('bar', $get_2->response);
|
||||
}
|
||||
|
||||
public function testDownloadCallback()
|
||||
{
|
||||
// Upload a file.
|
||||
$upload_file_path = Helper\get_png();
|
||||
$upload_test = new Test();
|
||||
$upload_test->server('upload_response', 'POST', array(
|
||||
'image' => '@' . $upload_file_path,
|
||||
));
|
||||
$uploaded_file_path = $upload_test->curl->response->file_path;
|
||||
|
||||
// Download the file.
|
||||
$download_callback_called = false;
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'download_response');
|
||||
$multi_curl->addDownload(Test::TEST_URL . '?' . http_build_query(array(
|
||||
'file_path' => $uploaded_file_path,
|
||||
)), function($instance, $fh) use (&$download_callback_called) {
|
||||
PHPUnit_Framework_Assert::assertFalse($download_callback_called);
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertTrue(is_resource($fh));
|
||||
PHPUnit_Framework_Assert::assertEquals('stream', get_resource_type($fh));
|
||||
PHPUnit_Framework_Assert::assertGreaterThan(0, strlen(stream_get_contents($fh)));
|
||||
PHPUnit_Framework_Assert::assertEquals(0, strlen(stream_get_contents($fh)));
|
||||
PHPUnit_Framework_Assert::assertTrue(fclose($fh));
|
||||
$download_callback_called = true;
|
||||
});
|
||||
$multi_curl->start();
|
||||
$this->assertTrue($download_callback_called);
|
||||
|
||||
// Remove server file.
|
||||
$this->assertEquals('true', $upload_test->server('upload_cleanup', 'POST', array(
|
||||
'file_path' => $uploaded_file_path,
|
||||
)));
|
||||
|
||||
unlink($upload_file_path);
|
||||
$this->assertFalse(file_exists($upload_file_path));
|
||||
$this->assertFalse(file_exists($uploaded_file_path));
|
||||
}
|
||||
|
||||
public function testDownloadCallbackError()
|
||||
{
|
||||
$download_before_send_called = false;
|
||||
$download_callback_called = false;
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->beforeSend(function ($instance) use (&$download_before_send_called) {
|
||||
PHPUnit_Framework_Assert::assertFalse($download_before_send_called);
|
||||
$download_before_send_called = true;
|
||||
});
|
||||
$multi_curl->addDownload(Test::ERROR_URL, function($instance, $fh) use (&$download_callback_called) {
|
||||
$download_callback_called = true;
|
||||
});
|
||||
$multi_curl->start();
|
||||
$this->assertTrue($download_before_send_called);
|
||||
$this->assertFalse($download_callback_called);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user