cUrl Login then cUrl Download
Yes it's possible.
CURLOPT_COOKIEJAR
is the write path for cookies, while CURLOPT_COOKIEFILE
is the read path for cookies. If you provide CURLOPT_COOKIEFILE
with the same path as you did with CURLOPT_COOKIEJAR
, cURL will persist the cookies across requests:
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
On ImpressPages I've done it this way:
//initial request with login data$ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php');curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX");curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_COOKIESESSION, true);curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hostscurl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems on some hosts$answer = curl_exec($ch);if (curl_error($ch)) { echo curl_error($ch);}//another request preserving the sessioncurl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile');curl_setopt($ch, CURLOPT_POST, false);curl_setopt($ch, CURLOPT_POSTFIELDS, "");$answer = curl_exec($ch);if (curl_error($ch)) { echo curl_error($ch);}