Skip to content

Commit 4bc8ae0

Browse files
committed
Replace direct curl calls with Symfony HttpClient
- Add symfony/http-client dependency to composer.json - Update ScrapePartnersCommand to use HttpClientInterface via DI - Replace curl_* functions with HttpClient API - Improve testability and follow Symfony best practices Benefits: - Better error handling and exceptions - Easier to mock in tests - Consistent with Symfony ecosystem - Type safety with readonly properties
1 parent 66ac18e commit 4bc8ae0

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"doctrine/doctrine-migrations-bundle": "*",
5252
"knplabs/knp-paginator-bundle": "^6",
5353
"symfony/event-dispatcher": "^7",
54+
"symfony/http-client": "^7",
5455
"symfony/serializer": "^7",
5556
"symfony/uid": "^7",
5657
"symfony/yaml": "^7",

src/Bitrix24Partners/Console/ScrapePartnersCommand.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@
1010
use Symfony\Component\Console\Input\InputOption;
1111
use Symfony\Component\Console\Output\OutputInterface;
1212
use Symfony\Component\Console\Style\SymfonyStyle;
13+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1314

1415
#[AsCommand(
1516
name: 'bitrix24:partners:scrape',
1617
description: 'Scrape partners from bitrix24.ru/partners and generate CSV file'
1718
)]
1819
class ScrapePartnersCommand extends Command
1920
{
21+
public function __construct(
22+
private readonly HttpClientInterface $httpClient
23+
) {
24+
parent::__construct();
25+
}
26+
2027
#[\Override]
2128
protected function configure(): void
2229
{
@@ -76,22 +83,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7683

7784
private function fetchUrl(string $url): string
7885
{
79-
$ch = curl_init();
80-
curl_setopt($ch, CURLOPT_URL, $url);
81-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
82-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
83-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
84-
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');
85-
86-
$html = curl_exec($ch);
87-
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
88-
curl_close($ch);
89-
90-
if (false === $html || 200 !== $httpCode) {
91-
throw new \RuntimeException(sprintf('Failed to fetch URL: HTTP %d', $httpCode));
86+
$response = $this->httpClient->request('GET', $url, [
87+
'headers' => [
88+
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
89+
],
90+
'max_redirects' => 5,
91+
'verify_peer' => false,
92+
'verify_host' => false,
93+
]);
94+
95+
$statusCode = $response->getStatusCode();
96+
97+
if (200 !== $statusCode) {
98+
throw new \RuntimeException(sprintf('Failed to fetch URL: HTTP %d', $statusCode));
9299
}
93100

94-
return $html;
101+
return $response->getContent();
95102
}
96103

97104
/**

0 commit comments

Comments
 (0)