views:

102

answers:

1

I am passing post data to a website, but I have to visit the homepage first before I can navigate to any other parts of the website. So how can I do that? Do I just use a header redirect and then have it execute the rest of the cURL?

Update: I think this might be better than the solution below: http://coderscult.com/php/php-curl/2008/05/20/php-curl-cookies-example/

+1  A: 

Just run two cURL calls using the same object. So first initialize cURL with the homepage URL, and execute the session:

$curl = curl_init("http://example.com");
curl_exec($curl);

Then using the same object, set a new URL, and execute it with your set parameters:

curl_setopt($curl,CURLOPT_URL,"your_new_url");
curl_setopt($curl,CURLOPT_POSTFIELDS,"var1=value&var2=another_value");
curl_exec($curl);
BraedenP
don't forget the cookie jar ....
RageZ
Actually you're right! The website stores cookies, how do I make sure this happens?
Doug
Well in that case, add **curl_setopt($curl,CURLOPT_COOKIEJAR,"your_cookies.txt");** before the first curl_exec(), and add **curl_setopt($curl,CURLOPT_COOKIEFILE,"your_cookies.txt");** before the second curl_exec(). That way the cookies from the homepage will be stored in the variable on the first run, and sent back to the server on the second run.
BraedenP