2016-01-30 1 views
1

J'essaye de me connecter à un site Web et de télécharger un dossier de csv, la connexion semble fonctionner mais le fichier de csv ne télécharge pas, quand je fais ces manuellement je peux télécharger ce dossier problème. il ne donne pas de message d'erreur et la sortie var_dump montre « true »PHP CURL Connectez-vous et téléchargez le code de fichier ne fonctionne pas

quelqu'un peut me aider à renard, vous remercier

define('LOGINURL', 'http://www.highlite.nl/user/login'); 
define('LOGINFIELDS', 'Login=xxxxxx&Password=xxxx&LoginButton=LoginButton'); 
define('DWNLDURL', 'http://www.highlite.nl/silver.download/download/products_v2_0.csv'); 
$ckfile = tempnam ("/tmp", "CURLCOOKIE"); 
$fp = fopen("/tmp/import.csv", "w");     

/* STEP 2. visit the login page to authenticate and set the cookie properly */ 
$ch = curl_init(LOGINURL); 
curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, LOGINFIELDS); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 

$output = curl_exec($ch); 

/* STEP 3. request download */ 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL,DWNLDURL); 
curl_setopt($ch, CURLOPT_FILE, $fp); 
//curl_setopt($ch, CURLOPT_URL, $ch); 
$result = curl_exec($ch); 

var_dump($result); 
+0

http://stackoverflow.com/questions/34998100/download-a-file-with-curl/34998285#34998285 – Drone

Répondre

1

essayer d'ajouter 'w+' à votre fonction fopen

serait ..

$fp = fopen("/tmp/import.csv", "w+"); 

En cas de réussite, la fonction fopen renvoie une ressource de pointeur de fichier. Notez que nous passons en "w +" comme deuxième paramètre parce que "w +" indique à PHP que nous voulons ouvrir le fichier en lecture et en écriture. Après avoir configuré avec succès notre pointeur de fichier, nous pouvons le passer à cURL via l'option CURLOPT_FILE, comme vous le faites déjà.

//Pass our file handle to cURL. 
curl_setopt($ch, CURLOPT_FILE, $fp); 
1

S'il vous plaît essayer cette

define('LOGINURL', 'http://www.highlite.nl/user/login'); 
define('LOGINFIELDS', 'Login=xxxxxx&Password=xxxx&LoginButton=LoginButton'); 
define('DWNLDURL', 'http://www.highlite.nl/silver.download/download/products_v2_0.csv'); 
$ckfile = tempnam ("/tmp", "CURLCOOKIE"); 
$fp = fopen("/tmp/import.csv", "w+"); // Changed w to w+     

/* STEP 2. visit the login page to authenticate and set the cookie properly */ 
$ch = curl_init(LOGINURL); 
curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, LOGINFIELDS); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 

$output = curl_exec($ch); 

/* STEP 3. request download */ /*Updated code*/ 
$ch = curl_init(DWNLDURL); 
curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_ENCODING, ""); 
$result = curl_exec($ch); 
curl_close($ch); 
fclose($fp); 
var_dump($result);