Hello pepsichris,
I had to do this exact same thing and here is how I did it. I had to go through 3 steps of a form keeping the session alive and what I did was to extract the session from the header. Here is what I did:
$ch_one = curl_init();
curl_setopt($ch_one, CURLOPT_URL, 'https://url.to.connect');
curl_setopt($ch_one, CURLOPT_REFERER, 'https://www.yourwebsite.c
curl_setopt($ch_one, CURLOPT_HEADER, 1);
curl_setopt($ch_one, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch_one, CURLOPT_USERAGENT, "$_SERVER[HTTP_USER_AGENT]
curl_setopt($ch_one, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch_one, CURLOPT_TIMEOUT, 5);
$result_one = curl_exec($ch_one);
curl_close($ch_one);
preg_match_all('|Set-Cooki
$cookies = implode(';', $cookies[1]);
$sess = explode(";", $cookies);
$ch_two = curl_init();
curl_setopt($ch_two, CURLOPT_URL, 'https://url.that.the.form
curl_setopt($ch_two, CURLOPT_REFERER, 'https://url.to.connect');
curl_setopt($ch_two, CURLOPT_HEADER, 1);
curl_setopt($ch_two, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch_two, CURLOPT_USERAGENT, "$_SERVER[HTTP_USER_AGENT]
curl_setopt($ch_two, CURLOPT_POST,1);
curl_setopt($ch_two, CURLOPT_POSTFIELDS, $post="$sess[1]");
curl_setopt($ch_two, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch_two, CURLOPT_TIMEOUT, 5);
$result_two = curl_exec($ch_two);
curl_close($ch_two);
$ch_three = curl_init();
curl_setopt($ch_three, CURLOPT_URL, 'https://final.processing.
curl_setopt($ch_three, CURLOPT_REFERER, 'https://url.that.the.form
curl_setopt($ch_three, CURLOPT_HEADER, 1);
curl_setopt($ch_three, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch_three, CURLOPT_USERAGENT, "$_SERVER[HTTP_USER_AGENT]
curl_setopt($ch_three, CURLOPT_POST,1);
curl_setopt($ch_three, CURLOPT_POSTFIELDS, $post="$sess[1]");
curl_setopt($ch_three, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch_three, CURLOPT_TIMEOUT, 60);
$result_three = curl_exec($ch_three);
curl_close($ch_three);
Now the final variables I needed are in $result_three and I used preg_match to get them. Now since I had to simulate 3 steps you notice on the first call im using my website as the referer and the url to connect is the page I needed. then on the second connect you supply the session and the referer is the form page and the url to connect on the second call is the forms action. then on the 3rd call you simulate the the forms action page as the referer and the url is the final processing url. You might not need this many steps but just get the session from the cookie first then use the referer and url to simulate the exact actions you would do clicking through yourself. I had to get the session id this way because in this particular scenario cookie jar was not sending the correct session id and it failed everytime. Now to get the variables for instance I was getting them from form fields in the final result page like this:
preg_match("/<input type='hidden' name='field1' value='(.*?)'>/", $result_three, $field1);
Now preg_match returns an array so the data from the value field can be accessed like this:
$field1[1]
Hope that helps ;)
Main Topics
Browse All Topics





by: palanee83Posted on 2007-12-18 at 03:57:47ID: 20491340
HI
To maintain the session across the form when u use CURL you have to write the cookie information in the cookie file
using the below two line
//you should create the file for cookie and give the absolute path.
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
I got the below code from php.ini. This below code will maintain the cookies across the page
Select allOpen in new window