Работает обработка ссылок на таблицы с численностью

This commit is contained in:
2024-09-06 14:11:38 +03:00
parent 04374fef40
commit 2be45826c1
1698 changed files with 138656 additions and 174 deletions

View File

@@ -1,6 +1,7 @@
<?php
namespace ContingentParser\Http;
use ContingentParser\Color;
use ContingentParser\Logger\HttpLogger;
use ContingentParser\Printer;
use CurlHandle;
@@ -12,16 +13,17 @@ final class CurlHelper
private CurlHandle|bool $curl;
private string $url;
private array $site;
private int $countRedirect;
private const MAX_REDIRECT = 5;
/**
* Коснтруктор
* Инициализация сессии
* @param string $url
* URL сайта
* @param array $site
* Идентификатор организации и базовый URL сайта
* @param string $url URL сайта
* @param array $site Идентификатор организации и базовый URL сайта
*/
public function __construct(string $url, array $site)
{
$this->countRedirect = 0;
$this->url = $url;
$this->site = $site;
@@ -49,21 +51,25 @@ final class CurlHelper
* Получить html-разметку
* @return string
*/
public function getContent() : string
public function getContent(): string
{
curl_setopt($this->curl, CURLOPT_URL, $this->url);
$html = curl_exec($this->curl);
if ($this->checkLocation($this->url, $html)) {
$html = $this->getContent();
if ($this->countRedirect < self::MAX_REDIRECT) {
curl_setopt($this->curl, CURLOPT_URL, $this->url);
$html = curl_exec($this->curl);
if ($this->checkLocation($this->url, $html)) {
$this->countRedirect++;
$html = $this->getContent();
}
return $html;
}
return $html;
return '';
}
/**
* Summary of checkLocation
* @param string $html
* @return bool
*/
private function checkLocation(string &$url, string $html) : bool
private function checkLocation(string &$url, string $html): bool
{
preg_match('/location:(.*?)\n/i', $html, $matches);
if (empty($matches)) return false;
@@ -77,14 +83,14 @@ final class CurlHelper
* Сообщить об ошибке
* @return void
*/
public function reportError() : void
public function reportError(): void
{
$httpLogger = new HttpLogger('log/http-curl.log');
$httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
if ($httpCode != 200 && $httpCode != 0) {
Printer::println("HTTP-code: $httpCode", 'red');
Printer::println("HTTP-code: $httpCode", Color::RED);
$message = implode(' ', $this->site) . ' HTTP-code(' . $httpCode.')';
$httpLogger->log($message, $httpCode);
} else if ($httpCode == 0) {
@@ -93,7 +99,7 @@ final class CurlHelper
$message .= " cURL error ({$errno}): ".curl_strerror($errno);
$httpLogger->log($message);
} else {
Printer::println("HTTP-code: $httpCode", 'blue');
Printer::println("HTTP-code: $httpCode", Color::BLUE);
}
}
}