/** * GET Request * * @param $url * @param $datas * @return string */ public function get_request($url, $datas = array()) { $body = ''; try { $url2 = new Net_URL2($url); foreach ($datas as $key => $val) { $url2->setQueryVariable($key, mb_convert_encoding($val, $this->response_encoding, 'UTF-8'), true); } $this->http->setURL($url2); $this->http->setMethod(HTTP_Request2::METHOD_GET); if (!empty($this->cookies)) { foreach ($this->cookies as $cookie) { $this->http->addCookie($cookie['name'], $cookie['value']); } } $response = $this->http->send(); if (count($response->getCookies())) { $this->cookies = $response->getCookies(); } $body = mb_convert_encoding($response->getBody(), 'UTF-8', $this->response_encoding); } catch (Exception $e) { debug($e->getMessage()); } return $body; }
private function __construct() { // sanity check $siteUrl = get_option('siteurl'); $layoutUrl = DAIQUIRI_URL . '/core/layout/'; if (strpos($layoutUrl, $siteUrl) !== false) { echo '<h1>Error with theme</h1><p>Layout URL is below CMS URL.</p>'; die(0); } // construct request require_once 'HTTP/Request2.php'; $req = new HTTP_Request2($layoutUrl); $req->setConfig(array('ssl_verify_peer' => false, 'connect_timeout' => 2, 'timeout' => 3)); $req->setMethod('GET'); $req->addCookie("PHPSESSID", $_COOKIE["PHPSESSID"]); try { $response = $req->send(); if (200 != $response->getStatus()) { echo '<h1>Error with theme</h1><p>HTTP request status != 200.</p>'; die(0); } } catch (HTTP_Request2_Exception $e) { echo '<h1>Error with theme</h1><p>Error with HTTP request.</p>'; die(0); } $body = explode('<!-- content -->', $response->getBody()); if (count($body) == 2) { $this->_header = $body[0]; $this->_footer = $body[1]; } else { echo '<h1>Error with theme</h1><p>Malformatted layout.</p>'; die(0); } }
public function testCookies() { $cookies = array('CUSTOMER' => 'WILE_E_COYOTE', 'PART_NUMBER' => 'ROCKET_LAUNCHER_0001'); foreach ($cookies as $k => $v) { $this->request->addCookie($k, $v); } $response = $this->request->send(); $this->assertEquals(serialize($cookies), $response->getBody()); }
public function testCookies() { $req = new HTTP_Request2(); $req->addCookie('name', 'value'); $req->addCookie('foo', 'bar'); $headers = $req->getHeaders(); $this->assertEquals('name=value; foo=bar', $headers['cookie']); try { $req->addCookie('invalid cookie', 'value'); } catch (HTTP_Request2_Exception $e) { return; } $this->fail('Expected HTTP_Request2_Exception was not thrown'); }
/** * Fetches a json response via HTTP request * @todo Support cookies (to allow login and similiar features) * @param string $url URL to get json from * @param array $data (optional) HTTP post data * @param boolean $secure (optional) Wheter to verify peer using SSL or not, default false * @param string $certificateFile (optional) Local certificate file for non public SSL certificates * @param array Set an optional HTTP Authentication method and supply its login credentials. * The supplied array must comply with the following structure: * <pre class="brush: php"> * $httpAuth = array( * 'httpAuthMethod' => 'none|basic|disgest', * 'httpAuthUsername' => '<username>', * 'httpAuthPassword' => '<password>', * ); * </pre> * @return mixed Decoded JSON on success, false otherwise */ public function getJson($url, $data = array(), $secure = false, $certificateFile = '', $httpAuth = array(), $files = array()) { $request = new \HTTP_Request2($url, \HTTP_Request2::METHOD_POST); if (!empty($httpAuth)) { switch ($httpAuth['httpAuthMethod']) { case 'basic': $request->setAuth($httpAuth['httpAuthUsername'], $httpAuth['httpAuthPassword'], \HTTP_Request2::AUTH_BASIC); break; case 'disgest': $request->setAuth($httpAuth['httpAuthUsername'], $httpAuth['httpAuthPassword'], \HTTP_Request2::AUTH_DIGEST); break; case 'none': default: break; } } foreach ($data as $name => $value) { $request->addPostParameter($name, $value); } if (!empty($files)) { foreach ($files as $fieldId => $file) { $request->addUpload($fieldId, $file); } } if ($this->sessionId !== null) { $request->addCookie(session_name(), $this->sessionId); } $request->setConfig(array('ssl_verify_host' => false, 'ssl_verify_peer' => false, 'follow_redirects' => true, 'strict_redirects' => true)); $response = $request->send(); //echo '<pre>';var_dump($response->getBody());echo '<br /><br />'; $cookies = $response->getCookies(); foreach ($cookies as &$cookie) { if ($cookie['name'] === session_name()) { $this->sessionId = $cookie['value']; break; } } if ($response->getStatus() != 200) { \DBG::msg(__METHOD__ . ' Request failed! Status: ' . $response->getStatus()); \DBG::msg('URL: ' . $url); \DBG::dump($data); return false; } $body = json_decode($response->getBody()); if ($body === NULL) { \DBG::msg(__METHOD__ . ' failed!'); \DBG::dump($response->getBody()); } return $body; }
/** * Make a low-level web hit to this site, with authentication. * @param string $path URL fragment for something under the base path * @param array $params POST parameters to send * @param boolean $auth whether to include auth data * @return string * @throws Exception on low-level error conditions */ protected function hit($path, $params = array(), $auth = false, $cookies = array()) { $url = $this->basepath . '/' . $path; $http = new HTTP_Request2($url, 'POST'); if ($auth) { $http->setAuth($this->username, $this->password, HTTP_Request2::AUTH_BASIC); } foreach ($cookies as $name => $val) { $http->addCookie($name, $val); } $http->addPostParameter($params); $response = $http->send(); $code = $response->getStatus(); if ($code < '200' || $code >= '400') { throw new Exception("Failed API hit to {$url}: {$code}\n" . $response->getBody()); } return $response; }
function daiquiri_auto_login() { if (!is_user_logged_in()) { // check which user is logged in into daiquiri right now $userUrl = DAIQUIRI_URL . '/auth/account/show/'; require_once 'HTTP/Request2.php'; $req = new HTTP_Request2($userUrl); $req->setConfig(array('ssl_verify_peer' => false, 'connect_timeout' => 2, 'timeout' => 3)); $req->setMethod('GET'); $req->addCookie("PHPSESSID", $_COOKIE["PHPSESSID"]); $req->setHeader('Accept: application/json'); try { $response = $req->send(); $status = $response->getStatus(); $body = $response->getBody(); } catch (HTTP_Request2_Exception $e) { echo '<h1>Error with daiquiri auth</h1><p>Error with HTTP request.</p>'; die(0); } if ($status == 403) { if (is_user_logged_in()) { wp_clear_auth_cookie(); wp_redirect($_SERVER['REQUEST_URI']); exit; } } else { if ($status == 200) { // decode the non empty json to the remote user array $remoteUser = json_decode($response->getBody()); $daiquiriUser = array(); foreach (array('id', 'username', 'email', 'role') as $key) { if (isset($remoteUser->row->{$key})) { $daiquiriUser[$key] = $remoteUser->row->{$key}; } } foreach (array('firstname', 'lastname', 'website') as $key) { if (isset($remoteUser->row->details->{$key})) { $daiquiriUser[$key] = $remoteUser->row->details->{$key}; } } // create/update the wordpress user to match the daiquiri user // the id in daiquiri maps to the user_login in wp // the username in daiquiri maps to the user_nicename in wp $wpUser = array('user_login' => $daiquiriUser['id'], 'user_nicename' => $daiquiriUser['username'], 'user_pass' => 'foo', 'user_email' => $daiquiriUser['email']); // get the role of the user if ($daiquiriUser['role'] === 'admin') { $wpUser['role'] = 'administrator'; } else { if ($daiquiriUser['role'] === 'manager') { $wpUser['role'] = 'editor'; } else { if (defined('DAIQUIRI_AUTHOR_ROLE') && $daiquiriUser['role'] === DAIQUIRI_AUTHOR_ROLE) { $wpUser['role'] = 'author'; } else { if (defined('DAIQUIRI_CONTRIBUTOR_ROLE') && $daiquiriUser['role'] === DAIQUIRI_CONTRIBUTOR_ROLE) { $wpUser['role'] = 'contributor'; } else { $wpUser['role'] = 'subscriber'; } } } } // get the name and the other credentials if (isset($daiquiriUser['firstname'])) { $wpUser['first_name'] = $daiquiriUser['firstname']; } if (isset($daiquiriUser['lastname'])) { $wpUser['last_name'] = $daiquiriUser['lastname']; } if (isset($daiquiriUser['website'])) { $wpUser['user_url'] = $daiquiriUser['website']; } if (isset($wpUser['first_name']) && isset($wpUser['last_name'])) { $wpUser['display_name'] = $wpUser['first_name'] . ' ' . $wpUser['last_name']; } // update or create the user in the wordpress db $storedUser = get_user_by('login', $wpUser['user_login']); if ($storedUser === false) { // create a new user in the wordpress db $status = wp_insert_user($wpUser); } else { // update the user in the wordpress database $wpUser['ID'] = $storedUser->ID; $status = wp_update_user($wpUser); } if (is_int($status)) { $userId = $status; } else { echo '<h1>Error with auth</h1>'; var_dump($status); exit; } // log in the newly created or updated user $user = get_userdata($userId); wp_set_current_user($user->ID, $user->user_login); wp_set_auth_cookie($user->ID); do_action('wp_login', $user->user_login); } else { echo '<h1>Error with auth</h1><p>HTTP request status != 200.</p>'; die(0); } } } }
/** * Re-send a request after successful re-authentication * Re-creates a GET or POST request based on data passed along in a form. Used * in case of an expired security token so that the user doesn't lose changes. */ function resend_request() { global $_CONF; $method = ''; if (isset($_POST['token_requestmethod'])) { $method = COM_applyFilter($_POST['token_requestmethod']); } $returnUrl = ''; if (isset($_POST['token_returnurl'])) { $returnUrl = urldecode($_POST['token_returnurl']); if (substr($returnUrl, 0, strlen($_CONF['site_url'])) != $_CONF['site_url']) { // only accept URLs on our site $returnUrl = ''; } } $postData = ''; if (isset($_POST['token_postdata'])) { $postData = urldecode($_POST['token_postdata']); } $getData = ''; if (isset($_POST['token_getdata'])) { $getData = urldecode($_POST['token_getdata']); } $files = ''; if (isset($_POST['token_files'])) { $files = urldecode($_POST['token_files']); } if (SECINT_checkToken() && !empty($method) && !empty($returnUrl) && ($method === 'POST' && !empty($postData) || $method === 'GET' && !empty($getData))) { $magic = get_magic_quotes_gpc(); if ($method === 'POST') { $req = new HTTP_Request2($returnUrl, HTTP_Request2::METHOD_POST); $data = unserialize($postData); foreach ($data as $key => $value) { if ($key == CSRF_TOKEN) { $req->addPostParameter($key, SEC_createToken()); } else { if ($magic) { $value = stripslashes_gpc_recursive($value); } $req->addPostParameter($key, $value); } } if (!empty($files)) { $files = unserialize($files); } if (!empty($files)) { foreach ($files as $key => $value) { $req->addPostParameter('_files_' . $key, $value); } } } else { $data = unserialize($getData); foreach ($data as $key => &$value) { if ($key == CSRF_TOKEN) { $value = SEC_createToken(); } else { if ($magic) { $value = stripslashes_gpc_recursive($value); } } } $returnUrl = $returnUrl . '?' . http_build_query($data); $req = new HTTP_Request2($returnUrl, HTTP_Request2::METHOD_GET); } $req->setHeader('User-Agent', 'Geeklog/' . VERSION); // need to fake the referrer so the new token matches $req->setHeader('Referer', COM_getCurrentUrl()); foreach ($_COOKIE as $cookie => $value) { $req->addCookie($cookie, $value); } try { $response = $req->send(); $status = $response->getStatus(); if ($status == 200) { COM_output($response->getBody()); } else { throw new HTTP_Request2_Exception('HTTP error: status code = ' . $status); } } catch (HTTP_Request2_Exception $e) { if (!empty($files)) { SECINT_cleanupFiles($files); } trigger_error("Resending {$method} request failed: " . $e->getMessage()); } } else { if (!empty($files)) { SECINT_cleanupFiles($files); } COM_redirect($_CONF['site_url'] . '/index.php'); } // don't return exit; }
private function _scrape_page($url) { $this->proxies = getProxyIPS(TRUE, 500); require_once 'HTTP/Request2.php'; if (count($this->proxies) > 0) { foreach ($this->proxies as $index => $proxy) { $now = time(); $totalAttempts = $proxy->fails + $proxy->connects; $failureRate = $totalAttempts == 0 ? 0 : $proxy->fails / $totalAttempts * 100; $gracePeriod = $now - strtotime($proxy->last_warn_time); $scheme = 'http'; switch ($proxy->scheme) { case 'https': case 'HTTPS': //Request2 only takes http, but proxy should still work $scheme = 'http'; break; case 'socks4': case 'socks5': $scheme = 'socks5'; break; } $proxyString = $scheme . '://' . $proxy->proxy_host . ":" . $proxy->proxy_port; //we don't want to mess with the ones that have too many warnings //let's try 25% failure/connect ratio should exclude this proxy temporarily if ($failureRate > 25 && $this->warn_period > $gracePeriod) { log_message('error', "Skipping {$proxyString} due to {$failureRate}% failure rate.\nTime calc: {$gracePeriod} period: {$this->warn_period}"); continue; } try { $options = array("timeout" => "5", "follow_redirects" => true, "max_redirects" => 3); $scrape = new HTTP_Request2($url, HTTP_Request2::METHOD_GET, $options); $scrape->setAdapter('curl'); $scrape->setConfig(array('proxy_host' => trim($proxy->proxy_host), 'proxy_port' => trim($proxy->proxy_port))); $aKey = array_rand($this->agents); $scrape->setHeader(array('Accept-Charset' => 'utf-8', 'User-Agent' => $this->agents[$aKey])); // send http request $response = $scrape->send(); $body = $response->getBody(); $status = $response->getStatus(); $source = parse_url($url); $upData = array(); //echo "From: $proxyString\nResponse status: ".$response->getStatus()."\n"; $this->CI->db->where("id", $proxy->id); $this->CI->db->set('connects', 'connects+1', FALSE); $this->CI->db->update($this->_table_proxy_ips); $updateFlag = $html = false; $title = ''; if ($status == '200' || $status == '304') { $body = preg_replace('/[^[:print:]]/', '', $body); $html = str_get_html($body); if (!$html) { $headerLog = ''; foreach ($response->getHeader() as $k => $v) { $headerLog .= "\t{$k}: {$v}\n"; } log_message('error', "!method_exists\n" . $response->getStatus() . "\ntitle: {$title}\nheaders: {$headerLog}\n{$proxyString}\n{$url}"); } else { $title = $html->find('title', 0); $title = $title ? strtolower($title->plaintext) : ''; $html->clear(); unset($html); //echo "got: $url\ntitle: $title\n"; } } //find any known phantom sites if (strpos($title, 'onlinecollegesuniversity.com') || strpos($title, 'articlesdigest.info') || strpos($title, 'ihowandwhy.com')) { $updateFlag = true; if ((int) $proxy->warns >= $this->warn_max * 2) { log_message('error', "Ban status Phantom {$title}:\n{$proxyString}\n{$url}"); $upData = array('use_flag' => 0, 'ban_source' => $source['host'] . ' - ' . $title, 'ban_type' => $status, 'ban_agent' => $this->agents[$aKey]); unset($this->proxies[$index]); } else { log_message('error', "Phantom site {$title}:\n{$proxyString}\n{$url}"); } } elseif ($status >= 500) { // Server Error -- assume this is ban $updateFlag = true; log_message('error', "Ban status {$status}:\n{$proxyString}\n{$url}"); $upData = array('use_flag' => 0, 'ban_source' => $source['host'], 'ban_type' => $status, 'ban_agent' => $this->agents[$aKey]); unset($this->proxies[$index]); } elseif ($status == 404) { $updateFlag = true; if ((int) $proxy->warns >= $this->warn_max * 2) { log_message('error', "Ban status {$status}:\n{$proxyString}\n{$url}"); $upData = array('use_flag' => 0, 'ban_source' => $source['host'], 'ban_type' => $status, 'ban_agent' => $this->agents[$aKey]); } else { log_message('error', "Warning {$status}:\n{$proxyString}\n{$url}"); } } elseif ($status >= 400) { $updateFlag = true; if ((int) $proxy->warns >= $this->warn_max) { log_message('error', "Ban status {$status}:\n{$proxyString}\n{$url}"); $upData = array('use_flag' => 0, 'ban_source' => $source['host'], 'ban_type' => $status, 'ban_agent' => $this->agents[$aKey]); } else { log_message('error', "Warning {$status}:\n{$proxyString}\n{$url}"); } } if ($updateFlag) { $this->CI->db->set('warns', 'warns+1', FALSE); $this->CI->db->set('last_warn_time', 'now()', FALSE); $this->CI->db->where("id", $proxy->id); $this->CI->db->update($this->_table_proxy_ips, $upData); } foreach ($response->getCookies() as $c) { /* echo "\tname: {$c['name']}, value: {$c['value']}".(empty($c['expires'])? '': ", expires: {$c['expires']}").(empty($c['domain'])? '': ", domain: {$c['domain']}").(empty($c['path'])? '': ", path: {$c['path']}").", secure: ".($c['secure']? 'yes': 'no')."\n";*/ $scrape->addCookie($c['name'], $c['value']); } unset($scrape); return $status == '200' || $status == '304' ? $body : false; } catch (HTTP_Request2_Exception $e) { //do proxy deactivation here... //once we have a good sample & connection failure is > 75% - kill proxy if ((int) $proxy->fails > 10 && ($failureRate > 75 || (int) $proxy->connects == 0)) { log_message('error', "Connection Ban status {$e->getNativeCode()}:\n{$proxyString}\n{$url}\n" . $e->getMessage() . "\nFails: {$proxy->fails} - {$failureRate}%"); $this->CI->db->where('id', $proxy->id); $this->CI->db->set('fails', 'fails+1', FALSE); $this->CI->db->set('last_warn_time', 'now()', FALSE); $this->CI->db->update($this->_table_proxy_ips, array('use_flag' => 0, 'ban_source' => $proxy->proxy_host . ':' . $proxy->proxy_port, 'ban_type' => "Connection: " . $e->getNativeCode(), 'ban_agent' => $this->agents[$aKey])); unset($this->proxies[$index]); } else { log_message('error', "Connection Warning {$e->getNativeCode()}:\n{$proxyString}\nFails: {$proxy->fails} rate: {$failureRate}\n{$url}"); $this->CI->db->where('id', $proxy->id); $this->CI->db->set('fails', 'fails+1', FALSE); $this->CI->db->set('last_warn_time', 'now()', FALSE); $this->CI->db->update($this->_table_proxy_ips); } unset($scrape); //return false; } } } else { log_message('error', 'We are out of proxies'); email_alertToTeam('amzecs _scrape_page() error', 'We are out of proxies'); } log_message('error', "amzecs _scrape_page() error - Neither success or failure\n{$proxyString}"); email_alertToTeam('amzecs _scrape_page() error', "Neither success or failure\n{$proxyString}"); return false; }
echo 'Uploads: ' . PHP_EOL; print_r($uploads); $HttpRequest = new HTTP_Request2(); $HttpRequest->setHeader(array('Accept-Language' => $language, 'User-Agent' => $userAgent, 'X-Image-Scale' => $imageScale)); $HttpRequest->setUrl($serverAdder); // 65秒待つ $HttpRequest->setConfig(array('timeout' => 65, 'adapter' => 'HTTP_Request2_Adapter_Curl', 'ssl_verify_peer' => FALSE, 'ssl_verify_host' => FALSE)); eval('$HttpRequest->setMethod(HTTP_Request2::METHOD_' . $method . ');'); if (count($posts) > 0) { foreach ($posts as $keysKey => $keysVal) { $HttpRequest->addPostParameter($keysKey, $keysVal); } } if (count($cookies) > 0) { foreach ($cookies as $keysKey => $keysVal) { $HttpRequest->addCookie($keysKey, $keysVal); } } if (count($uploads) > 0) { for ($uploadCnt = 0; count($uploads) > $uploadCnt; $uploadCnt++) { $HttpRequest->addUpload($uploads[$uploadCnt]['formname'], $uploads[$uploadCnt]['filepath'], $uploads[$uploadCnt]['filename']); } } echo PHP_EOL; echo '【Response】' . PHP_EOL; $Response = $HttpRequest->send(); //print_r($HttpRequest); $statusCode = $Response->getStatus(); if (200 == $statusCode) { echo 'status code: ' . $Response->getStatus() . PHP_EOL; foreach ($Response->getHeader() as $key => $value) {
public function testAddCookieToJar() { $req = new HTTP_Request2(); $req->setCookieJar(); try { $req->addCookie('foo', 'bar'); $this->fail('Expected HTTP_Request2_Exception was not thrown'); } catch (HTTP_Request2_LogicException $e) { } $req->setUrl('http://example.com/path/file.php'); $req->addCookie('foo', 'bar'); $this->assertArrayNotHasKey('cookie', $req->getHeaders()); $cookies = $req->getCookieJar()->getAll(); $this->assertEquals(array('name' => 'foo', 'value' => 'bar', 'domain' => 'example.com', 'path' => '/path/', 'expires' => null, 'secure' => false), $cookies[0]); }
function do_sendemail() { $req = new HTTP_Request2($this->eYou_bot_url_send_, HTTP_Request2::METHOD_POST); $req->setHeader("User-Agent", $this->req_emu_agent_); $req->setHeader("Accept", $this->req_emu_accept_); $req->setHeader("Accept-Language", $this->req_emu_accept_language_); $req->setHeader("Accept-Encoding", $this->req_emu_accept_encoding_); $req->setHeader("Connection", $this->req_emu_connection_); $req->setHeader("Content-Type", $this->req_emu_content_type_); $req->setHeader("Referer", $this->eYou_bot_referer_send_); $req->addCookie("Cookie", $this->eYou_bot_cookie_string_); $req->addPostParameter("Msg", ""); $req->addPostParameter("faint", $this->eYou_CNchar_urlencoded_send_immediately_); $req->addPostParameter("AttFiles", ""); $req->addPostParameter("html", "yes"); $req->addPostParameter("eyou_ctype", ""); $req->addPostParameter("content", $this->content_); $req->addPostParameter("recipient", $this->recipient_); $req->addPostParameter("subject", $this->subject_); $req->addPostParameter("ccopy", ""); $req->addPostParameter("bccopy", ""); $req->addPostParameter("priority", 3); $req->addPostParameter("alter", ""); $req->addPostParameter("editcontent", $this->content_); $req->addPostParameter("signature", "no_signum"); $response = $req->send(); if (strstr($response->getBody(), 'ABOUT_THIS_SUCCESS')) { return 0; } else { return 2; } /* echo "------------------------------------"; echo $response->getBody(); echo "------------------------------------"; */ }
<?php require 'HTTP/Request2.php'; $r = new HTTP_Request2('http://www.example.com/needs-cookies.php'); $r->addCookie('user', 'ellen'); $r->addCookie('activity', 'swimming'); $page = $r->send()->getBody(); echo $page;