/** * @inheritDoc */ public function batchLoad($requests) { /** @var IApiRequest[] $originals */ $originals = []; $batchRequests = []; foreach ($requests as $i => $request) { if ($request instanceof IApiRequest) { $originals[$i] = $request; $reqDet = $request->getRequestDetail(); $batchReq = ['url' => $reqDet->getUrl(), 'headers' => $this->_buildHeaders($reqDet), 'data' => $this->_buildData($reqDet), 'type' => $reqDet->getMethod(), 'options' => $reqDet->getOptions(), 'cookies' => []]; $batchRequests[$i] = $batchReq; } } try { $responses = \Requests::request_multiple($batchRequests); foreach ($responses as $id => $response) { $originals[$id]->setRawResult($this->_getResult($response)); } return $this; } catch (\Exception $e) { $this->_handleException($e); } }
/** * @inheritdoc */ public function multiRequest(array $urls) { $requests = array(); foreach ($urls as $urlName => $urlData) { if (is_string($urlData)) { $urlData = array($urlData, array()); } $urlOptions = new Options($urlData[1]); $method = $urlOptions->get('method', 'GET', 'up'); $args = $urlOptions->get('args'); $url = 'GET' === $method ? Url::addArg((array) $args, $urlData[0]) : $urlData[0]; $args = 'GET' !== $method ? $args : array(); $requests[$urlName] = array('url' => $url, 'headers' => $urlOptions->getHeaders(), 'data' => $args, 'type' => $method, 'options' => $this->_getClientOptions($urlOptions)); } $httpResults = \Requests::request_multiple($requests); /** @var string $resName */ /** @var \Requests_Response $httpResult */ $result = array(); foreach ($httpResults as $resName => $httpResult) { $result[$resName] = array($httpResult->status_code, $httpResult->headers->getAll(), $httpResult->body); } return $result; }
public function testMultipleToFile() { $requests = array('get' => array('url' => httpbin('/get'), 'options' => array('filename' => tempnam(sys_get_temp_dir(), 'RLT'))), 'post' => array('url' => httpbin('/post'), 'type' => Requests::POST, 'data' => 'test', 'options' => array('filename' => tempnam(sys_get_temp_dir(), 'RLT')))); $responses = Requests::request_multiple($requests, $this->getOptions()); // GET request $contents = file_get_contents($requests['get']['options']['filename']); $result = json_decode($contents, true); $this->assertEquals(httpbin('/get'), $result['url']); $this->assertEmpty($result['args']); unlink($requests['get']['options']['filename']); // POST request $contents = file_get_contents($requests['post']['options']['filename']); $result = json_decode($contents, true); $this->assertEquals(httpbin('/post'), $result['url']); $this->assertEquals('test', $result['data']); unlink($requests['post']['options']['filename']); }
public function us_fan_counts() { header('content-type: application/json'); $ajax_debug = UltimateSocialDeux::opt('us_ajax_debug', false); if (wp_verify_nonce($_REQUEST['nonce'], 'us_nonce') || $ajax_debug) { if (!class_exists('Requests')) { require_once plugin_dir_path(__FILE__) . '/includes/Requests.php'; Requests::register_autoloader(); } $args = $_REQUEST['args'] ? $_REQUEST['args'] : die('Args not set'); $args = urldecode(stripslashes($args)); $args = json_decode($args, true); $option = maybe_unserialize(get_option('us_fan_counts', array())); $json = array(); $networks = explode(',', $args['networks']); $networks = array_keys(array_flip($networks)); $timestamp = time(); foreach ($networks as $key => $network) { $option[$network]['count'] = isset($option[$network]['count']) ? $option[$network]['count'] : 0; $json[$network]['count'] = $option[$network]['count']; $option[$network]['timestamp'] = $timestamp; $id = ''; $key = ''; $secret = ''; $api = ''; $app = ''; $user = ''; $name = ''; $username = ''; switch ($network) { case 'facebook': $app_token = UltimateSocialDeux::opt('us_facebook_token'); $fb_token = $app_token ? "?access_token=" . $app_token : ''; $id = UltimateSocialDeux::opt('us_facebook_id'); if ($id) { $requests[$network] = array('url' => "https://graph.facebook.com/" . $id . $fb_token); } break; case 'twitter': $id = UltimateSocialDeux::opt('us_twitter_id'); $key = UltimateSocialDeux::opt('us_twitter_key'); $secret = UltimateSocialDeux::opt('us_twitter_secret'); if ($id && $key && $secret) { $token = get_option('us_fan_count_twitter_token'); if (!$token) { $credentials = $key . ':' . $secret; $encode = base64_encode($credentials); $args = array('method' => 'POST', 'httpversion' => '1.1', 'blocking' => true, 'headers' => array('Authorization' => 'Basic ' . $encode, 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'), 'body' => array('grant_type' => 'client_credentials')); add_filter('https_ssl_verify', '__return_false'); $response = wp_remote_post('https://api.twitter.com/oauth2/token', $args); $keys = json_decode(wp_remote_retrieve_body($response)); if (!isset($keys->errors) && $keys) { update_option('us_fan_count_twitter_token', $keys->access_token); $token = $keys->access_token; } } $requests[$network] = array('url' => 'https://api.twitter.com/1.1/users/show.json?screen_name=' . $id, 'headers' => array('Authorization' => "Bearer {$token}")); } break; case 'google': $id = UltimateSocialDeux::opt('us_google_id'); $key = UltimateSocialDeux::opt('us_google_key'); if ($key && $id) { $requests[$network] = array('url' => "https://www.googleapis.com/plus/v1/people/" . $id . "?key=" . $key); } break; case 'behance': $id = UltimateSocialDeux::opt('us_behance_id'); $api = UltimateSocialDeux::opt('us_behance_api'); if ($id && $api) { $requests[$network] = array('url' => "http://www.behance.net/v2/users/" . $id . "?api_key=" . $api); } break; case 'delicious': $id = UltimateSocialDeux::opt('us_delicious_id'); if ($id) { $requests[$network] = array('url' => "http://feeds.delicious.com/v2/json/userinfo/" . $id); } break; case 'linkedin': $id = UltimateSocialDeux::opt('us_linkedin_id'); $app = UltimateSocialDeux::opt('us_linkedin_app'); $api = UltimateSocialDeux::opt('us_linkedin_api'); if (!class_exists('LinkedIn')) { require_once plugin_dir_path(__FILE__) . 'includes/linkedin/linkedin.php'; } if (!class_exists('OAuthServer')) { require_once plugin_dir_path(__FILE__) . 'includes/OAuth/OAuth.php'; } if ($id && $api && $id) { $count = 0; $opt = array('appKey' => $app, 'appSecret' => $api, 'callbackUrl' => ''); $api_call = new LinkedIn($opt); $response = $api_call->company(trim('universal-name=' . $id . ':(num-followers)')); if ($ajax_debug) { print_r($response); } if (false !== $response['success']) { $company = new SimpleXMLElement($response['linkedin']); if (isset($company->{'num-followers'})) { $count = intval(current($company->{'num-followers'})); } } $option[$network]['count'] = $count; $json[$network]['count'] = $count; } break; case 'youtube': $id = UltimateSocialDeux::opt('us_youtube_id'); if ($id) { $requests[$network] = array('url' => "http://gdata.youtube.com/feeds/api/users/" . $id . "?alt=json"); } break; case 'soundcloud': $id = UltimateSocialDeux::opt('us_soundcloud_id'); $user = UltimateSocialDeux::opt('us_soundcloud_username'); if ($id && $user) { $requests[$network] = array('url' => 'http://api.soundcloud.com/users/' . $user . '.json?client_id=' . $id); } break; case 'vimeo': $id = UltimateSocialDeux::opt('us_vimeo_id'); if ($id) { $requests[$network] = array('url' => "http://vimeo.com/api/v2/channel/" . $id . "/info.json"); } break; case 'dribbble': $id = UltimateSocialDeux::opt('us_dribbble_id'); if ($id) { $requests[$network] = array('url' => "http://api.dribbble.com/" . $id); } break; case 'github': $id = UltimateSocialDeux::opt('us_github_id'); if ($id) { $requests[$network] = array('url' => "https://api.github.com/users/" . $id); } break; case 'envato': $id = UltimateSocialDeux::opt('us_envato_id'); if ($id) { $requests[$network] = array('url' => "http://marketplace.envato.com/api/edge/user:"******".json"); } break; case 'instagram': $api = UltimateSocialDeux::opt('us_instagram_api'); $id = explode(".", $api); if ($api && $id) { $requests[$network] = array('url' => "https://api.instagram.com/v1/users/" . $id[0] . "/?access_token=" . $api); } break; case 'mailchimp': $name = UltimateSocialDeux::opt('us_mailchimp_name'); $api = UltimateSocialDeux::opt('us_mailchimp_api'); $count = 0; if ($name && $api) { if (!class_exists('MCAPI')) { require_once plugin_dir_path(__FILE__) . 'includes/MCAPI.class.php'; } $api = new MCAPI($api); $retval = $api->lists(); if ($ajax_debug) { print_r($retval); } if (count($retval['data']) > 0) { foreach ($retval['data'] as $list) { if ($list['name'] == $name) { $count = intval($list['stats']['member_count']); break; } } } } $option[$network]['count'] = intval($count); $json[$network]['count'] = intval($count); break; case 'vkontakte': $id = UltimateSocialDeux::opt('us_vkontakte_id'); if ($id) { $requests[$network] = array('url' => "http://api.vk.com/method/groups.getById?gid=" . $id . "&fields=members_count"); } break; case 'pinterest': $username = UltimateSocialDeux::opt('us_pinterest_username'); if ($username) { $requests[$network] = array('url' => 'http://www.pinterest.com/' . $username . '/'); } break; case 'flickr': $id = UltimateSocialDeux::opt('us_flickr_id'); $api = UltimateSocialDeux::opt('us_flickr_api'); if ($id && $api) { $requests[$network] = array('url' => "https://api.flickr.com/services/rest/?method=flickr.groups.getInfo&api_key=" . $api . "&group_id=" . $id . "&format=json&nojsoncallback=1"); } break; case 'feedpress': $manual = intval(UltimateSocialDeux::opt('us_feedpress_manual', 0)); $url = UltimateSocialDeux::opt('us_feedpress_url'); if (filter_var($url, FILTER_VALIDATE_URL)) { $requests[$network] = array('url' => $url); } if ($manual) { $option[$network]['count'] = $manual; $json[$network]['count'] = $manual; } break; default: unset($option[$network]); unset($json[$network]); break; } } $responses = !empty($requests) ? Requests::request_multiple($requests) : die('No requests sent.'); foreach ($responses as $network => $data) { switch ($network) { case 'facebook': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['likes']); $json[$network]['count'] = intval($content['likes']); } break; case 'twitter': case 'soundcloud': case 'dribbble': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['followers_count']); $json[$network]['count'] = intval($content['followers_count']); } break; case 'google': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['circledByCount']); $json[$network]['count'] = intval($content['circledByCount']); } break; case 'behance': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['user']['stats']['followers']); $json[$network]['count'] = intval($content['user']['stats']['followers']); } break; case 'delicious': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content[2]['n']); $json[$network]['count'] = intval($content[2]['n']); } break; case 'youtube': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['entry']['yt$statistics']['subscriberCount']); $json[$network]['count'] = intval($content['entry']['yt$statistics']['subscriberCount']); } break; case 'vimeo': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['total_subscribers']); $json[$network]['count'] = intval($content['total_subscribers']); } break; case 'github': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['followers']); $json[$network]['count'] = intval($content['followers']); } break; case 'envato': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['user']['followers']); $json[$network]['count'] = intval($content['user']['followers']); } break; case 'instagram': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['data']['counts']['followed_by']); $json[$network]['count'] = intval($content['data']['counts']['followed_by']); } break; case 'vkontakte': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['response'][0]['members_count']); $json[$network]['count'] = intval($content['response'][0]['members_count']); } break; case 'pinterest': if (isset($responses[$network]->body)) { $html = $responses[$network]->body; if ($ajax_debug) { print_r($html); } $doc = new DOMDocument(); @$doc->loadHTML($html); $metas = $doc->getElementsByTagName('meta'); for ($i = 0; $i < $metas->length; $i++) { $meta = $metas->item($i); if ($meta->getAttribute('name') == 'pinterestapp:followers') { $count = intval($meta->getAttribute('content')); break; } } $option[$network]['count'] = $count; $json[$network]['count'] = $count; } break; case 'flickr': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['group']['members']['_content']); $json[$network]['count'] = intval($content['group']['members']['_content']); } break; case 'feedpress': if (isset($responses[$network]->body)) { $content = $responses[$network]->body; if ($ajax_debug) { print_r($content); } $content = json_decode($content, true); $option[$network]['count'] = intval($content['subscribers']) + $manuel; $json[$network]['count'] = intval($content['subscribers']) + $manuel; } break; } } maybe_serialize(update_option('us_fan_counts', $option)); echo str_replace('\\/', '/', json_encode($json)); } else { die('Nonce not verified'); } die; }
/** * Send multiple HTTP requests simultaneously * * @see Requests::request_multiple() * * @param array $requests Requests data (see {@see Requests::request_multiple}) * @param array $options Global and default options (see {@see Requests::request}) * @return array Responses (either Requests_Response or a Requests_Exception object) */ public function request_multiple($requests, $options = array()) { foreach ($requests as $key => $request) { $requests[$key] = $this->merge_request($request, false); } $options = array_merge($this->options, $options); // Disallow forcing the type, as that's a per request setting unset($options['type']); return Requests::request_multiple($requests, $options); }
<?php // First, include Requests include '../library/Requests.php'; // Next, make sure Requests can load internal classes Requests::register_autoloader(); // Setup what we want to request $requests = array(array('url' => 'http://httpbin.org/get', 'headers' => array('Accept' => 'application/javascript')), 'post' => array('url' => 'http://httpbin.org/post', 'data' => array('mydata' => 'something')), 'delayed' => array('url' => 'http://httpbin.org/delay/10', 'options' => array('timeout' => 20))); // Setup a callback function my_callback(&$request, $id) { var_dump($id, $request); } // Tell Requests to use the callback $options = array('complete' => 'my_callback'); // Send the request! $responses = Requests::request_multiple($requests, $options); // Note: the response from the above call will be an associative array matching // $requests with the response data, however we've already handled it in // my_callback() anyway! // // If you don't believe me, uncomment this: # var_dump($responses);