PHP Receive data from a CSV via URL

0

Good morning, I need to know if there is a possibility of doing fgetcsv() using temporary of a .csv file coming from a url.

The reason you try to do this is because Chrome does not overwrite any saved files.

You could do the following:

  • Be able to save by PHP or Javascript always replacing the files.
  • Be able to read the csv file without saving it Or any other suggestion that suits, may be API's.
  • I already use php to extract data from a file saved on the server; But in this case you would have to replace the filename with $url

    $url = "http://servidor.com/filtros/%5AD%7A/da:14-02-2017+00%3A00..../output:csv";
    
    if (($base = fopen("arquivo.csv", "r")) !== FALSE) {
       while (($data = fgetcsv($base, 0, ";")) !== FALSE) {
           $data = array_map("utf8_encode", $data);
           //resultado da extração
       }
    }else{
       //retorno do else
    }
    
        
    asked by anonymous 14.02.2017 / 13:34

    1 answer

    1

    Part of your question involves the need to login before getting the file, since the file in question is password protected.

    Actually I've always been a bit puzzled by this too and decided to try to solve it. I was able to make it work in a simple page using cURL.

    $loginUrl = 'http://url-da-pagina-com-login';
    $dataUrl = 'http://url-da-pagina-com-o-csv';
    
    $ch = curl_init ($loginUrl);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'email'=>'[email protected]',
        'password'=>'123',
    ));
    
    curl_exec($ch);
    
    //basicamente esse é o pulo do gato, voce troca a url, entao ainda continua na mesma sessao
    curl_setopt($ch, CURLOPT_URL, $dataUrl);
    $dataResp = curl_exec($ch);
    
    echo $dataResp;
    

    In my test the page was displayed, so I think it's already a good starting point. Hope it helps.

        
    15.02.2017 / 19:57