How can PHP scripts be optimized to handle multiple HTTP requests for updating data from external sources efficiently?
To optimize PHP scripts for handling multiple HTTP requests for updating data from external sources efficiently, you can use asynchronous requests with a library like Guzzle. By sending multiple requests concurrently, you can reduce the overall processing time and improve the efficiency of your script.
```php
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Pool;
$client = new Client();
$requests = function () {
yield new Request('GET', 'http://example.com/api/data1');
yield new Request('GET', 'http://example.com/api/data2');
// Add more requests as needed
};
$pool = new Pool($client, $requests(), [
'concurrency' => 5, // Number of concurrent requests
'fulfilled' => function ($response, $index) {
// Handle successful responses
echo 'Request ' . $index . ' completed' . PHP_EOL;
},
'rejected' => function ($reason, $index) {
// Handle failed requests
echo 'Request ' . $index . ' failed: ' . $reason . PHP_EOL;
},
]);
$promise = $pool->promise();
$promise->wait();
```
This code snippet demonstrates how to use Guzzle to send multiple HTTP requests concurrently and handle the responses efficiently. You can adjust the concurrency level and add more requests as needed to optimize the script for updating data from external sources.
Keywords
Related Questions
- Are there any best practices or recommended functions in PHP to handle the correct encoding and transmission of non-Latin characters in form submissions?
- Are there alternative methods to securely output PHP code without using heredoc strings?
- What are common logic errors when trying to store query results in PHP variables?