Om te beginnen: ik weet (vrijwel) niks van programmeren.
Ik gebruik al paar jaar een script voor het downloaden van bestanden vanaf de website van leverancier naar mijn eigen server. dit script laat ik via cronjobs uitvoeren. Bij deze leverancier moest echter ingelogd worden voor het bestand gedownload kon worden.
Dit script is als volgt:
<?php
$remotefile = 'http://map.website.nl/csv/csv-voorraad.csv';
$localfile = '/home/u1667y88765/domains/mijnsite.nl/public_html/files/voorraad-leverancier.csv'; // <-- Change this to an existing directory where to store the xml file
// This file is needed to save the session cookie
$scalacookie ='tmp/leveranciercookie.txt'; // <-- Change this to an existing directory to store a temporary file containing the cookie (cookie jar)
$loginUrl = "https://www.websiteleverancier.nl/login.html";
// User name and password to login the wholesale website, a separate account can be create for this.
$user = '[email protected]'; // <-- put your login here
$pw = 'password'; // <-- put your password here
//init curl
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'login_email='.$user.'&login_password='.$pw);
//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, $scalacookie);
//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request (the login)
$store = curl_exec($ch);
//the login is now done and you can continue to get the
//protected content.
//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, $remotefile);
//execute the request
$content = curl_exec($ch);
// close session
curl_close($ch);
//save the data to disk
file_put_contents($localfile, $content);
?>
dit werkt voor 1 leverancier dus prima.
Nu wil ik dus voor andere leverancier gebruiken.
Maar als ik zelf wil inloggen moet ik inloggen op https://www.websiteleverancier.nl/login.html
maar het gewenste bestand moet ik downloaden vanaf http://map.website.nl/csv/csv-voorraad.csv en daar is dus geen gebruikersnaam en wachtwoord voor nodig.
Als ik nu via cron dat script uitvoer krijg ik een csv bestand met daarin het volgende:
<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx</center>
</body>
</html>
login gegevens kloppen.
Nu is bovenstaande doordat niet ingelogd hoeft te worden waarschijnlijk zo en zo veel te uitgebreid dus had ik op google gezocht naar ander script voor downloaden van een bestand via php. darabij kwam ik het volgende tegen:
#!/usr/bin/php <?php $data = @file("http://map.website.nl/csv/csv-voorraad.csv"); $lf = "/home/u1667y88765/domains/mijnsite.nl/public_html/files/voorraad-leverancier.csv"; $fh = fopen($lf, 'w'); fwrite($fh, $data[0]); fclose($fh); ?>
Maar als ik die start via een cronjob gebeurd er helemaal niks. word geen file voorraad-leverancier.csv aangemaakt.
wie kan me vertellen hoe ik dit probleempje kan oplossen zodat ook zonder in te loggen dat bestand naar mijn server gedownload wordt?
Groet
Daniel