Skip to content

Instantly share code, notes, and snippets.

@EvilFreelancer
Last active May 14, 2023 12:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvilFreelancer/948c21b53865bb5e16fb3fc820a4a852 to your computer and use it in GitHub Desktop.
Save EvilFreelancer/948c21b53865bb5e16fb3fc820a4a852 to your computer and use it in GitHub Desktop.
Асинхронная загрузка ссылок (кравлер) PHP
<?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