function init_record($queries) { $GLOBALS['init_record_return'] = array(); if (isset($queries['SalesForce'])) { $GLOBALS['init_record_return']['SalesForce'] = array(); } $sforce_multi_curl = new MultiCurl(); // SalesForce handler $sforce_multi_curl->success(function ($instance) { $response = $instance->response; if (SalesForce::is_valid_session($response)) { if (isset($response->records[0])) { array_push($GLOBALS['init_record_return']['SalesForce'], $response->records[0]); } } else { return false; } }); $user_id = get_current_user_id(); $account_id = get_user_meta($user_id, 'sforce_account_id', true); $sforce_oauth_token = get_option('sforce_oauth_token'); if (isset($queries['SalesForce'])) { foreach ($queries['SalesForce'] as $object_name => $filters) { foreach ($filters as $filter_name => $fields) { $query = urlencode("SELECT " . implode(',', $fields) . " FROM " . $object_name . " WHERE " . stripcslashes($filter_name)); $url = SalesForce::get_api_base() . '/query/?q=' . $query; $sforce_multi_curl->setHeader('Authorization', 'Bearer ' . $sforce_oauth_token); $sforce_multi_curl->addGet($url); } } } $sforce_multi_curl->start(); return $GLOBALS['init_record_return']; }
public function testClose() { $multi_curl = new MultiCurl(); $multi_curl->addGet(Test::TEST_URL); $multi_curl->start(); $this->assertTrue(is_resource($multi_curl->multiCurl)); $multi_curl->close(); $this->assertFalse(is_resource($multi_curl->multiCurl)); }
<?php require '../src/Curl/Curl.php'; require '../src/Curl/MultiCurl.php'; use Curl\Curl; use Curl\MultiCurl; $multi_curl = new MultiCurl(); $multi_curl->addGet('https://www.google.com/search', array('q' => 'hello world')); $multi_curl->addGet('https://duckduckgo.com/', array('q' => 'hello world')); $multi_curl->addGet('https://www.bing.com/search', array('q' => 'hello world')); $multi_curl->start();
function load_page_data($queries) { $GLOBALS['load_page_return'] = array(); if (isset($queries['SalesForce'])) { $GLOBALS['load_page_return']['SalesForce'] = array(); } if (isset($queries['ShareFile'])) { $GLOBALS['load_page_return']['ShareFile'] = array(); } $sforce_multi_curl = new MultiCurl(); $sfile_multi_curl = new MultiCurl(); // SalesForce handler $sforce_multi_curl->success(function ($instance) { $response = $instance->response; if (SalesForce::is_valid_session($response)) { if (isset($response->records[0])) { array_push($GLOBALS['load_page_return']['SalesForce'], $response->records[0]); } } else { return false; } }); // ShareFile handler $sfile_multi_curl->success(function ($instance) { $response = $instance->response; if (!empty($response->Results)) { array_push($GLOBALS['load_page_return']['ShareFile'], array($response->Results[0]->Details => $response->Results[0]->ItemID)); } }); // SalesForce $sforce_oauth_token = get_option('sforce_oauth_token'); if (isset($queries['SalesForce'])) { foreach ($queries['SalesForce'] as $object_name => $filters) { foreach ($filters as $filter_name => $fields) { $query = urlencode("SELECT " . implode(',', $fields) . " FROM " . $object_name . " WHERE " . stripcslashes($filter_name)); $url = SalesForce::get_api_base() . '/query/?q=' . $query; $sforce_multi_curl->setHeader('Authorization', 'Bearer ' . $sforce_oauth_token); $sforce_multi_curl->addGet($url); } } } // ShareFile $user_id = get_current_user_id(); $parent_folder = get_user_meta($user_id, 'sfile_id', true); $sfile_oauth_token = get_option('sfile_oauth_token'); $sfile_api_url_base = get_option('sfile_api_url_base'); if (isset($queries['ShareFile'])) { foreach ($queries['ShareFile'] as $sub_folder => $files) { if (ShareFile::get_item_id($sub_folder)) { foreach ($files as $file) { $url = $sfile_api_url_base . "/sf/v3/Items/AdvancedSimpleSearch"; $query = array('Query' => array("ParentID" => $parent_folder, "SearchQuery" => $file, "ItemNameOnly" => false)); $data = json_encode($query); $sfile_multi_curl->setHeader('Authorization', 'Bearer ' . $sfile_oauth_token); $sfile_multi_curl->setHeader('Content-Type', 'application/json'); $sfile_multi_curl->addPost($url, $data); } } else { ShareFile::create_folder($sub_folder, ''); } } } $sforce_multi_curl->start(); $sfile_multi_curl->start(); return $GLOBALS['load_page_return']; }
use Curl\MultiCurl; $server_count = 10; $urls = array(); $port = 8000; for ($i = 0; $i < $server_count; $i++) { $port += 1; $urls[] = 'http://localhost:' . $port . '/'; } $multi_curl = new MultiCurl(); $success = 0; $error = 0; $complete = 0; $multi_curl->success(function ($instance) use(&$success) { $success += 1; }); $multi_curl->error(function ($instance) use(&$error) { $error += 1; }); $multi_curl->complete(function ($instance) use(&$complete) { $complete += 1; }); $limit = 1000; for ($i = 0; $i < $limit; $i++) { $url = $urls[mt_rand(0, count($urls) - 1)]; $multi_curl->addGet($url); } $multi_curl->start(); echo 'complete: ' . $complete . "\n"; echo 'success: ' . $success . "\n"; echo 'error: ' . $error . "\n"; echo 'done' . "\n";
public function testSetUrl() { $data = array('key' => 'value'); $multi_curl = new MultiCurl(); $multi_curl->setHeader('X-DEBUG-TEST', 'get'); $multi_curl->setUrl(Test::TEST_URL); $multi_curl->addDelete($data)->complete(function ($instance) { PHPUnit_Framework_Assert::assertEquals(Test::TEST_URL, $instance->baseUrl); PHPUnit_Framework_Assert::assertEquals('key=value', $instance->response); }); $multi_curl->start(); $multi_curl = new MultiCurl(); $multi_curl->setHeader('X-DEBUG-TEST', 'get'); $multi_curl->setUrl(Test::TEST_URL); $multi_curl->addGet($data)->complete(function ($instance) { PHPUnit_Framework_Assert::assertEquals(Test::TEST_URL, $instance->baseUrl); PHPUnit_Framework_Assert::assertEquals('key=value', $instance->response); }); $multi_curl->start(); $multi_curl = new MultiCurl(); $multi_curl->setHeader('X-DEBUG-TEST', 'get'); $multi_curl->setUrl(Test::TEST_URL); $multi_curl->addHead($data)->complete(function ($instance) { PHPUnit_Framework_Assert::assertEquals(Test::TEST_URL, $instance->baseUrl); PHPUnit_Framework_Assert::assertEquals('HEAD /?key=value HTTP/1.1', $instance->requestHeaders['Request-Line']); }); $multi_curl->start(); $multi_curl = new MultiCurl(); $multi_curl->setHeader('X-DEBUG-TEST', 'get'); $multi_curl->setUrl(Test::TEST_URL); $multi_curl->addOptions($data)->complete(function ($instance) { PHPUnit_Framework_Assert::assertEquals(Test::TEST_URL, $instance->baseUrl); PHPUnit_Framework_Assert::assertEquals('key=value', $instance->response); }); $multi_curl = new MultiCurl(); $multi_curl->setHeader('X-DEBUG-TEST', 'request_method'); $multi_curl->setUrl(Test::TEST_URL); $multi_curl->addPatch($data)->complete(function ($instance) { PHPUnit_Framework_Assert::assertEquals(Test::TEST_URL, $instance->baseUrl); PHPUnit_Framework_Assert::assertEquals('PATCH', $instance->response); }); $multi_curl = new MultiCurl(); $multi_curl->setHeader('X-DEBUG-TEST', 'post'); $multi_curl->setUrl(Test::TEST_URL); $multi_curl->addPost($data)->complete(function ($instance) { PHPUnit_Framework_Assert::assertEquals(Test::TEST_URL, $instance->baseUrl); PHPUnit_Framework_Assert::assertEquals('key=value', $instance->response); }); $multi_curl = new MultiCurl(); $multi_curl->setHeader('X-DEBUG-TEST', 'put'); $multi_curl->setUrl(Test::TEST_URL); $multi_curl->addPut($data)->complete(function ($instance) { PHPUnit_Framework_Assert::assertEquals(Test::TEST_URL, $instance->baseUrl); PHPUnit_Framework_Assert::assertEquals('key=value', $instance->response); }); }
public function testUnsetHeader() { $request_key = 'X-Request-Id'; $request_value = '1'; $data = array('test' => 'server', 'key' => 'HTTP_X_REQUEST_ID'); $multi_curl = new MultiCurl(); $multi_curl->setHeader($request_key, $request_value); $multi_curl->addGet(Test::TEST_URL, $data)->complete(function ($instance) use($request_value) { PHPUnit_Framework_Assert::assertEquals($request_value, $instance->response); }); $multi_curl->start(); $multi_curl = new MultiCurl(); $multi_curl->setHeader($request_key, $request_value); $multi_curl->unsetHeader($request_key); $multi_curl->addGet(Test::TEST_URL, $data)->complete(function ($instance) { PHPUnit_Framework_Assert::assertEquals('', $instance->response); }); $multi_curl->start(); }
public function testAddRequestAfterStart() { $this->markTestSkipped('Callbacks not permitted at this time. SugarCRM disallows the use of call_user_func[_array]'); $multi_curl = new MultiCurl(); $urls = array(); $copy_of_urls = array(); for ($i = 0; $i < 10; $i++) { $url = Test::TEST_URL . '?' . md5(mt_rand()); $urls[] = $url; $copy_of_urls[] = $url; } $urls_called = array(); $multi_curl->complete(function ($instance) use(&$multi_curl, &$urls, &$urls_called) { $urls_called[] = $instance->url; $next_url = array_pop($urls); if (!($next_url === null)) { $multi_curl->addGet($next_url); } }); $multi_curl->addGet(array_pop($urls)); $multi_curl->start(); $this->assertNotEmpty($copy_of_urls); $this->assertNotEmpty($urls_called); $this->assertEquals(count($copy_of_urls), count($urls_called)); foreach ($copy_of_urls as $url) { $this->assertTrue(in_array($url, $urls_called, true)); } }
public function testAddRequestAfterStart() { $multi_curl = new MultiCurl(); $urls = array(); $copy_of_urls = array(); for ($i = 0; $i < 10; $i++) { $url = Test::TEST_URL . '?' . md5(mt_rand()); $urls[] = $url; $copy_of_urls[] = $url; } $urls_called = array(); $multi_curl->complete(function ($instance) use(&$multi_curl, &$urls, &$urls_called) { $urls_called[] = $instance->url; $next_url = array_pop($urls); if (!($next_url === null)) { $multi_curl->addGet($next_url); } }); $multi_curl->addGet(array_pop($urls)); $multi_curl->start(); $this->assertNotEmpty($copy_of_urls); $this->assertNotEmpty($urls_called); $this->assertEquals(count($copy_of_urls), count($urls_called)); foreach ($copy_of_urls as $url) { $this->assertTrue(in_array($url, $urls_called, true)); } }
<?php require __DIR__ . '/vendor/autoload.php'; use Curl\MultiCurl; $headers = array('Content-Type' => 'application/json', 'X-CUSTOM-HEADER' => 'my-custom-header'); $multi_curl = new MultiCurl(); $multi_curl->beforeSend(function ($instance) use($headers) { foreach ($headers as $key => $value) { $instance->setHeader($key, $value); } }); $multi_curl->addGet('https://www.example.com/'); $multi_curl->addGet('https://www.example.org/'); $multi_curl->addGet('https://www.example.net/'); $multi_curl->start();