【发布时间】: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