Using Curl to login to a website, go to a link, again submit a form and get the output Using Curl to login to a website, go to a link, again submit a form and get the output curl curl

Using Curl to login to a website, go to a link, again submit a form and get the output


You need to attach the cookie(s) to CURL. There are two options:

  1. You can do this all manually by reading the cookie, and attaching manually using

    $ch = curl_init();$sCookie = 'JSESSIONID=' . $sessionIDSavedEarlier . '; path=/'; curl_setopt ( $ch , CURLOPT_COOKIE, sCookie );... rest of POST
  2. You can use a "cookie jar" (probably easiest)

    $ch = curl_init();curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");... rest of POST

(Making sure you can read/write to a file called "cookie.txt" - if you have multiple users using hte script, then make that file unique for each user through a PHP session!)


So you want to login, get the page and then goto a page which is only accessible once logged in?

If so, then ensure that the cookie.txt is writeable by your script and check the url doesn't contain any special parameters (like your session ID). Are you extracting it from the profile page.

If you know the content of the form, you should just be able to post direct to that forms destination, but you may need to set the referring page to the site your logging into.


curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

Use this in your cUrl script .Where

$postfields = 'form1=value1&form2=value2&form3=value3';

where form1,form2.. are the 'name' attributes of your inputs .