[UPHPU] curl script to log into site
TuxToaster
tuxtoaster at gmail.com
Fri Apr 6 23:48:23 MDT 2012
Typically you'd ensure that you enable the CURL_HEADER and disable the CURL_FOLLOWLOCATION options, and then your response would include the Location: header with the URL so that you could parse it out.
There is also a way of getting the effective URL using curl_getinfo, but you may have better results parsing the actual response as there could be other details returned in the page that may be useful.
As far as sequence, you just call them in order, i.e. (pseudo code, not tested but should give you the idea):
$ch = curl_init('http://...../login.php');
curl_setopt($ch, CURL_HEADER, true);
$Response = curl_exec($ch);
curl_close($ch);
// parse out response here and get URL
$parsed_location_url = preg_match('Location:....');
$ch = curl_init($parsed_location_url);
$Response = curl_exec($ch);
curl_close($ch);
Also, if the site sets any cookies, be sure to specify a cookie jar location (just a text file somewhere, like /tmp/cookies.txt) in the options on both requests, this will allow it to maintain cookies if it uses them for auth.
Hope this helps!
Sent from my iPad
On Apr 6, 2012, at 10:03 PM, Wade Preston Shearer <wadeshearer.lists at me.com> wrote:
> I am trying to write a cURL script that authenticates with a site and then downloads a file. The issue is that posting to the form action directly does not work. The reason for that is because the website generates a token when you access the log in form and passes it in via the URL.
>
> So, I think to complete this, I would have to do two cURL hits:
>
> 1. One to hit the original URL. Handle the redirect. Get the token out the of the URL.
>
> 2. Hit the second URL passing the token in as a GET parameter as I POST in the log-in form vars.
>
>
> I know how to do #2 but am not sure about #1 or how to complete #1 and #1 in sequence in the same script.
>
>
> Any pointers anyone can provide?
>
> _______________________________________________
>
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
More information about the UPHPU
mailing list