示例#1
0
文件: Scraper.php 项目: pharman/Test
 /**
  * Scrape URL and collect output
  * @param string $url
  * @param OutputInterface $output
  */
 public function scrape($url, OutputInterface $output)
 {
     $this->collection->exchangeArray([]);
     try {
         $initialPage = $this->client->get($url)->send();
     } catch (BadResponseException $e) {
         $output->writeln('<error>' . $e->getMessage() . '</error>');
         throw new \RuntimeException('Unable to load initial page');
     }
     $xml = $this->getSimpleXml($initialPage->getBody());
     $items = $xml->xpath($this->config['collection']);
     foreach ($items as $item) {
         $title = $item->xpath($this->config['title']);
         $unitPrice = $item->xpath($this->config['unitPrice']);
         $itemUrl = $item->xpath($this->config['itemUrl']);
         $itemSize = 0;
         $itemDesc = null;
         if (isset($itemUrl[0]->attributes()['href'])) {
             try {
                 $itemPage = $this->client->get((string) $itemUrl[0]->attributes()['href'])->send();
                 $itemPageBody = $this->getSimpleXml($itemPage->getBody());
                 $itemSize = $itemPage->getContentLength();
                 $itemDesc = $itemPageBody->xpath($this->config['desc']);
             } catch (BadResponseException $e) {
                 $output->writeln('<error>' . $e->getMessage() . '</error>');
             }
         }
         if ($title && $unitPrice) {
             $parsedPrice = (double) \trim(\str_replace('&pound', null, $unitPrice[0]));
             $this->collection->append(['title' => \trim($title[0]), 'unit_price' => $parsedPrice, 'description' => \trim($itemDesc[0]), 'size' => \round($itemSize / 1024) . 'kb']);
         }
     }
     return $this->collection;
 }
示例#2
0
 /**
  * @param string $postalCode
  * @param string $houseNumber
  * @return array
  */
 public function lookupAddress($postalCode, $houseNumber)
 {
     $this->client->setConfig(['curl.options' => ['CURLOPT_CONNECTTIMEOUT_MS' => '2000', 'CURLOPT_RETURNTRANSFER' => true]]);
     $url = sprintf($this->baseUrl . '/%s/%s', $postalCode, $houseNumber);
     $request = $this->client->get($url)->setAuth($this->apiKey, $this->apiSecret);
     return $request->send()->json();
 }
示例#3
0
 /**
  * @return \DOMDocument
  */
 public function getDocument()
 {
     $request = $this->client->get($this->url);
     $this->response = $request->send();
     $this->setHtml($this->response->getBody(true));
     return parent::getDocument();
 }
 /**
  * Injects the livereload script.
  *
  * @param Response $response A Response instance
  */
 protected function injectScript(Response $response)
 {
     if (function_exists('mb_stripos')) {
         $posrFunction = 'mb_strripos';
         $substrFunction = 'mb_substr';
     } else {
         $posrFunction = 'strripos';
         $substrFunction = 'substr';
     }
     $content = $response->getContent();
     $pos = $posrFunction($content, '</body>');
     if (false !== $pos) {
         $script = "livereload.js";
         if ($this->checkServerPresence) {
             // GET is required, as livereload apparently does not support HEAD requests ...
             $request = $this->httpClient->get($script);
             try {
                 $checkResponse = $this->httpClient->send($request);
                 if ($checkResponse->getStatusCode() !== 200) {
                     return;
                 }
             } catch (CurlException $e) {
                 // If error is connection failed, we assume the server is not running
                 if ($e->getCurlHandle()->getErrorNo() === 7) {
                     return;
                 }
                 throw $e;
             }
         }
         $content = $substrFunction($content, 0, $pos) . "\n" . '<script src="' . $this->httpClient->getBaseUrl() . $script . '"></script>' . "\n" . $substrFunction($content, $pos);
         $response->setContent($content);
     }
 }
 /**
  * @inheritdoc
  */
 public function get($url)
 {
     $request = $this->client->get($url);
     $this->setHeaders($request);
     $response = $request->send();
     return json_decode($response->getBody(true));
 }
示例#6
0
 /**
  * parse
  *
  * @param  string $text
  *
  * @return array
  */
 public function parse($text)
 {
     $request = $this->client->get();
     $request->getQuery()->set('text', $text);
     $data = $request->send()->json();
     return $this->processSuggestions($text, $data);
 }
 public function authenticate(array $credentials)
 {
     $mcrypt = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
     $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($mcrypt), MCRYPT_DEV_RANDOM);
     mcrypt_generic_init($mcrypt, $this->cryptPassword, $iv);
     $url = $this->getUrl($credentials[self::USERNAME], $credentials[self::PASSWORD], $mcrypt, $iv);
     try {
         $res = $this->httpClient->get($url)->send();
     } catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
         if ($e->getResponse()->getStatusCode() === 403) {
             throw new \Nette\Security\AuthenticationException("User '{$credentials[self::USERNAME]}' not found.", self::INVALID_CREDENTIAL);
         } elseif ($e->getResponse()->getStatusCode() === 404) {
             throw new \Nette\Security\AuthenticationException("Invalid password.", self::IDENTITY_NOT_FOUND);
         } else {
             throw $e;
         }
     }
     $responseBody = trim(mdecrypt_generic($mcrypt, $res->getBody(TRUE)));
     $apiData = Json::decode($responseBody);
     $user = $this->db->table('users')->where('id = ?', $apiData->id)->fetch();
     $registered = new \DateTimeImmutable($apiData->registered->date, new \DateTimeZone($apiData->registered->timezone));
     $userData = array('username' => $credentials[self::USERNAME], 'password' => $this->calculateAddonsPortalPasswordHash($credentials[self::PASSWORD]), 'email' => $apiData->email, 'realname' => $apiData->realname, 'url' => $apiData->url, 'signature' => $apiData->signature, 'language' => $apiData->language, 'num_posts' => $apiData->num_posts, 'apiToken' => $apiData->apiToken, 'registered' => $registered->getTimestamp());
     if (!$user) {
         $userData['id'] = $apiData->id;
         $userData['group_id'] = 4;
         $this->db->table('users')->insert($userData);
         $user = $this->db->table('users')->where('username = ?', $credentials[self::USERNAME])->fetch();
     } else {
         $user->update($userData);
     }
     return $this->createIdentity($user);
 }
示例#8
0
 /**
  * Query product
  *
  * @param $productId
  * @return mixed
  */
 public function product($productId)
 {
     $query = ['apikey' => Config::get('providers.' . $this->provider)['apikey']];
     $url = $this->baseUrl . "products/{$productId}?" . http_build_query($query);
     $request = $this->client->get($url);
     $response = $request->send();
     return $response->json()['products'][0];
 }
 /**
  * @param string $uri
  * @param array $params
  * @param null $headers
  * @return array|null
  */
 public function get($uri, $params = [], $headers = null)
 {
     if (!empty($params)) {
         $uri .= '?' . http_build_query($params);
     }
     $response = $this->client->get($uri, $headers)->send();
     return $this->parseResponse($response);
 }
示例#10
0
 /**
  * @param string $file
  * @return string
  */
 public function getFileContent($file)
 {
     $request = $this->client->get('/exports/openstackcert/' . $file);
     $request->setAuth(COA_FILE_API_BASE_USER, COA_FILE_API_BASE_PASS);
     $response = $request->send();
     $content = (string) $response->getBody();
     return $content;
 }
示例#11
0
 public function getLinhas($termosBusca)
 {
     $request = $this->client->get('Linha/Buscar');
     $request->addCookie('apiCredentials', $this->apiCredentials);
     $request->getQuery()->set('termosBusca', $termosBusca);
     $response = $request->send();
     return $response->json();
 }
示例#12
0
 /**
  * Return the current users information 
  * 
  * @param  Array $authorization
  * @return Array
  */
 public function getUserDetails(array $authorization)
 {
     $endpoint = "people/me.json";
     $url = "{$authorization['accounts'][0]['href']}/{$endpoint}";
     $whoami = $this->client->get($url)->send()->json();
     $avatar = parse_url($whoami['avatar_url']);
     return ['name' => $whoami['name'], 'firstName' => $authorization['identity']['first_name'], 'lastName' => $authorization['identity']['last_name'], 'email' => $whoami['email_address'], 'avatar' => "//{$avatar['host']}/{$avatar['path']}?{$avatar['query']}"];
 }
 /**
  * @param string $method
  * @param array $context
  * @return \Guzzle\Http\Message\RequestInterface
  */
 protected function prepareRequest($method, $context = array())
 {
     $request = $this->client->get($method);
     $query = $request->getQuery();
     foreach ($context as $key => $value) {
         $query->set($key, $value);
     }
     return $request;
 }
 /**
  * @param string $uri
  * @param array $headers
  * @param array $options
  * @return array|\Guzzle\Http\Message\Response|null
  */
 public function get($uri, $headers = [], $options = [])
 {
     if (!$uri) {
         return null;
     }
     $this->request = $this->client->get($uri, $headers, $options);
     $this->response = $this->request->send();
     return $this->response;
 }
 /**
  * {@inheritdoc}
  */
 public function handle($workload)
 {
     /** @var PageDocument $document */
     $document = $this->documentManager->find($workload['uuid'], $workload['locale']);
     $urls = $this->webspaceManager->findUrlsByResourceLocator($document->getResourceSegment(), $this->environment, $workload['locale'], $workload['webspaceKey']);
     foreach ($urls as $url) {
         $this->client->get($url)->send();
     }
 }
示例#16
0
 /**
  * @When I send a GET request to :path
  */
 public function iSendAGetRequestTo($path)
 {
     $request = $this->guzzleClient->get('http://localhost:8081' . $path);
     try {
         $this->lastResponse = $this->guzzleClient->send($request);
     } catch (GuzzleResponseException $guzzleResponseException) {
         $this->lastResponse = $guzzleResponseException->getResponse();
     }
 }
 /**
  * @param $spaceId
  * @param array $attributes
  * @return array|\Guzzle\Http\Message\Response|null
  */
 public function getContentEntryBy($spaceId, $contentType, $attributes = [])
 {
     $path = sprintf(self::CONTENTFUL_ENTRIES_URL . "&content_type=%s", $spaceId, $this->accessToken, $contentType);
     foreach ($attributes as $attr => $value) {
         $path .= sprintf("&%s=%s", $attr, $value);
     }
     $request = $this->guzzleClient->get($path, [self::CONTENTFUL_HEADERS_V1_JSON]);
     $response = $this->guzzleClient->send($request);
     return $response;
 }
示例#18
0
 /**
  * Enumerate API endpoints
  *
  * @param string $baseUrl
  *
  * @return \Guzzle\Http\Message\Response
  */
 public function enumerate($baseUrl)
 {
     try {
         // Call directory endpoint
         $response = $this->guzzle->get($baseUrl . '/directory')->send();
     } catch (\Exception $e) {
         throw new ApiCallErrorException($e->getMessage(), 2);
     }
     return $this->processResponse($response);
 }
 /**
  * Import the culturefeed domains.
  */
 public function import()
 {
     $this->client = new Client($this::END_POINT);
     try {
         $body = $this->client->get('heading_categorisation.xml')->send()->getBody(TRUE);
         $this->importHeadings(new SimpleXMLElement($body));
     } catch (ClientErrorResponseException $e) {
         watchdog_exception('culturefeed_cnapi_import', $e);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function sendValidationRequest($uri)
 {
     try {
         $this->guzzleClient->setSslVerification($this->serverCaValidation);
         $request = $this->guzzleClient->get($uri);
         $response = $request->send();
         return (string) $response->getBody();
     } catch (RuntimeException $e) {
         throw new ValidationException("Validation request to CAS server failed with message: " . $e->getMessage());
     }
 }
 /**
  * Import the culturefeed domains.
  */
 public function import()
 {
     $this->client = new Client($this::END_POINT);
     try {
         // Cities.
         $body = $this->client->get('city')->send()->getBody(TRUE);
         $this->importCities(new SimpleXMLElement($body));
     } catch (ClientErrorResponseException $e) {
         watchdog_exception('culturefeed_city_import', $e);
     }
 }
示例#22
0
 public function testSend()
 {
     $request = $this->client->get('http://echo.jsontest.com/foo/bar');
     $this->assertEquals('new', $request->getState());
     $response = $request->send();
     $expectedType = 'application/json; charset=ISO-8859-1';
     $this->assertEquals($expectedType, $response->getContentType());
     $result = $response->json();
     $expectedResult = array('foo' => 'bar');
     $this->assertEquals($expectedResult, $result);
 }
示例#23
0
 protected function fetchUrl($url)
 {
     $response = $this->client->get($url)->send();
     if ($response->getStatusCode() !== 200) {
         throw new \Exception('Unable to fetch location data');
     }
     $response_data = json_decode($response->getBody(), true);
     if (!is_array($response_data)) {
         throw new \Exception('Ooops, we received invalid data :/');
     }
     return $response_data;
 }
 /**
  * Generates an app access token.
  * You can also use getSimpleAppToken
  *
  * @see FacebookGraphModel::getSimpleAppToken()
  * @link https://developers.facebook.com/docs/facebook-login/access-tokens/#apptokens
  *
  * @param \Facebook $facebook
  *
  * @return null
  */
 public function getAppToken(\Facebook $facebook)
 {
     $request = $this->client->get("/oauth/access_token");
     $request->getQuery()->set("client_id", $facebook->getAppId())->set("client_secret", $facebook->getAppSecret())->set("grant_type", "client_credentials");
     try {
         $response = $request->send();
         parse_str($response->getBody(true), $data);
         return $data;
     } catch (ClientErrorResponseException $e) {
         return null;
     }
 }
示例#25
0
 /**
  * Get data from a path
  * @param string $path - the path to be requested
  * @param array $headers - any additional headers you want to send on the request
  * @throws ErrorException
  * @return Response
  */
 public function get($path, array $headers = array())
 {
     $representation = $this->getRepresentationInstance();
     $headers['Accept'] = $representation->getContentType();
     $request = $this->transport->get($path, $headers);
     try {
         $response = $this->transport->send($request);
     } catch (BadResponseException $exception) {
         throw $this->handleErrorResponse($exception);
     }
     $representation = $representation::createFromString($response->getBody(true));
     return new Response($representation, $response);
 }
 public function getInfo()
 {
     try {
         $request = $this->client->get('/repos/' . $this->projectName);
         $response = $request->send();
         return json_decode($response->getBody(), true);
     } catch (ClientErrorResponseException $e) {
         if ($e->getResponse()->getStatusCode() !== 404) {
             throw $e;
         }
     }
     return null;
 }
示例#27
0
 /**
  * @param string $username
  *
  * @throws UserNotFoundException
  *
  * @return UserInterface
  */
 public function getUserByUsername($username)
 {
     $request = $this->httpClient->get('user?username='******'&expand=attributes');
     try {
         $response = $request->send();
     } catch (ClientErrorResponseException $exception) {
         if (404 === $exception->getResponse()->getStatusCode()) {
             throw new UserNotFoundException($exception->getResponse()->getBody());
         }
         throw $exception;
     }
     $userMapper = $this->userMapper;
     return $userMapper($response->json());
 }
示例#28
0
 /**
  * @param string $url
  * @param array  $headers
  * @param array  $options
  *
  * @return array|null
  * @throws \Exception
  */
 public function processQuery($url, $headers = array(), $options = array())
 {
     $request = $this->client->get($url, $headers, $options);
     /**@var Response */
     $response = $this->client->send($request);
     if ($response->isSuccessful()) {
         try {
             return $response->json();
         } catch (RuntimeException $cannotParseJsonException) {
             return $response->getBody(true);
         }
     } else {
         throw new \Exception("Fail to complete the request. Server returns code {$response->getStatusCode()}");
     }
 }
 protected function _fetchById($id, $options)
 {
     $settings = craft()->plugins->getPlugin('weather')->getSettings();
     try {
         $client = new Client('http://api.openweathermap.org');
         foreach ($options as $key => $value) {
             $client->setDefaultOption("query/{$key}", $value);
         }
         if (isset($settings->apiKey) && strlen($settings->apiKey) > 0) {
             $client->setDefaultOption('query/APPID', $settings->apiKey);
         }
         $client->setDefaultOption('query/id', $id);
         $request = $client->get('/data/2.5/weather', array('$e'));
         $response = $request->send();
     } catch (CurlException $e) {
         WeatherPlugin::log("Connection error to Open Weather Maps API", LogLevel::Error);
         return false;
     }
     if ($response->isSuccessful()) {
         $data = $response->json();
         if ($data['cod'] == 200) {
             return $data;
         } else {
             WeatherPlugin::log("Error: {$data['message']}", LogLevel::Error);
             return false;
         }
     }
 }
示例#30
0
 public function setLaundryState(&$laundryPlace)
 {
     $user = '******';
     $pass = '******';
     try {
         $client = new Client($laundryPlace['url']);
         $request = $client->get('/LaundryState', [], ['auth' => [$user, $pass, 'Digest'], 'timeout' => 1.5, 'connect_timeout' => 1.5]);
         $response = $request->send();
         $body = $response->getBody();
         libxml_use_internal_errors(true);
         $crawler = new Crawler();
         $crawler->addContent($body);
         foreach ($crawler->filter('img') as $img) {
             $resource = $img->getAttribute('src');
             $img->setAttribute('src', 'http://129.241.126.11/' . trim($resource, '/'));
         }
         $crawler->addHtmlContent('<h1>foobar</h1>');
         //'<link href="http://129.241.126.11/pic/public_n.css" type="text/css">');
         $laundryPlace['html'] = $crawler->html();
         libxml_use_internal_errors(false);
         preg_match_all('/bgColor=Green/', $body, $greenMatches);
         preg_match_all('/bgColor=Red/', $body, $redMatches);
         $laundryPlace['busy'] = count($redMatches[0]);
         $laundryPlace['available'] = count($greenMatches[0]);
     } catch (\Exception $e) {
         $laundryPlace['available'] = self::NETWORK_ERROR;
         $laundryPlace['busy'] = self::NETWORK_ERROR;
         $laundryPlace['html'] = self::NETWORK_ERROR;
     }
 }