views:

86

answers:

2

Hello:

I am attempting to use PHP and cURL to:

  1. Access a web-form & maintain session
  2. Post the data to that form with session in place

In my head, I see it as two separate functions, one to get the form (initiate session) and another to post the data.

A: 

You can execute a command line call to curl from php to save cookies to a file like so:

curl -c '/tmp/mycookies.txt' 'http://www.site.com/login.php

Then use those cookies when submiting to the page like so:

curl -b '/tmp/mycookies.txt' -d 'uname=MyLoginName&pass=MyPassword&action=login&x=67&y=11' 'http://www.site.com/login.php'

For more info about these command line flags:

Jalmarez
Thanks for that; but I notice now I am getting a 302 redirect when I use cURL. But, not when I'm using a web browser. It appears the page is looking for headers as well.
Andrew
A: 

You'll find this page helpful:

http://uk2.php.net/manual/en/function.curl-setopt.php

In particular, check out CURLOPT_COOKIE, CURLOPT_COOKIEFILE, and CURLOPT_COOKIEJAR.

You should be able to make 2 curl requests in the same php file, one that initiates the form (and grabs your session cookie then places it in your cookie file) and another that POSTS your request (using the same cookies).

Check out the comments further down that page for examples. You might also want to look at the paypal curl examples floating around the web, they do pretty much what you're after.

Hippyjim