2011-12-29 152 views
0

我正在編寫腳本以將Megaupload中的文件下載到我的服務器上。我使用PHP捲曲,我有登錄腳本,下載Cookie文件:使用cURL從Megaupload下載文件PHP

<?php 
function login($username, $password){ 
$mega = curl_init(); 
curl_setopt($mega, CURLOPT_URL, "http://www.megaupload.com/?c=login"); 
curl_setopt($mega, CURLOPT_POST, true); 
curl_setopt($mega, CURLOPT_POSTFIELDS, "login=1&redir=1&username=$username&password=$password"); 
curl_setopt($mega, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/megaupload_cookie.txt"); 
curl_setopt($mega, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/megaupload_cookie.txt"); 
curl_exec($mega); 
curl_close($mega); 
} 
?> 

和下載腳本:

<?php 
    include("megaupload_login.php"); 
    login("username", "ps"); 
    set_time_limit(0);  
    $url = "http://www.megaupload.com/?d=A428CAKH"; 
    $fp = fopen("winch.zip", "w"); 
    $dl = curl_init($url); 
    curl_setopt($dl, CURLOPT_COOKIEFILE, "megaupload_cookie.txt"); 
    curl_setopt($dl, CURLOPT_FILE, $fp); 
    curl_exec($dl); 
    curl_close($dl); 
    fclose($fp); 
    ?> 

的問題是,該文件不下載。我所得到的是一個名爲winch.zip的文件,大小爲0字節。我認爲該程序實際上是下載登錄頁面,因爲在運行腳本時,瀏覽器只顯示megaupload登錄頁面,但地址是localhost。任何想法爲什麼這可能不工作?

+0

嘗試以下鏈接 http://stackoverflow.com/questions/6103774/serving- downloads-with-php-and-curl – Nishant 2011-12-29 07:40:48

+0

大多數下載主機都會發出重定向到一個標記化/時間有限的下載url。你還沒有通過'CURLOPT_FOLLOWLOCATION'告訴curl遵循重定向# – 2011-12-29 07:41:26

+0

@marcb真棒,後續設置使它工作,非常感謝。 – jz999 2012-01-03 02:01:10

回答

1

嘗試下面的代碼您的下載部分:

 

function download_file($link) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $link); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megaupload.txt"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $result = curl_exec($ch);  
    curl_close($ch); 
    return $result; 
} 
$filecontents = download_file("http://www.megaupload.com/?d=A428CAKH"); 
 

希望它可以幫助