Basically, I have a script that logs in to a website, and downloads a file. Fairly straight forward. Unfortunately something's missing in my code that's preventing it from working properly.
When I run it, I get back a html page outputted to my file which is exactly what I would get in my browser if I was attempting to access the file link without being logged on; access denied, you must be logged in, etc.
However, if I run the first part of my script on its own by commenting out the file download request, then re-run the script in its entirety, I am able to download the file as I should, so I know it's working in a sense. It just doesn't seem to want to log me in when I run the entire script.
// Log me in
curl_setopt($handle, CURLOPT_URL, $login_url);
curl_setopt($handle, CURLOPT_REFERER, $admin_url);
curl_setopt($handle, CURLOPT_COOKIEJAR, $Cookie_Location);
curl_setopt($handle, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
// Grab the file
curl_setopt($handle, CURLOPT_URL, $csv_loc);
curl_setopt($handle, CURLOPT_FILE, $csv_handle);
echo $response = curl_exec($handle);
curl_close($handle);
So I can log in, then re-run the script and download the file, but I can't do both at the same time. I've tried all sorts of different additional curl options such as COOKIEJAR and COOKIEFILE, as well as FOLLOWLOCATION and REFERER, which were my only hunches as to why my code wasn't working. Something in my "Grab the file" code is either breaking my log in, or is behaving like I'm not logged in.
Edit: SOLVED.
I've decided to include the solution so others avoid the same mistake I did.
All I needed to do was seperate my requests, like so;
// Log me in
curl_setopt($handle, CURLOPT_URL, $login_url);
curl_setopt($handle, CURLOPT_REFERER, $admin_url);
curl_setopt($handle, CURLOPT_COOKIEJAR, $Cookie_Location);
curl_setopt($handle, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
echo $response = curl_exec($handle);
// Grab the file
curl_setopt($handle, CURLOPT_URL, $csv_loc);
curl_setopt($handle, CURLOPT_FILE, $csv_handle);
curl_exec($handle);
curl_close($handle);
First curl_exec logs me in to the site, and the second one then grabs and downloads my file. Then I just close the handle.