Skip to content

Commit

Permalink
NGSTACK-843 use Symfony HttpClient instead of curl
Browse files Browse the repository at this point in the history
  • Loading branch information
Katarina Miočić committed Apr 22, 2024
1 parent c1548a8 commit 5be448f
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions lib/Core/Search/Common/PageTextExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,18 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RuntimeException;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use function count;
use function curl_close;
use function curl_error;
use function curl_exec;
use function curl_getinfo;
use function curl_init;
use function curl_setopt;
use function explode;
use function in_array;
use function is_string;
use function libxml_use_internal_errors;
use function mb_strlen;
use function mb_strpos;
use function mb_substr;
use function sprintf;
use function trim;
use const CURLINFO_HTTP_CODE;
use const CURLOPT_RETURNTRANSFER;
use const XML_ELEMENT_NODE;
use const XML_HTML_DOCUMENT_NODE;
use const XML_TEXT_NODE;
Expand Down Expand Up @@ -222,33 +214,27 @@ private function hasClass(DOMNode $node, string $className): bool
private function fetchPageSource(int $contentId, string $languageCode): string
{
$url = $this->generateUrl($languageCode, $contentId);
$curlHandle = curl_init($url);

if ($curlHandle === false) {
throw new RuntimeException('There was an error initializing a cURL session');
}

curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
$this->httpClient = HttpClient::create(
);

$html = curl_exec($curlHandle);
if (!is_string($html)) {
throw new RuntimeException('curl_exec could not fetch url');
}
$response = $this->httpClient->request(
'GET',
$url
);

$httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
$html = $response->getContent();

if ($httpCode !== 200) {
if ($response->getStatusCode() !== 200) {
throw new IndexPageUnavailableException(
sprintf(
'Could not fetch URL "%s": %s',
$url,
curl_error($curlHandle),
$response->getInfo()['error'],
),
);
}

curl_close($curlHandle);

return $html;
}

Expand Down

0 comments on commit 5be448f

Please sign in to comment.