我已经实现了一个 PHP 函数,它检查并下载大量图像(> 1'000) - 使用数组传递给它 - 使用 PHPcurl_multi_init()
method.
经过几次返工后,因为我得到了 0 字节文件等内容。我现在有一个下载所有图像的解决方案 - 但下载的所有其他图像文件都不完整。
在我看来好像我使用file_put_contents()
“太早”,意思是,在使用完全接收到一些图像数据之前curl_multi_exec()
.
不幸的是,我没有找到任何类似的问题,也没有找到任何适合我的情况的谷歌结果,其中我需要使用curl_multi_exec,但不想使用curl-opt-header检索和保存图像“CURLOPT_FILE
".
希望有人能够帮助我了解我所缺少的内容以及为什么我在本地保存了一些损坏的图像。
以下是检索到的损坏图像的一些示例:
这是我传递给 Multi-CURL-Function 的示例数组:
$curl_httpresources = [
[ 'http://www.gravatar.com/avatar/example?d=mm&r=x&s=427'
,'/srv/www/data/images/1_unsplash.jpg' ],
[ 'http://www.gravatar.com/avatar/example?d=identicon&r=x&s=427'
,'/srv/www/data/images/2_unsplash.jpg' ],
[ 'http://www.gravatar.com/avatar/example?d=monsterid&r=x&s=427'
,'/srv/www/data/images/3_unsplash.jpg' ],
[ 'http://www.gravatar.com/avatar/example?d=wavatar&r=x&s=427'
,'/srv/www/data/images/4_unsplash.jpg' ],
[ 'http://www.gravatar.com/avatar/example?d=retro&r=x&s=427'
,'/srv/www/data/images/5_unsplash.jpg' ],
[ 'http://www.gravatar.com/avatar/example?d=mm&r=x&s=427'
,'/srv/www/data/images/6_unsplash.jpg' ],
[ 'http://www.gravatar.com/avatar/example?d=identicon&r=x&s=427'
,'/srv/www/data/images/7_unsplash.jpg' ],
[ 'http://www.gravatar.com/avatar/example?d=monsterid&r=x&s=427'
,'/srv/www/data/images/8_unsplash.jpg' ],
[ 'http://www.gravatar.com/avatar/example?d=wavatar&r=x&s=427'
,'/srv/www/data/images/9_unsplash.jpg' ],
[ 'http://www.gravatar.com/avatar/example?d=retro&r=x&s=427'
,'/srv/www/data/images/10_unsplash.jpg' ],
];
我的 Multi-cURL PHP 函数
现在,对于我当前正在使用的功能 - 并且有点“有效”,除了一些部分下载的文件 - 这是代码:
function cURLfetch(array $resources)
{
/** Disable PHP timelimit, because this could take a while... */
set_time_limit(0);
/** Validate the $resources Array (not empty, null, or alike) */
$resources_num = count($resources);
if ( empty($resources) || $resources_num <= 0 ) return false;
/** Callback-Function for writing data to file */
$callback = function($resource, $filepath)
{
file_put_contents($filepath, $resource);
/** For Debug only: output <img>-Tag with saved $resource */
printf('<img src="%s"><br>', str_replace('/srv/www', '', $filepath));
};
/**
* Initialize CURL process for handling multiple parallel requests
*/
$curl_instance = curl_multi_init();
$curl_multi_exec_active = null;
$curl_request_options = [
CURLOPT_USERAGENT => 'PHP-Script/1.0 (+https://website.com/)',
CURLOPT_TIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_VERBOSE => false,
CURLOPT_RETURNTRANSFER => true,
];
/**
* Looping through all $resources
* $resources[$i][0] = HTTP resource
* $resources[$i][1] = Target Filepath
*/
for ($i = 0; $i < $resources_num; $i++)
{
$curl_requests[$i] = curl_init($resources[$i][0]);
curl_setopt_array($curl_requests[$i], $curl_request_options);
curl_multi_add_handle($curl_instance, $curl_requests[$i]);
}
do {
try {
$curl_execute = curl_multi_exec($curl_instance, $curl_multi_exec_active);
} catch (Exception $e) {
error_log($e->getMessage());
}
} while ($curl_execute == CURLM_CALL_MULTI_PERFORM);
/** Wait until data arrives on all sockets */
$h = 0; // initialise a counter
while ($curl_multi_exec_active && $curl_execute == CURLM_OK)
{
if (curl_multi_select($curl_instance) != -1)
{
do {
$curl_data = curl_multi_exec($curl_instance, $curl_multi_exec_active);
$curl_done = curl_multi_info_read($curl_instance);
/** Check if there is data... */
if ($curl_done['handle'] !== NULL)
{
/** Continue ONLY if HTTP statuscode was OK (200) */
$info = curl_getinfo($curl_done['handle']);
if ($info['http_code'] == 200)
{
if (!empty(curl_multi_getcontent($curl_requests[$h]))) {
/** Curl request successful. Process data using the callback function. */
$callback(curl_multi_getcontent($curl_requests[$h]), $resources[$h][1]);
}
$h++; // count up
}
}
} while ($curl_data == CURLM_CALL_MULTI_PERFORM);
}
}
/** Close all $curl_requests */
foreach($curl_requests as $request) {
curl_multi_remove_handle($curl_instance, $request);
}
curl_multi_close($curl_instance);
return true;
}
/** Start fetching images from an Array */
cURLfetch($curl_httpresources);
非常感谢您的帮助,非常感谢!