PHP CURL Login and Download file code not working PHP CURL Login and Download file code not working curl curl

PHP CURL Login and Download file code not working


try adding 'w+' to your fopen function

would be ..

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

On success, the fopen function will return a file pointer resource. Note that we pass in “w+” as the second parameter because “w+” tells PHP that we want to open the file for reading and writing.

After we’ve successfully set up our file pointer, we can hand it over to cURL via the CURLOPT_FILE option, like so what you are already doing.

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


Please try with this

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);