Efficiency improvement: Sharing development skills for PHP asynchronous HTTP downloading of multiple files
In modern Internet applications, file downloading is a very common requirement. However, when a large number of files need to be downloaded, the traditional synchronous download method is often inefficient, causing users to wait for a long time and reducing the user experience. This article will introduce a development technique for using PHP asynchronous HTTP to download multiple files to improve download efficiency and user experience.
$urls = array( 'http://example.com/file1.zip', 'http://example.com/file2.zip', 'http://example.com/file3.zip', // 添加更多文件链接 ); $mh = curl_multi_init(); $handles = array(); foreach ($urls as $url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_multi_add_handle($mh, $ch); $handles[] = $ch; } $running = null; do { curl_multi_exec($mh, $running); } while ($running > 0); foreach ($handles as $ch) { curl_multi_remove_handle($mh, $ch); curl_close($ch); } curl_multi_close($mh);
The above code first defines the file link that needs to be downloaded, and then initializes a cURL multiprocessing handle. Loop through the file links, creating a cURL handle for each link and adding it to the multiprocessing handle. Subsequently, all requests are executed by calling the curl_multi_exec()
function until no requests are running. Finally, clean up resources and close cURL handles and multiprocessing handles.
curl_multi_info_read()
function. The following is a sample code: $completed = 0; do { $mh = curl_multi_init(); $handles = array(); foreach ($urls as $url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_multi_add_handle($mh, $ch); $handles[] = $ch; } $running = null; do { curl_multi_exec($mh, $running); $info = curl_multi_info_read($mh); if ($info !== false) { // 处理下载结果 $completed++; $handle = $info['handle']; // ... } } while ($running > 0); foreach ($handles as $ch) { curl_multi_remove_handle($mh, $ch); curl_close($ch); } curl_multi_close($mh); } while ($completed < count($urls));
In the above code, after each asynchronous HTTP request is executed, the curl_multi_info_read()
function is used to check whether any request has been completed. If there is a completed request, the relevant download result information can be obtained from the returned information and processed accordingly. By recording the number of completed requests, you can determine whether all requests have been completed.
foreach ($urls as $url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 添加错误处理 curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_multi_add_handle($mh, $ch); $handles[] = $ch; }
In the above code, by setting the CURLOPT_FAILONERROR
option to true, you can throw an exception when the request fails. At the same time, by setting reasonable CURLOPT_CONNECTTIMEOUT
and CURLOPT_TIMEOUT
options, you can set the connection and request timeout to avoid download failures caused by timeout.
Summary
The technique of using PHP asynchronous HTTP to download multiple files can greatly improve download efficiency and user experience. By properly using the cURL extension library, multiple HTTP requests can be initiated simultaneously to maximize the use of network bandwidth. At the same time, abnormal situations are handled reasonably to ensure the stability and reliability of downloads. Through these development skills, we can handle the download requirements of large amounts of files more efficiently in actual projects.
The above is the detailed content of Efficiency improvement: Sharing of development skills for PHP asynchronous HTTP download of multiple files. For more information, please follow other related articles on the PHP Chinese website!