Last active
May 14, 2023 12:12
-
-
Save EvilFreelancer/948c21b53865bb5e16fb3fc820a4a852 to your computer and use it in GitHub Desktop.
Асинхронная загрузка ссылок (кравлер) PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Promise; | |
// Создаем клиент Guzzle HTTP | |
$client = new Client(); | |
// Массив URL-адресов для запросов | |
$urls = [ | |
'https://api.example.com/users/1', | |
'https://api.example.com/users/2', | |
'https://api.example.com/users/3', | |
'https://api.example.com/users/4', | |
'https://api.example.com/users/5', | |
]; | |
// Создаем массив обещаний (promises) для каждого запроса | |
$promises = []; | |
foreach ($urls as $url) { | |
$promises[$url] = $client->getAsync($url); | |
} | |
// Параллельно отправляем все запросы | |
$results = Promise\unwrap($promises); | |
// Обрабатываем результаты запросов | |
foreach ($results as $url => $response) { | |
echo 'Response from ' . $url . ': ' . $response->getBody() . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment