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']; }
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); }); }
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 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 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(); }