phpcookiescurlcookiejar

use curl to save login cookies


I have no real experience working with curl, so I probably need some help.

I have a php script that needs access to a file called redownloadsubmitter.jsp, but when I try it fails because the php script on my server has no authentication done prior to accessing the script.

The login script is located in portalloginsubmitter.jsp

How can I "login" with the PHP script and then afterwards access the downloadsubmitter to download the needed data?

I read some articles about curlopt_cookiejar, but I can't seem to get it how I need to go about it. Help?


Solution

  • This link should help: http://curl.haxx.se/libcurl/php/examples/cookiejar.html

    In summary. Hit the page where the cookies are generated (EX: before you need them), then use this in the curl request.

    curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
    

    Make sure you execute and close it, to get the cookie jar to store the cookies. They are only stored on the closing of the curl request. On the next response (ex: when you require the cookies).

    Add this into it.

    curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");
    

    Just a note from experience making automated scripted log ins, most scripts require way more than just cookies. Some will require some of the below mentioned things.