public function testSharedCookies() { $session = new Requests_Session(httpbin('/')); $options = array('follow_redirects' => false); $response = $session->get('/cookies/set?requests-testcookie=testvalue', array(), $options); $this->assertEquals(302, $response->status_code); // Check the cookies $response = $session->get('/cookies'); $this->assertTrue($response->success); // Check the response $data = json_decode($response->body, true); $this->assertNotNull($data); $this->assertArrayHasKey('cookies', $data); $cookies = array('requests-testcookie' => 'testvalue'); $this->assertEquals($cookies, $data['cookies']); }
/** * Performs a parse query to the wiki API. * @param array $array array of details to be passed in the query. * @return array unserialized php output from the wiki API. */ public function parse($array) { $array['action'] = 'parse'; $array['format'] = 'php'; $apiResult = $this->session->get($this->api . '?' . http_build_query($array)); return unserialize($apiResult->body); }
/** * Retrieves the given $url and returns any capture groups in $regex for the all matches or false if not found. The * structure of the returned capture groups matches preg_match_all: * http://php.net/manual/en/function.preg-match-all.php with the flag PREG_SET_ORDER. * * @param \Requests_Session $session The session to send the request from. * @param string $url The page URL. * @param string $regex The regex to use. * * @return array|false The capture groups if found, otherwise false. */ public static function findAll(\Requests_Session $session, $url, $regex) { $r = $session->get($url); if (preg_match_all($regex, $r->body, $matches, PREG_SET_ORDER)) { return $matches; } return false; }
/** * Downloads data from the given URL. * * @param string $url The URL to download from * @return mixed The downloaded data (string), or null if error */ public function download($url) { $getResult = $this->session->get($url); if (!$getResult->success) { $this->error = array(); $this->error['file'] = 'Download error (HTTP status: ' . $getResult->status_code . ')'; $this->error['http'] = $getResult->status_code; return null; } return $getResult->body; }
/** * Получение формы входа(логина) в соц сети * * @param $client_id * @param array $scope * @return \Requests_Response */ private function login_form($client_id, array $scope) { $params = array('client_id' => $client_id, 'scope' => $scope, 'redirect_uri' => 'http://oauth.vk.com/blank.html', 'display' => 'mobile', 'response_type' => 'token'); $url = 'https://oauth.vk.com/authorize?' . http_build_query($params); return $this->session->get($url); }
<?php // First, include Requests include '../library/Requests.php'; // Next, make sure Requests can load internal classes Requests::register_autoloader(); // Set up our session $session = new Requests_Session('http://httpbin.org/'); $session->headers['Accept'] = 'application/json'; $session->useragent = 'Awesomesauce'; // Now let's make a request! $request = $session->get('/get'); // Check what we received var_dump($request); // Let's check our user agent! $request = $session->get('/user-agent'); // And check again var_dump($request);
//$requestProxy = new Requests_Proxy_HTTP($requestProxyParams); $requestSession->options['proxy']['type'] = 'HTTP'; break; case 'SOCKS5': case 'CURLPROXY_SOCKS5': //BC for old constant name $requestSession->options['proxy']['type'] = 'SOCKS5'; break; default: die('Proxy type should be either "HTTP" or "SOCKS5", check your configuration file'); } } foreach ($files as $file) { try { $requestSession->filename = $file['destination']; $result = $requestSession->get($file['path']); if ($result->success === true) { echo $file['description'] . ' successfully downloaded<br>' . "\n"; } } catch (Requests_Exception $e) { echo 'Error occurred while downloading ' . $file['description'] . ': ' . $e->getMessage() . "<br>\n"; } ob_flush(); flush(); } echo 'Download complete, unpacking files...<br>' . "\n"; ob_flush(); flush(); } elseif (!in_array('exec', array_map('trim', explode(',', ini_get('disable_functions'))))) { //wget $proxyString = '';
/** * Retrieve an array of information about the current * user from the MyEMSL user API * * @method get_user_info * * @return array * * @author Ken Auberry <*****@*****.**> */ function get_user_info() { // $protocol = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "https" : "http"; $protocol = "http"; $basedir = 'myemsl'; // $url_base = dirname(dirname($this->myemsl_ini['getuser']['prefix'])); $url_base = "{$protocol}://localhost"; $options = array('verify' => FALSE, 'timeout' => 60, 'connect_timeout' => 30); $headers = array(); foreach ($_COOKIE as $cookie_name => $cookie_value) { $headers[] = "{$cookie_name}={$cookie_value}"; } $headers = array('Cookie' => implode(';', $headers)); $session = new Requests_Session($url_base, $headers, array(), $options); try { $response = $session->get('/myemsl/userinfo'); $user_info = json_decode($response->body, TRUE); } catch (Exception $e) { $user_info = array('error' => 'Unable to retrieve User Information'); return $user_info; } $DB_myemsl = $this->load->database('default', TRUE); // go retrieve the instrument/group lookup table $DB_myemsl->like('type', 'Instrument.')->or_like('type', 'omics.dms.instrument'); $query = $DB_myemsl->get('groups'); $inst_group_lookup = array(); if ($query && $query->num_rows() > 0) { foreach ($query->result() as $row) { if ($row->type == 'omics.dms.instrument') { $inst_id = intval($row->group_id); } else { if (strpos($row->type, 'Instrument.') >= 0) { $inst_id = intval(str_replace('Instrument.', '', $row->type)); } else { continue; } } $inst_group_lookup[$inst_id]['groups'][] = intval($row->group_id); } } $new_instruments_list = array(); foreach ($user_info['instruments'] as $eus_instrument_id => $inst_info) { $new_instruments_list[$eus_instrument_id] = $inst_info; if (array_key_exists($eus_instrument_id, $inst_group_lookup)) { $new_instruments_list[$eus_instrument_id]['groups'] = $inst_group_lookup[$eus_instrument_id]['groups']; } else { $new_instruments_list[$eus_instrument_id]['groups'] = array(); } } $user_info['instruments'] = $new_instruments_list; return $user_info; }