【问题标题】:Using cURL to download images to ZipArchive使用 cURL 将图像下载到 ZipArchive
【发布时间】:2010-07-19 18:40:36
【问题描述】:

我正在尝试通过他们的 API 从社交网站将图像下载到 ZipArchive,我正在使用以下代码:

...
public function downloadAlbumZip($album_name, $photos)
{
    $zip = new ZipArchive();
    $album_name = $this->_cleanAlbumName($album_name);
    $filename = 'album.zip';
    $file = tempnam(PHOTOS, "{$album_name}-").'.zip';

    if ($zip->open($file, ZIPARCHIVE::CREATE) === TRUE)
    {
        foreach ($photos as $photo) {
            $image = $photo['pid'] . '.jpg';
            $binary = $this->_getImage($photo['src']);
            $zip->addFromString($image, $binary);
        }

        $output = print_r($zip, true);
        $zip->close();

        exit('Zip Saved.<br /><a href="javascript:history.go(-1);">Back to Album Overview</a>');
    } else {
        die('Zip Failed.');
    }
}

private function _getImage($img)
{
    if ( function_exists('curl_init') )
    {
        $ch = curl_init($img);

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

        $rawdata = curl_exec($ch);
        curl_close($ch);

        return $rawdata;
    }
}
...

哪个有效,但速度很慢,并且经常以超时错误结束,有人可以推荐一种加快速度的方法吗?

谢谢

【问题讨论】:

    标签: php image curl download zip


    【解决方案1】:

    您应该先下载所有图像,验证它们是否正常,然后在超时时重新下载。

    您可以使用CURL_MULTI_EXEC 并行执行图片下载。

    请记住,加速任何事情的关键是确定瓶颈所在。是压缩文件吗?如果是这种情况,您可以使用本机二进制文件并脱壳。是下载图片吗?是图片的大小吗?

    您应该进行一些分析,即使它只是回显time() 以查看哪些操作花费的时间最长。

    【讨论】:

      猜你喜欢
      • 2015-11-26
      • 2011-06-30
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      相关资源
      最近更新 更多