mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 04:01:47 +00:00
Add example implementation of MultiCurl::addGet() with MultiCurlTest tests
This commit is contained in:
+1
-1
@@ -11,7 +11,7 @@ class Curl
|
||||
private $headers = array();
|
||||
private $options = array();
|
||||
|
||||
private $before_send_function = null;
|
||||
public $before_send_function = null;
|
||||
private $success_function = null;
|
||||
private $error_function = null;
|
||||
private $complete_function = null;
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace Curl;
|
||||
|
||||
class MultiCurl
|
||||
{
|
||||
public $curl_multi;
|
||||
public $curls = array();
|
||||
|
||||
private $before_send_function = null;
|
||||
private $success_function = null;
|
||||
private $error_function = null;
|
||||
private $complete_function = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->curl_multi = curl_multi_init();
|
||||
}
|
||||
|
||||
public function addDelete($url, $data = array())
|
||||
{
|
||||
}
|
||||
|
||||
public function addDownload($url, $filename)
|
||||
{
|
||||
}
|
||||
|
||||
public function addGet($url, $data = array())
|
||||
{
|
||||
$curl = new Curl();
|
||||
$curl->base_url = $url;
|
||||
$curl->setURL($url, $data);
|
||||
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
|
||||
$curl->setOpt(CURLOPT_HTTPGET, true);
|
||||
$this->addHandle($curl);
|
||||
return $curl;
|
||||
}
|
||||
|
||||
public function addHead($url, $data = array())
|
||||
{
|
||||
}
|
||||
|
||||
public function addOptions($url, $data = array())
|
||||
{
|
||||
}
|
||||
|
||||
public function addPatch($url, $data = array())
|
||||
{
|
||||
}
|
||||
|
||||
public function addPost($url, $data = array())
|
||||
{
|
||||
}
|
||||
|
||||
public function addPut($url, $data = array())
|
||||
{
|
||||
}
|
||||
|
||||
public function beforeSend($callback)
|
||||
{
|
||||
echo 'setting multicurl beforeSend' . "\n";
|
||||
$this->before_send_function = $callback;
|
||||
}
|
||||
|
||||
public function success($callback)
|
||||
{
|
||||
echo 'setting multicurl success' . "\n";
|
||||
$this->success_function = $callback;
|
||||
}
|
||||
|
||||
public function error($callback)
|
||||
{
|
||||
echo 'setting multicurl error' . "\n";
|
||||
$this->error_function = $callback;
|
||||
}
|
||||
|
||||
public function complete($callback)
|
||||
{
|
||||
echo 'setting multicurl complete' . "\n";
|
||||
$this->complete_function = $callback;
|
||||
}
|
||||
|
||||
public function start()
|
||||
{
|
||||
echo 'running start' . "\n";
|
||||
foreach ($this->curls as $ch) {
|
||||
$ch->call($ch->before_send_function);
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while ($active > 0);
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
foreach ($this->curls as $ch) {
|
||||
$ch->close();
|
||||
}
|
||||
|
||||
curl_multi_close($this->curl_multi);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->close();
|
||||
}
|
||||
|
||||
private function addHandle($curl)
|
||||
{
|
||||
echo 'adding handle' . "\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));
|
||||
}
|
||||
echo 'copying over callbacks from multicurl to curl' . "\n";
|
||||
$curl->beforeSend($this->before_send_function);
|
||||
$curl->success($this->success_function);
|
||||
$curl->error($this->error_function);
|
||||
$curl->complete($this->complete_function);
|
||||
$this->curls[] = $curl;
|
||||
$curl->id = count($this->curls);
|
||||
echo 'handle added' . "\n";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
require '../src/Curl/Curl.php';
|
||||
require '../src/Curl/MultiCurl.php';
|
||||
require 'Helper.php';
|
||||
|
||||
use \Curl\MultiCurl;
|
||||
//use \Curl\CaseInsensitiveArray;
|
||||
use \Helper\Test;
|
||||
|
||||
class MultiCurlTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testMultiCurlCallback()
|
||||
{
|
||||
$before_send_called = false;
|
||||
$success_called = false;
|
||||
$error_called = false;
|
||||
$complete_called = false;
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->beforeSend(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertFalse($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$before_send_called = true;
|
||||
});
|
||||
$multi_curl->success(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertTrue($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$success_called = true;
|
||||
});
|
||||
$multi_curl->error(function ($instance) use (
|
||||
&$error_called) {
|
||||
$error_called = true;
|
||||
});
|
||||
$multi_curl->complete(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertTrue($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertTrue($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$complete_called = true;
|
||||
});
|
||||
|
||||
$multi_curl->addGet(Test::TEST_URL);
|
||||
$multi_curl->start();
|
||||
|
||||
$this->assertTrue($before_send_called);
|
||||
$this->assertTrue($success_called);
|
||||
$this->assertFalse($error_called);
|
||||
$this->assertTrue($complete_called);
|
||||
}
|
||||
|
||||
public function testMultiCurlCallbackError()
|
||||
{
|
||||
$before_send_called = false;
|
||||
$success_called = false;
|
||||
$error_called = false;
|
||||
$complete_called = false;
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->beforeSend(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertFalse($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$before_send_called = true;
|
||||
});
|
||||
$multi_curl->success(function ($instance) use (
|
||||
&$success_called) {
|
||||
$success_called = true;
|
||||
});
|
||||
$multi_curl->error(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertTrue($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$error_called = true;
|
||||
});
|
||||
$multi_curl->complete(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertTrue($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertTrue($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
PHPUnit_Framework_Assert::assertTrue($instance->error);
|
||||
PHPUnit_Framework_Assert::assertTrue($instance->curl_error);
|
||||
PHPUnit_Framework_Assert::assertFalse($instance->http_error);
|
||||
PHPUnit_Framework_Assert::assertEquals(CURLE_OPERATION_TIMEOUTED, $instance->error_code);
|
||||
PHPUnit_Framework_Assert::assertEquals(CURLE_OPERATION_TIMEOUTED, $instance->curl_error_code);
|
||||
$complete_called = true;
|
||||
});
|
||||
|
||||
$multi_curl->addGet(Test::ERROR_URL);
|
||||
$multi_curl->start();
|
||||
|
||||
$this->assertTrue($before_send_called);
|
||||
$this->assertFalse($success_called);
|
||||
$this->assertTrue($error_called);
|
||||
$this->assertTrue($complete_called);
|
||||
}
|
||||
|
||||
public function testCurlCallback()
|
||||
{
|
||||
$before_send_called = false;
|
||||
$success_called = false;
|
||||
$error_called = false;
|
||||
$complete_called = false;
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
echo 'about to add get' . "\n";
|
||||
$get = $multi_curl->addGet(Test::TEST_URL);
|
||||
echo 'get added' . "\n";
|
||||
echo 'about to add before send' . "\n";
|
||||
$get->beforeSend(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
echo 'before send called' . "\n";
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertFalse($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$before_send_called = true;
|
||||
});
|
||||
echo 'about to set success' . "\n";
|
||||
$get->success(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
echo 'success called' . "\n";
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertTrue($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$success_called = true;
|
||||
});
|
||||
echo 'about to set error' . "\n";
|
||||
$get->error(function ($instance) use (
|
||||
&$error_called) {
|
||||
echo 'error called' . "\n";
|
||||
$error_called = true;
|
||||
});
|
||||
echo 'about to set complete' . "\n";
|
||||
$get->complete(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
echo 'complete called' . "\n";
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertTrue($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertTrue($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$complete_called = true;
|
||||
});
|
||||
|
||||
echo 'about to run start' . "\n";
|
||||
$multi_curl->start();
|
||||
echo 'calls done' . "\n";
|
||||
|
||||
$this->assertTrue($before_send_called);
|
||||
$this->assertTrue($success_called);
|
||||
$this->assertFalse($error_called);
|
||||
$this->assertTrue($complete_called);
|
||||
}
|
||||
|
||||
public function testCurlCallbackError()
|
||||
{
|
||||
$before_send_called = false;
|
||||
$success_called = false;
|
||||
$error_called = false;
|
||||
$complete_called = false;
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
echo 'about to add get' . "\n";
|
||||
$get = $multi_curl->addGet(Test::ERROR_URL);
|
||||
echo 'get added' . "\n";
|
||||
echo 'about to add before send' . "\n";
|
||||
$get->beforeSend(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
echo 'before send called' . "\n";
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertFalse($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$before_send_called = true;
|
||||
});
|
||||
echo 'about to set success' . "\n";
|
||||
$get->success(function ($instance) use (
|
||||
&$success_called) {
|
||||
echo 'success called' . "\n";
|
||||
$success_called = true;
|
||||
});
|
||||
echo 'about to set error' . "\n";
|
||||
$get->error(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
echo 'error called' . "\n";
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertTrue($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$error_called = true;
|
||||
});
|
||||
echo 'about to set complete' . "\n";
|
||||
$get->complete(function ($instance) use (
|
||||
&$before_send_called,
|
||||
&$success_called,
|
||||
&$error_called,
|
||||
&$complete_called) {
|
||||
echo 'complete called' . "\n";
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertTrue($before_send_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertTrue($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$complete_called = true;
|
||||
});
|
||||
|
||||
echo 'about to run start' . "\n";
|
||||
$multi_curl->start();
|
||||
echo 'calls done' . "\n";
|
||||
|
||||
$this->assertTrue($before_send_called);
|
||||
$this->assertFalse($success_called);
|
||||
$this->assertTrue($error_called);
|
||||
$this->assertTrue($complete_called);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user