function test_parse_response_parses_all_responses() { $client = new CurlHttpClient(); //Warning - the line endings in this string must be \r\n line endings. $server_response = 'HTTP/1.1 401 Unauthorized Via: 1.1 DORY Connection: Keep-Alive Proxy-Support: Session-Based-Authentication Connection: Proxy-Support Content-Length: 12 Date: Wed, 03 Oct 2007 00:15:59 GMT Content-Type: text/plain; charset=UTF-8 WWW-Authenticate: Digest realm="bigfoot", domain="null", nonce="8Q84YxUBAABJP5W9FaNm7Fli2QGGO99o", algorithm=MD5, qop="auth" HTTP/1.1 200 OK Via: 1.1 DORY Connection: Keep-Alive Proxy-Connection: Keep-Alive Content-Length: 3 Date: Wed, 03 Oct 2007 00:15:59 GMT content-type: text/html; charset=UTF-8 Server: Bigfoot/5.282.18209 Cache-Control: max-age=7200, must-revalidate foo'; list($response_code, $response_headers, $response_body) = $client->parse_response($server_response); $this->assertEquals(200, $response_code); }
function test_send_request_and_get_response_concurrently() { $client = new CurlHttpClient(); $google_request = new HttpRequest('GET', 'http://www.google.com/'); $yahoo_request = new HttpRequest('GET', 'http://www.yahoo.com/'); $google_key = $client->send_request($google_request); $yahoo_key = $client->send_request($yahoo_request); $yahoo_response = $client->get_response_for($yahoo_key); $google_response = $client->get_response_for($google_key); $this->assertContains('Server: gws', $google_response); $this->assertContains('X-XRDS-Location: http://open.login.yahooapis.com/openid20/www.yahoo.com/xrds', $yahoo_response); }
function hit($keywords, $attempt = 0, $page_token = null) { $geocode_api_key = ''; //put your api key here $url = 'https://maps.googleapis.com/maps/api/place/textsearch/json'; $params = array("query" => $keywords, "key" => $geocode_api_key); if ($page_token) { $params['pagetoken'] = $page_token; } $url = $url . "?"; foreach ($params as $key => $value) { $url = $url . $key . '=' . urlencode($value) . '&'; } rtrim($url, '&'); if ($attempt > 0) { echo "\nattempt = {$attempt}\n"; } if ($attempt >= 5) { echo "\n\ngiving up on {$url}\n\n"; exit; } $ch = new CurlHttpClient(); $ch->fetch_url($url); $resp_raw = $ch->get_response(); echo "{$url} attempt = {$attempt}, resp_raw = {$resp_raw}\n"; $resp = json_decode($resp_raw, true); $status = $resp['status']; if ($status == 'OVER_QUERY_LIMIT') { //api query limit exceeded echo "over limit for {$keywords}"; exit; } if ($status == 'ZERO_RESULTS') { echo 'zero results'; exit; } if ($status != 'OK') { usleep(500000); return hit($keywords, $attempt + 1); } $page_token = isset($resp['next_page_token']) ? $resp['next_page_token'] : null; return array($resp['results'], $page_token); }
public function testCurlException() { $request = HttpRequest::create()->setUrl(HttpUrl::create()->parse('http://nonexistentdomain.xyz'))->setMethod(HttpMethod::get()); try { $response = CurlHttpClient::create()->setTimeout(3)->send($request); $this->fail(); } catch (NetworkException $e) { $this->assertContains('curl error', $e->getMessage()); } }
function getTeasersADNOW($request) { try { $id = '158750'; if (isset($request['id'])) { $id = $request['id']; } $request = array('Id' => $id, 'ajax' => 1, 'd_ip' => '215.12.56.100', 'uid' => '', 'cld' => '', 'unq' => isset($request['unq']) ? $request['unq'] : '', 'Referer_' => isset($request['Referer_']) ? base64_encode($request['Referer_']) : '', 'docurl_' => isset($request['docurl_']) ? base64_encode($request['docurl_']) : '', 'doc_inf' => isset($request['doc_inf']) ? base64_encode($request['doc_inf']) : '', 'sub_id' => 'aadblock'); $client = new CurlHttpClient(); $url = 'http://n.ads2-adnow.com/a?'; $data = $client->get($url . http_build_query($request)); $data = json_decode($data['body'], true); $out = 'error'; if (!isset($data['code']) || !isset($data['teasers'])) { throw new Exception('Error'); } $html = new teaserJson2Html(); $html->setJson($data)->setCode()->setTeasers(); $request['tbl'] = 'aaa'; if (!isset($request['tbl'])) { $out = $html->getAdaptiveTeaser(); } else { $out = $html->getNotAdaptiveTeaser(); } $res = array(); $res[$id] = $out; return json_encode($res); } catch (Exception $e) { return $e->getMessage(); } }
public function get($url) { $rand_array_index = rand(0, count(self::$userAgents) - 1); $user_agents = self::$userAgents[$rand_array_index]; $return = array(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLINFO_HEADER_OUT, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, $user_agents); curl_setopt($ch, CURLOPT_ENCODING, 'utf-8'); curl_setopt($ch, CURLOPT_TIMEOUT, 200); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $res = curl_exec($ch); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $return['http_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $return['header'] = substr($res, 0, $header_size); $return['body'] = substr($res, $header_size); return $return; } } $client = new CurlHttpClient(); $url = 'http://n.ads2-adnow.com/a?'; $ids = ['166085', '27780', '75057', '80145', '80146', '100839', '70054', '67297', '71917', '52744', '70051', '68283', '68282', '67292', '71841', '70052', '70047', '71918', '71431', '29058', '107043', '78998', '79001', '79002', '79003', '79005', '79006', '79007', '100991', '71840', '66799', '67289', '71792', '69573', '69572', '71839', '70060', '69872', '71844', '100985', '67288', '69871', '73001', '67304', '71916', '29252', '67283', '73432', '71835', '70041', '66481', '71915', '70064', '110516', '71922', '100647', '71923', '100648', '30446', '100681', '100677', '100674', '99952', '75814', '69886', '70039', '70036', '70045', '66830', '66500', '67294', '69371', '71789', '71849', '67302', '70037', '31280', '71931', '71802', '70040', '100854', '100684', '70044', '107201', '71920', '70043', '79817', '74563', '71836', '100539', '67308', '71836', '67308', '69491', '69492', '70035', '67293', '71837', '66497', '66610', '69875', '66794', '77969', '77973']; foreach ($ids as $v) { $data = $client->get($url . http_build_query($request)); $data = json_decode($data['body'], true); }
/** * @return CurlHttpClient */ private function spawnClient() { return CurlHttpClient::create()->setOldUrlConstructor(false)->setTimeout(5); }