$cookie_jar = tempnam('/tmp','cookie');
// INIT CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, self::CURL_TIMEOUT);
// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, self::LOGIN);
// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);
// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'email=' . self::USR . '&password=' . self::PASS);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);
if(!$store) return false;
// SET FILE TO DOWNLOAD
curl_setopt($ch, CURLOPT_URL, self::SITE . $this->domain);
So I'm trying to access a site using curl. First, I login to the site using curl's post variables. Then I access a page on the site to retrieve data (after logging in using curl). I'm I doing the right thing above? Have I used the right code? Is it necessary to have these two lines of code:
I haven't looked at it closely, but it looks about right.
Pretty much if you're trying to mimic a browser and the page your code's visiting is expecting
cookies to be enabled - you need the cookie lines and associated file(s). If you'll be logging in each
time and cookies aren't required, you can probably eliminate those lines and the need for the
"cookiejar" file and path to save cookies when the curl session ends.
Is it working?
Actually... Now that I look at it...
It doesn't appear that you need the cookie stuff and if it's working, it doesn't appear that the target server requires cookies be set.
As a matter of fact, you're just creating a temporary file with nothing in it and deleting it when the session's over.
That as you surmised would be a waste and make it hard to comprehend the code in 6 months.
Exploring the cookie file a little more...
The cookie file would be unnecessary unless of course that somewhere earlier in the code, the temp cookie file
gets loaded with actual valid cookie data that the remote server wants and needs to munch on...