Exemplo n.º 1
0
 /**
  * осуществляет поиск по yandex-у с указанными параметрами
  * @param array $params массив параметров
  * @throws Exception
  * @return array массив полученных результатов
  */
 public function search($params = [])
 {
     Browser::configure(['options' => [CURLOPT_TIMEOUT => $this->_config['timeout']], 'proxy' => false], ['return' => 'content', 'exception' => true, 'direct' => true, 'retry' => false, 'cache' => true]);
     try {
         try {
             Browser::mix('Xml');
             $responce = Browser::get($this->_processSearchData($params));
         } catch (\Exception $e) {
             throw $e;
         } finally {
             Browser::unmix('Xml');
         }
         if (!empty($responce['yandexsearch']['response']['error']) && $responce['yandexsearch']['response']['error']['@code'] != Exception::EMPTY_RESULT && $responce['yandexsearch']['response']['error']['@code'] != Exception::EMPTY_QUERY) {
             throw new Exception($responce['yandexsearch']['response']['error']['@'], $responce['yandexsearch']['response']['error']['@code']);
         }
         $docs = [];
         if (!empty($responce['yandexsearch']['response']['results'])) {
             $groups = !empty($responce['yandexsearch']['response']['results']['grouping']['group'][0]) ? $responce['yandexsearch']['response']['results']['grouping']['group'] : array($responce['yandexsearch']['response']['results']['grouping']['group']);
             foreach ($groups as $doc) {
                 $docs[] = (object) ['url' => $doc['doc']['url']];
             }
         }
         $result = ['total_pages' => !empty($responce['yandexsearch']['response']['results']['grouping']['found-docs'][0]['@']) ? $responce['yandexsearch']['response']['results']['grouping']['found-docs'][0]['@'] : 0, 'results' => $docs];
         return $result;
     } catch (\Exception $e) {
         throw new Exception('Не удалось выполнить поиск: ' . $e->getMessage() . ' -> ' . $this->_processSearchData($params));
     }
 }
Exemplo n.º 2
0
 /**
  * Загружаем robots txt
  *
  * @param string $url домен или url для загрузки
  * @return string $content содержимое robots
  */
 protected function _request($url, $agent)
 {
     Browser::pack();
     Browser::configure(['agent' => $agent], ['direct' => true, 'exception' => true]);
     $content = Browser::get(Uri::getHost($url) . '/robots.txt');
     Browser::unpack();
     return $content;
 }
Exemplo n.º 3
0
 /**
  * Получаем хэдер страницы, код ответа подменяется - либо 200, либо 404, все остальные -1
  *
  * @param string $page url страницы
  * @return array хэдер
  */
 public static function getAnswerHeader($page)
 {
     $_this = self::getInstance();
     // пытаемся считать из кэша
     if (is_object($_this->_Cache)) {
         try {
             $info = $_this->_Cache->readCache($page);
             if (!empty($info)) {
                 return $info;
             }
         } catch (\Exception $e) {
         }
     }
     $info = array();
     try {
         Browser::configure(['proxy' => 'random', 'options' => [CURLOPT_NOBODY => true, CURLOPT_FOLLOWLOCATION => true], 'agent' => $_this->_config['agent']], ['retry' => false, 'exception' => false, 'return' => 'headers']);
         // Получаем страницу
         $info = Browser::get($page);
         if (!in_array($info['http_code'], array(self::STATUS_OK, self::STATUS_NOT_FOUND))) {
             $info['http_code'] = self::STATUS_FAIL;
         }
     } catch (Exception $e) {
         // Если браузер свалился - то код все упало
         $info['http_code'] = self::STATUS_FAIL;
     }
     if (is_object($_this->_Cache)) {
         // пишем всё это в кэш
         try {
             $_this->_Cache->writeCache($page, $info);
         } catch (\Exception $e) {
         }
     }
     return $info;
 }