我正在尝试使用 cURL 从具有多个连接的 URL 下载图像以加快该过程。
这是我的代码:
function multiRequest($data, $options = array()) {
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($data as $id => $d) {
$path = 'image_'.$id.'.png';
if(file_exists($path)) { unlink($path); }
$fp = fopen($path, 'x');
$url = $d;
$curly[$id] = curl_init($url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_FILE, $fp);
fclose($fp);
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curly as $id => $c) {
curl_multi_remove_handle($mh, $c);
}
// all done
curl_multi_close($mh);
}
并执行:
$data = array(
'http://example.com/img1.png',
'http://example.com/img2.png',
'http://example.com/img3.png'
);
$r = multiRequest($data);
所以它并没有真正起作用。它创建了 3 个文件,但字节为零(空),并给我以下错误(3 次),它正在打印原始 .PNG 的某种内容:
Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in /Applications/MAMP/htdocs/test.php on line 34
那么请问,你能告诉我如何解决吗?
在此先感谢您的帮助!