1

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.

Martyn Shutt
  • 1,671
  • 18
  • 25

1 Answers1

3

If this is exactly the code you are using, then:

// Log me in
curl_setopt($handle, CURLOPT_URL, $login_url); 

// Grab the file
curl_setopt($handle, CURLOPT_URL, $csv_loc);

echo $response = curl_exec($handle);
curl_close($handle);

You are redefining your URL. You can not send a POST to one URL (login) and a GET (grab a file) in one request, you will need to send 2 separate requests for that.

Unless your login form gives you file back as a response immediately.

favoretti
  • 29,299
  • 4
  • 48
  • 61
  • I'm not quite sure I follow. Do you mean create an entirely seperate request and handle with curl_init() ? I just tried this, and basically got a HTTP 401 error, as my script also uses Simple HTTP Authentication. I can't seem to get anything to carry over to my second request. – Martyn Shutt Jan 01 '13 at 19:38
  • Have a look at this: http://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl :) – favoretti Jan 01 '13 at 19:41
  • Actually, I've already got my Simple HTTP Authentication sorted out, I just didn't include the code in my question. It's the first thing I do in cURL. My point was; I can't seem to run a second request at all. It's forgetting everything I do in the first request, hence the 401 error. – Martyn Shutt Jan 01 '13 at 19:47
  • Do you use the same cookies in the 2nd request? – favoretti Jan 01 '13 at 19:53
  • Silly me. All I needed to do was add another curl_exec($handle); before my second request, and then close the handle. I was trying to start a completely new handle with curl_init(). I understand how this works now. Just didn't seem too clear how you start a second request. Resolved. Thanks for your help. – Martyn Shutt Jan 01 '13 at 19:59