PHP 8.3 Fibers for Concurrent API Calls
Introduction When your application needs to call multiple external APIs — like fetching trending videos from 7 different regions — doing it sequentially is slow. PHP 8.1 introduced Fibers, and PHP ...

Source: DEV Community
Introduction When your application needs to call multiple external APIs — like fetching trending videos from 7 different regions — doing it sequentially is slow. PHP 8.1 introduced Fibers, and PHP 8.3 made them production-ready. Here's how I use Fibers on ViralVidVault to fetch data from multiple YouTube API endpoints concurrently. The Problem: Sequential API Calls // Sequential: ~7 seconds for 7 regions (1s each) foreach (['US','GB','PL','NL','SE','NO','AT'] as $region) { $results[$region] = fetchTrending($region); // ~1 second each } // Total: 7 * 1s = ~7 seconds The Solution: Fibers + curl_multi Fibers don't make I/O faster by themselves — they provide cooperative multitasking. Combined with curl_multi, they let you run multiple HTTP requests in parallel: <?php class ConcurrentFetcher { private array $fibers = []; private \CurlMultiHandle $multiHandle; private array $handles = []; public function __construct() { $this->multiHandle = curl_multi_init(); } public function addRequ