PHP Receive data from a CSV via URL

Asked

Viewed 196 times

0

Good morning, I need to know if it’s possible to do fgetcsv() using the temporary of a file .csv coming from a url.

The reason to try this is due to the fact that Chrome does not overwrite saved files.

Could make the shapes below:

  1. Be able to save by PHP or Javascript always replacing the files.
  2. Being able to read the arqvuio csv without saving it Or any other suggestion that you like, it can be API’s.

I already use php to extract data from a file saved on the server; But in that case you’d have to replace the file name 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
}
  • I didn’t understand well, but as far as I know fopen() accept a URL, only need to have a enabled setting(do not remember the name), if return $url is already a csv I believe that a fopen($url, 'r') will work

  • It even works this way, only it requires the login of the user even with the login already done, directly by the url works.

  • I got it, your session/login is saved/associated with the browser, so if you put it right in the browser it works. When you pass the URL by php, it is another session, basically it is the server accessing the site, so it is not logged in. Is the server that has this url yours? It is the same server that is calling the fopen()?

  • No, it is an external server :( and do not release me access to the databases

  • Pass the login data in the url does not scroll right ?

  • It depends on how the server manages the login url, but probably not. So the best solution is to make a form for people to upload the file it downloads from this server.

  • It is for automation, have to save the file in specific location the base will read it every 5 minutes, so the option to read the file straight from the url would be better.

  • I figured that’s it, but if you don’t have access to direct data, like an api, or something like that, it’s gonna be hard to log in and get csv. Maybe I can, but there’s a e não me liberam acesso as bases if they haven’t given you access the way you want it may even break some law or something like that.

Show 3 more comments

1 answer

1


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

In fact I was always a little intrigued by this too and decided to try to solve. I managed to make it work on 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 displayed the page, so I think it is already a good starting point. I hope it helps.

  • Neuber thanks so much for the help, here it was not blank, the console did not display anything. loginUrl: https://servor.com/login/ dataURl: http://servor.com/aba/...filtros..../output:csv, I put something like this, the login fields are called; User: name="data[User][email]", id="email-input", Password: name="data[User][password]", id="Username"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.