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']; }
function save_page_data($queries) { $GLOBALS['save_page_return'] = array(); $multi_curl = new MultiCurl(); $multi_curl->success(function ($instance) { if ($instance->http_status_code == '204') { $response = array('status' => 'success'); } else { $response = array('status' => 'error'); } array_push($GLOBALS['save_page_return'], $response); }); $multi_curl->setHeader('Authorization', 'Bearer ' . get_option('sforce_oauth_token')); $multi_curl->setHeader('Content-Type', 'application/json'); foreach ($queries as $api_endpoint => $fields) { $url = get_option('sforce_instance_url') . $api_endpoint; $fields_json = json_encode($fields, JSON_FORCE_OBJECT); $multi_curl->addPatch($url, $fields_json); } $multi_curl->start(); return $GLOBALS['save_page_return']; }
<?php require '../src/Curl/Curl.php'; require '../src/Curl/MultiCurl.php'; use Curl\Curl; use Curl\MultiCurl; $multi_curl = new MultiCurl(); $multi_curl->addDownload('https://php.net/images/logos/php-med-trans.png', '/tmp/php-med-trans.png'); $multi_curl->addDownload('https://upload.wikimedia.org/wikipedia/commons/c/c1/PHP_Logo.png', '/tmp/PHP_Logo.png'); $multi_curl->start();
<?php require __DIR__ . '/vendor/autoload.php'; 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";
<?php require __DIR__ . '/vendor/autoload.php'; use Curl\MultiCurl; $multi_curl = new MultiCurl(); // HINT: If API documentation refers to using something like curl -F "myimage=image.png", // curl --form "myimage=image.png", or the html form is similar to <form enctype="multipart/form-data" method="post">, // then try uncommenting the following line: // $multi_curl->setHeader('Content-Type', 'multipart/form-data'); $multi_curl->addPost('https://httpbin.org/post', array('image' => new CURLFile('the-lorax.jpg'))); $multi_curl->addPost('https://httpbin.org/post', array('image' => new CURLFile('swomee-swans.jpg'))); $multi_curl->addPost('https://httpbin.org/post', array('image' => new CURLFile('truffula-trees.jpg'))); $multi_curl->start();
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); }); }
<?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();
<?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();
<?php require '../src/Curl/Curl.php'; require '../src/Curl/MultiCurl.php'; use Curl\Curl; use Curl\MultiCurl; $multi_curl = new MultiCurl(); $multi_curl->addPatch('https://httpbin.org/patch', array('id' => '123', 'body' => 'hello world!')); $multi_curl->addPatch('https://httpbin.org/patch', array('id' => '456', 'body' => 'hello world!')); $multi_curl->start();
<?php require __DIR__ . '/vendor/autoload.php'; use Curl\MultiCurl; $multi_curl = new MultiCurl(); $multi_curl->addDelete('https://httpbin.org/delete', array('id' => '123')); $multi_curl->addDelete('https://httpbin.org/delete', array('id' => '456')); $multi_curl->start();
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 '../src/Curl/Curl.php'; require '../src/Curl/MultiCurl.php'; use Curl\Curl; use Curl\MultiCurl; $multi_curl = new MultiCurl(); $multi_curl->addPost('https://httpbin.org/post', array('to' => 'alice', 'subject' => 'hi', 'body' => 'hi Alice')); $multi_curl->addPost('https://httpbin.org/post', array('to' => 'bob', 'subject' => 'hi', 'body' => 'hi Bob')); $multi_curl->start();
<?php require __DIR__ . '/vendor/autoload.php'; use Curl\MultiCurl; $multi_curl = new MultiCurl(); $multi_curl->addPut('https://httpbin.org/put', array('id' => '123', 'subject' => 'hello', 'body' => 'hello')); $multi_curl->addPut('https://httpbin.org/put', array('id' => '456', 'subject' => 'hello', 'body' => 'hello')); $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']; }
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)); } }
<?php require __DIR__ . '/vendor/autoload.php'; use Curl\Curl; use Curl\MultiCurl; $multi_curl = new MultiCurl(); $multi_curl->success(function ($instance) { echo 'call to "' . $instance->url . '" was successful.' . "\n"; echo 'response: ' . $instance->response . "\n"; }); $multi_curl->error(function ($instance) { echo 'call to "' . $instance->url . '" was unsuccessful.' . "\n"; echo 'error code: ' . $instance->errorCode . "\n"; echo 'error message: ' . $instance->errorMessage . "\n"; }); $multi_curl->complete(function ($instance) { echo 'call completed' . "\n"; }); $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();
public function testSequentialId() { $completed = array(); $multi_curl = new MultiCurl(); $multi_curl->complete(function ($instance) use(&$completed) { $completed[] = $instance; }); for ($i = 0; $i < 100; $i++) { $multi_curl->addPost(Test::TEST_URL, $i); } $multi_curl->start(); foreach ($completed as $instance) { $sequential_id = $instance->getOpt(CURLOPT_POSTFIELDS); $this->assertEquals($sequential_id, $instance->id); } }
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)); }
public function testMultiPostRedirectGet() { // Deny post-redirect-get $multi_curl = new MultiCurl(Test::TEST_URL); $multi_curl->setOpt(CURLOPT_FOLLOWLOCATION, true); $multi_curl->setHeader('X-DEBUG-TEST', 'post_redirect_get'); $multi_curl->addPost(array(), false)->complete(function ($instance) { PHPUnit_Framework_Assert::assertEquals('Redirected: POST', $instance->response); }); $multi_curl->start(); // Allow post-redirect-get $multi_curl = new MultiCurl(Test::TEST_URL); $multi_curl->setOpt(CURLOPT_FOLLOWLOCATION, true); $multi_curl->setHeader('X-DEBUG-TEST', 'post_redirect_get'); $multi_curl->addPost(array(), true)->complete(function ($instance) { PHPUnit_Framework_Assert::assertEquals('Redirected: GET', $instance->response); }); $multi_curl->start(); }