mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-09-12 11:26:29 +00:00
Implement MultiCurl::addOptions(); Fix issue caused by messages left in the queue (curl_multi_info_read)
This commit is contained in:
@@ -271,26 +271,31 @@ class Curl
|
||||
|
||||
public function beforeSend($callback)
|
||||
{
|
||||
echo 'setting curl beforeSend' . "\n";
|
||||
$this->before_send_function = $callback;
|
||||
}
|
||||
|
||||
public function success($callback)
|
||||
{
|
||||
echo 'setting curl success' . "\n";
|
||||
$this->success_function = $callback;
|
||||
}
|
||||
|
||||
public function error($callback)
|
||||
{
|
||||
echo 'setting curl error' . "\n";
|
||||
$this->error_function = $callback;
|
||||
}
|
||||
|
||||
public function complete($callback)
|
||||
{
|
||||
echo 'setting curl complete' . "\n";
|
||||
$this->complete_function = $callback;
|
||||
}
|
||||
|
||||
public function setURL($url, $data = array())
|
||||
{
|
||||
$this->base_url = $url;
|
||||
$this->url = $this->buildURL($url, $data);
|
||||
$this->setOpt(CURLOPT_URL, $this->url);
|
||||
}
|
||||
|
||||
+27
-13
@@ -53,6 +53,12 @@ class MultiCurl
|
||||
|
||||
public function addOptions($url, $data = array())
|
||||
{
|
||||
$curl = new Curl();
|
||||
$curl->setURL($url, $data);
|
||||
$curl->unsetHeader('Content-Length');
|
||||
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS');
|
||||
$this->addHandle($curl);
|
||||
return $curl;
|
||||
}
|
||||
|
||||
public function addPatch($url, $data = array())
|
||||
@@ -95,28 +101,36 @@ class MultiCurl
|
||||
{
|
||||
echo 'running start' . "\n";
|
||||
foreach ($this->curls as $ch) {
|
||||
echo 'about to call before send' . "\n";
|
||||
$ch->call($ch->before_send_function);
|
||||
}
|
||||
echo 'called all before sends' . "\n";
|
||||
|
||||
$curl_handles = $this->curls;
|
||||
do {
|
||||
echo str_repeat('-', 80) . "\n";
|
||||
curl_multi_select($this->curl_multi);
|
||||
curl_multi_exec($this->curl_multi, $active);
|
||||
$info_array = curl_multi_info_read($this->curl_multi);
|
||||
if (!($info_array === false)) {
|
||||
foreach ($curl_handles as $key => $ch) {
|
||||
if ($ch->curl === $info_array['handle']) {
|
||||
echo $ch->id . ' completed' . "\n";
|
||||
$ch->curl_error_code = $info_array['result'];
|
||||
$ch->exec($ch->curl);
|
||||
curl_multi_remove_handle($this->curl_multi, $ch->curl);
|
||||
unset($curl_handles[$key]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
echo 'active: ' . $active . "\n";
|
||||
|
||||
while (!($info_array = curl_multi_info_read($this->curl_multi)) === false) {
|
||||
if ($info_array['msg'] === CURLMSG_DONE) {
|
||||
foreach ($curl_handles as $key => $ch) {
|
||||
if ($ch->curl === $info_array['handle']) {
|
||||
echo $ch->id . ' completed' . "\n";
|
||||
$ch->curl_error_code = $info_array['result'];
|
||||
$ch->exec($ch->curl);
|
||||
curl_multi_remove_handle($this->curl_multi, $ch->curl);
|
||||
unset($curl_handles[$key]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} while ($active > 0);
|
||||
|
||||
echo 'active: ' . $active . "\n";
|
||||
echo 'start completed' . "\n";
|
||||
}
|
||||
|
||||
public function close()
|
||||
@@ -135,7 +149,7 @@ class MultiCurl
|
||||
|
||||
private function addHandle($curl)
|
||||
{
|
||||
echo 'adding handle' . "\n";
|
||||
echo 'adding handle (' . $curl->getOpt(CURLOPT_CUSTOMREQUEST) . ')' . "\n";
|
||||
$curlm_error_code = curl_multi_add_handle($this->curl_multi, $curl->curl);
|
||||
if (!($curlm_error_code === CURLM_OK)) {
|
||||
throw new \ErrorException('cURL multi add handle error: ' . curl_multi_strerror($curlm_error_code));
|
||||
|
||||
@@ -23,11 +23,17 @@ class MultiCurlTest extends PHPUnit_Framework_TestCase
|
||||
$head_error_called = false;
|
||||
$head_complete_called = false;
|
||||
|
||||
$options_before_send_called = false;
|
||||
$options_success_called = false;
|
||||
$options_error_called = false;
|
||||
$options_complete_called = false;
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->beforeSend(function ($instance) use (
|
||||
&$delete_before_send_called, &$delete_success_called, &$delete_error_called, &$delete_complete_called,
|
||||
&$get_before_send_called, &$get_success_called, &$get_error_called, &$get_complete_called,
|
||||
&$head_before_send_called, &$head_success_called, &$head_error_called, &$head_complete_called) {
|
||||
&$head_before_send_called, &$head_success_called, &$head_error_called, &$head_complete_called,
|
||||
&$options_before_send_called, &$options_success_called, &$options_error_called, &$options_complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
$request_method = $instance->getOpt(CURLOPT_CUSTOMREQUEST);
|
||||
echo 'beforeSend request method: ' . $request_method . "\n";
|
||||
@@ -52,11 +58,19 @@ class MultiCurlTest extends PHPUnit_Framework_TestCase
|
||||
PHPUnit_Framework_Assert::assertFalse($head_complete_called);
|
||||
$head_before_send_called = true;
|
||||
}
|
||||
if ($request_method === 'OPTIONS') {
|
||||
PHPUnit_Framework_Assert::assertFalse($options_before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($options_success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($options_error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($options_complete_called);
|
||||
$options_before_send_called = true;
|
||||
}
|
||||
});
|
||||
$multi_curl->success(function ($instance) use (
|
||||
&$delete_before_send_called, &$delete_success_called, &$delete_error_called, &$delete_complete_called,
|
||||
&$get_before_send_called, &$get_success_called, &$get_error_called, &$get_complete_called,
|
||||
&$head_before_send_called, &$head_success_called, &$head_error_called, &$head_complete_called) {
|
||||
&$head_before_send_called, &$head_success_called, &$head_error_called, &$head_complete_called,
|
||||
&$options_before_send_called, &$options_success_called, &$options_error_called, &$options_complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
$request_method = $instance->getOpt(CURLOPT_CUSTOMREQUEST);
|
||||
echo 'success request method: ' . $request_method . "\n";
|
||||
@@ -81,19 +95,29 @@ class MultiCurlTest extends PHPUnit_Framework_TestCase
|
||||
PHPUnit_Framework_Assert::assertFalse($head_complete_called);
|
||||
$head_success_called = true;
|
||||
}
|
||||
if ($request_method === 'OPTIONS') {
|
||||
PHPUnit_Framework_Assert::assertTrue($options_before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($options_success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($options_error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($options_complete_called);
|
||||
$options_success_called = true;
|
||||
}
|
||||
});
|
||||
$multi_curl->error(function ($instance) use (
|
||||
&$delete_error_called,
|
||||
&$get_error_called,
|
||||
&$head_error_called) {
|
||||
&$head_error_called,
|
||||
&$options_error_called) {
|
||||
$delete_error_called = true;
|
||||
$get_error_called = true;
|
||||
$head_error_called = true;
|
||||
$options_error_called = true;
|
||||
});
|
||||
$multi_curl->complete(function ($instance) use (
|
||||
&$delete_before_send_called, &$delete_success_called, &$delete_error_called, &$delete_complete_called,
|
||||
&$get_before_send_called, &$get_success_called, &$get_error_called, &$get_complete_called,
|
||||
&$head_before_send_called, &$head_success_called, &$head_error_called, &$head_complete_called) {
|
||||
&$head_before_send_called, &$head_success_called, &$head_error_called, &$head_complete_called,
|
||||
&$options_before_send_called, &$options_success_called, &$options_error_called, &$options_complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
$request_method = $instance->getOpt(CURLOPT_CUSTOMREQUEST);
|
||||
echo 'complete request method: ' . $request_method . "\n";
|
||||
@@ -118,11 +142,19 @@ class MultiCurlTest extends PHPUnit_Framework_TestCase
|
||||
PHPUnit_Framework_Assert::assertFalse($head_complete_called);
|
||||
$head_complete_called = true;
|
||||
}
|
||||
if ($request_method === 'OPTIONS') {
|
||||
PHPUnit_Framework_Assert::assertTrue($options_before_send_called);
|
||||
PHPUnit_Framework_Assert::assertTrue($options_success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($options_error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($options_complete_called);
|
||||
$options_complete_called = true;
|
||||
}
|
||||
});
|
||||
|
||||
$multi_curl->addDelete(Test::TEST_URL);
|
||||
$multi_curl->addGet(Test::TEST_URL);
|
||||
$multi_curl->addHead(Test::TEST_URL);
|
||||
$multi_curl->addOptions(Test::TEST_URL);
|
||||
$multi_curl->start();
|
||||
|
||||
$this->assertTrue($delete_before_send_called);
|
||||
@@ -139,6 +171,11 @@ class MultiCurlTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertTrue($head_success_called);
|
||||
$this->assertFalse($head_error_called);
|
||||
$this->assertTrue($head_complete_called);
|
||||
|
||||
$this->assertTrue($options_before_send_called);
|
||||
$this->assertTrue($options_success_called);
|
||||
$this->assertFalse($options_error_called);
|
||||
$this->assertTrue($options_complete_called);
|
||||
}
|
||||
|
||||
public function testMultiCurlCallbackError()
|
||||
|
||||
Reference in New Issue
Block a user