Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/01/2013, 06:52
onda_allstar
 
Fecha de Ingreso: enero-2007
Mensajes: 17
Antigüedad: 19 años, 7 meses
Puntos: 0
CURL error al descargar CSV

Hola a todos.

Estoy utilizando CURL para logearme en una web y descargar un fichero .CSV. El código que utilizo me logea correctamente. Si accedo a alguna página .html también me funciona correctamente, pero cuando accedo a una dirección donde se encuentra un ficher .CSV me devuelve el mismo error que da si no estuviera logeado.


Aquí mi código que es el mismo que hay en este enlace:
http://www.forosdelweb.com/f18/curl-tomar-enviar-cookie-604822/


El ejemplo me crea correctamente el archivo info.html (necesito estar logeado para acceder, asi que me logeo correctamente y me guarda las cookies) pero al hacer la segunda petición sobre un archivo .CSV lo trata igual que si no estuviera logeado.

Alguna sugerencia?

Código PHP:
<?php
$location 
= "";
$cookiearr = array();

function 
getPage()
{
    global 
$location;
    global 
$cookiearr;
    global 
$ch;
    
    
$ch = curl_init();
    
    
$url = "https://www.web.com/login"; 

    
curl_setopt($ch, CURLOPT_URL,$url);
    
curl_setopt($ch, CURLOPT_REFERER, "");
    
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
    
    
$html = curl_exec($ch);
    
    
$matches = array();
    
    
$username = "user"; 
    
$password = "password";             
    
    
$fields = array("Email" => $username, "Passwd" => $password);  
    
    foreach(
$fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    
$fields_string = substr(trim($fields_string),0,-1);

    
$action = $url;
    
curl_setopt($ch, CURLOPT_URL, $action);
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    
curl_setopt($ch, CURLOPT_POST, 1);
    
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);

    
$html = curl_exec($ch);
        
    
$url = "http://www.web.com/info.html";
    
    
curl_setopt($ch, CURLOPT_URL, $url);
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    
    
$a = curl_exec($ch);
    
    
$fp = fopen('item.html', 'w');
    
fwrite($fp, $a);
    
fclose($fp);
    
    
    
$url = "www.web.com/data.CSV";
    
    
curl_setopt($ch, CURLOPT_URL, $url);
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    
   
$b = curl_exec($ch);
    
    
$fp = fopen('data.csv', 'w');
    
fwrite($fp, $b);
    
fclose($fp);
        
    return 
"";
}

function 
read_header($ch, $string)
{
    global 
$location;
    global 
$cookiearr;
    global 
$ch;
    
    
$length = strlen($string);
    if(!
strncmp($string, "Location:", 9))
    {
        
$location = trim(substr($string, 9, -1));
    }
    if(!
strncmp($string, "Set-Cookie:", 11))
    {
        
$cookiestr = trim(substr($string, 11, -1));
        
$cookie = explode(';', $cookiestr);
        
$cookie = explode('=', $cookie[0]);
        
$cookiename = trim(array_shift($cookie)); 
        
$cookiearr[$cookiename] = trim(implode('=', $cookie));
    }
    
$cookie = "";
    if(
trim($string) == "") 
    {
        foreach (
$cookiearr as $key=>$value)
        {
            
$cookie .= "$key=$value; ";
        }
        
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }
    
    return 
$length;
}

echo 
getPage();
?>