2012-07-06 20 views
7

Deseo descargar un archivo con Curl. El problema es que el enlace de descarga no es directa, por ejemplo:Descargar archivo Curl con url var

http://localhost/download.php?id=13456 

Cuando trato de descargar el archivo con rizo, se descarga el archivo download.php!

Aquí está mi código rizo:

 ### 
     function DownloadTorrent($a) { 
        $save_to = $this->torrentfolder; // Set torrent folder for download 
        $filename = str_replace('.torrent', '.stf', basename($a)); 

        $fp = fopen ($this->torrentfolder.strtolower($filename), 'w+');//This is the file where we save the information 
        $ch = curl_init($a);//Here is the file we are downloading 
        curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // Important 
        curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
        curl_setopt($ch, CURLOPT_URL, $fp); 
        curl_setopt($ch, CURLOPT_HEADER,0); // None header 
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); // Binary trasfer 1 
        curl_exec($ch); 
        curl_close($ch); 
        fclose($fp); 
    } 

¿Hay una manera de descargar el archivo sin conocer el camino?

Respuesta

4

Puede intentar CURLOPT_FOLLOWLOCATION

TRUE para seguir cualquier "Ubicación:" encabezado que el servidor envía como parte de la cabecera HTTP (tenga en cuenta que esto es recursivo, PHP seguirá tantos "Ubicación:" encabezados que se envía, a menos que CURLOPT_MAXREDIRS sea establecido).

Por lo que se traducirá en:

function DownloadTorrent($a) { 
    $save_to = $this->torrentfolder; // Set torrent folder for download 
    $filename = str_replace('.torrent', '.stf', basename($a)); 

    $fp = fopen ($this->torrentfolder.strtolower($filename), 'w+');//This is the file where we save the information 
    $ch = curl_init($a);//Here is the file we are downloading 
    curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // Important 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    curl_setopt($ch, CURLOPT_HEADER,0); // None header 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); // Binary transfer 1 
    curl_exec($ch); 
    curl_close($ch); 
    fclose($fp); 
} 
1

Oooh!

trabajo CURLOPT_FOLLOWLOCATION perfecta ...

El problema es que yo uso CURLOPT_URL por fopen(), simplemente cambiar CURLOPT_URL whit CURLOPT_FILE

y funciona muy bien! gracias por su ayuda =)

Cuestiones relacionadas