2013-01-19 52 views
0

我使用curl從Facebook獲取照片url用於授權用戶,對於每張照片我將其添加到zip文件以允許用戶將整個相冊下載爲壓縮。php curl從facebook下載多張照片不起作用

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->打開創建一個文件,zip->添加返回1每添加一個文件,但拉鍊尺寸仍然是0和ReadFile的不提示進行下載。

+0

會發生什麼事,當你'的var_dump($ download_file)'?你想要訪問其他人的照片嗎? Facebook可能有話要說... – hohner

+0

字符串(一些數字),顯示爲每張照片。你問這個? 不,我不訪問任何人的照片用戶將登錄後訪問自己的照片,所以不違法,我相信如果我採取用戶許可。 – sven

+0

你可以在你的問題中添加'$ zip-> addFile'方法的代碼嗎? – hohner

回答

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->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); 
+0

那麼刪除我存儲的文件怎麼樣?因爲它會永久保存在我的服務器上?你可以詳細說明一個律 – sven

+0

它只會保存一個文件,因爲文件名總是相同的。你只會覆蓋它。如果您真的想在使用後刪除它,請參閱我更新的答案。 – hohner

+0

不完全正常工作 – sven