84 lines
2.5 KiB
PHP
84 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Http;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Exception;
|
|
|
|
class HttpFetcher
|
|
{
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public static function fetchHtml(string $url): string
|
|
{
|
|
try {
|
|
$response = Http::get($url);
|
|
|
|
if (!$response->successful()) {
|
|
throw new Exception("Failed to fetch URL: {$url} - Status: {$response->status()}");
|
|
}
|
|
|
|
return $response->body();
|
|
} catch (Exception $e) {
|
|
logger()->error('HTTP fetch failed', [
|
|
'url' => $url,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<int, string> $urls
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
public static function fetchMultipleUrls(array $urls): array
|
|
{
|
|
try {
|
|
$responses = Http::pool(function ($pool) use ($urls) {
|
|
foreach ($urls as $url) {
|
|
$pool->get($url);
|
|
}
|
|
});
|
|
|
|
return collect($responses)
|
|
->filter(fn($response, $index) => isset($urls[$index]))
|
|
->reject(fn($response, $index) => $response instanceof Exception)
|
|
->map(function ($response, $index) use ($urls) {
|
|
$url = $urls[$index];
|
|
|
|
try {
|
|
if ($response->successful()) {
|
|
return [
|
|
'url' => $url,
|
|
'html' => $response->body(),
|
|
'success' => true
|
|
];
|
|
} else {
|
|
return [
|
|
'url' => $url,
|
|
'html' => null,
|
|
'success' => false,
|
|
'status' => $response->status()
|
|
];
|
|
}
|
|
} catch (Exception) {
|
|
return [
|
|
'url' => $url,
|
|
'html' => null,
|
|
'success' => false,
|
|
'error' => 'Exception occurred'
|
|
];
|
|
}
|
|
})
|
|
->filter(fn($result) => $result !== null)
|
|
->toArray();
|
|
} catch (Exception $e) {
|
|
logger()->error('Multiple URL fetch failed', ['error' => $e->getMessage()]);
|
|
|
|
return [];
|
|
}
|
|
}
|
|
}
|