【问题标题】:php curl to download multiple photos as zip from facebook not workingphp curl从facebook下载多张照片作为zip不起作用
【发布时间】:2013-01-19 13:09:17
【问题描述】:

我正在使用 curl 从 Facebook url 为授权用户获取照片,对于每张照片,我将其添加到 zip 文件中,以允许用户以 zip 格式下载整个相册。

 if( !isset($_GET['id']) )
die("No direct access allowed!");
    require 'facebook.php';
  $facebook = new Facebook(array(
'appId'  => 'removed',
'secret' => 'removed',
'cookie' => true,
));

if( !isset($_GET['id']) )
die("No direct access allowed!");
    $params = array();
    if( isset($_GET['offset']) )
        $params['offset'] = $_GET['offset'];
    if( isset($_GET['limit']) )
        $params['limit'] = $_GET['limit'];
    $params['fields'] = 'name,source,images';
    $params = http_build_query($params, null, '&');
    $album_photos = $facebook->api("/{$_GET['id']}/photos?$params");
    if( isset($album_photos['paging']) ) {
        if( isset($album_photos['paging']['next']) ) {
            $next_url = parse_url($album_photos['paging']['next'], PHP_URL_QUERY) . "&id=" . $_GET['id'];
        }
        if( isset($album_photos['paging']['previous']) ) {
            $pre_url = parse_url($album_photos['paging']['previous'], PHP_URL_QUERY) . "&id=" . $_GET['id'];
        }
    }
    $photos = array();
    if(!empty($album_photos['data'])) {
        foreach($album_photos['data'] as $photo) {
            $temp = array();
            $temp['id'] = $photo['id'];
            $temp['name'] = (isset($photo['name'])) ? $photo['name']:'photo_'.$temp['id'];
            $temp['picture'] = $photo['images'][1]['source'];
            $temp['source'] = $photo['source'];
            $photos[] = $temp;
        }
    }
    ?>
<?php if(!empty($photos)) { ?>
<?php
$zip = new ZipArchive();


$tmp_file =tempnam('.','');
$file_opened=$zip->open($tmp_file, ZipArchive::CREATE);


foreach($photos as $photo) {
    $url=$photo['source'];
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $download_file=curl_exec($ch);
    #add it to the zip
    $file_added=$zip->addFile($download_file);
}

    # close zip
    $zip->close();

    # send the file to the browser as a download
    header('Content-Description: File Transfer');
    header('Content-Type: application/force-download');
    header('Content-Disposition: attachment; filename="albums.zip"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($tmp_file));
    ob_clean();
    flush();
    readfile($tmp_file);
    exit;
 } ?>

问题:zip->open 创建一个文件,zip->add 为每个添加的文件返回 1,但 zip 大小仍然为 0,并且 readfile 不提示下载。

【问题讨论】:

  • 当您var_dump($download_file) 时会发生什么?您是否正在尝试访问其他人的照片? Facebook 可能对此有话要说……
  • 字符串(一些数字),为每张照片显示。你要吗?不,我不会访问任何人的照片
  • 您能否在问题中添加$zip-&gt;addFile 方法的代码?
  • $zip->addFile??不是为php内置的吗,这里php.net/manual/en/ziparchive.addfile.php
  • 阅读ZipArchive::addFile 的手册——什么这个方法期望作为第一个参数?那你就去吧。

标签: php facebook-graph-api curl facebook-php-sdk


【解决方案1】:

目前,您正在尝试这样做:

foreach ($photos as $photo) {
    $url = $photo['source'];
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $download_file = curl_exec($ch);
    $file_added = $zip->addFile($download_file);
}

这不起作用,因为$dowload_file 是一个字符串,而不是一个文件名。您需要将 cURL 参数保存到文件中,并将文件名传递给 $zip-&gt;addFile。如果您查看PHP's documentation,它需要的是文件名,而不是字符串:

要添加的文件的路径。

您需要执行以下操作:

$temp_file = './temp_file.txt';
file_put_contents($temp_file, $download_file);
$file_added = $zip->addFile($temp_file);

// Delete after use
unlink($temp_file);

【讨论】:

  • 那么删除我存储的文件怎么样,因为它会将它们永远保存在我的服务器上?你能详细说明一下吗
  • 它只会保存一个文件,因为文件名总是相同的。你只会覆盖它。如果您真的想在使用后删除它,请参阅我的更新答案。
  • 已编辑代码,现在我正在获取图像,但它们不正确。是 fwrite 或 file_put_contents 造成问题还是我没有从 curl 获取正确的数据??
  • 你从 curl 得到一个 STRING。您的 zip 函数需要一个 FILENAME。这就是为什么我说使用 file_put_contents 将 STRING 保存为 FILE,并将 FILENAME 传递给您的 zip 函数。
猜你喜欢
  • 2012-09-28
  • 2011-09-10
  • 1970-01-01
  • 2018-11-17
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多