I’ve got a cURL PHP script which works. It gets my schedule from my school site. Though there is one strange thing: On my webhost it creates the cookie.txt and on my localhost it doesn’t.
- Why doesn’t it create a cookie on my localhost? Any suggestions? Something with relative paths and wampserver?
And the questions that follows the latter:
- Is there any (speed) advantage of already being logged in on the school site (storing the cookie and thus saving an cURL request)?
I could for example check after the first cURL request if there is evidence in the response that I am already logged in.
If the answer to the above question is: ‘no, this doesn’t make the script faster’ I’ve got another question:
- Is it than best to specify only the
CURLOPT_COOKIEFILEoption? With an empty value? So no cookie jar?
I can’t give you my login information, though here is the script:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,
'http://www.groenewoud.nl/infoweb/infoweb/index.php');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$tokenSource = curl_exec($curl);
print_r (curl_getinfo($curl));
if (!$tokenSource) echo 'token problem';
// Get the token from within the source codes of infoweb.
preg_match('/name="csrf" value="(.*?)"/', $tokenSource, $token);
$postFields = array(
'user' => $userNum,
'paswoord' => $userPass,
'login' => 'loginform',
'csrf' => $token[1]);
$postData = http_build_query($postFields);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$tableSource = curl_exec($curl);
print_r( curl_getinfo($curl));
if (!$tableSource) echo 'post problem';
curl_close($curl);
1)
/cookie/cookie.txtmeans you’d need to have your cookie directory in the ROOT directory of your entire server.cookie/cookie.txt(note: NO leading slash) means the cookie directory would be a sub-directory of your script’s CURRENT directory. E.g. your script is running in/a/b/c/, then you’d have/a/b/c/cookie/cookie.txt.2) For speed advantages, there’s no change in HTTP speeds – you’re still stuck with the same pipes and transfer rates. But having the cookie initially MIGHT save you a few extra hits on the server to simulate the login-sequence, so would effectively be SLIGHTLY faster.
3) As for creating the cookies, that’s entirely up to curl’s settings. If you don’t specify a cookie file or cookie jar, it won’t create or look for the cookie file. Check the configuration/compile options between the two servers to see if one specifies some curl defaults that the other doesn’t have.
4) str_pos WOULD be faster than a curl request. Think of it as the difference between looking in your fridge for some food versus driving to the grocery store. Fridge is local and therefore faster.
5) curlopt_cookiefile tells curl where to store new cookies. curlopt_cookiejar tells curl where to load cookies from when it first starts up. They CAN be different files, but don’t have to be. If you’d like to keep some “clean” baseline cookies, then you use cookiejar = newstuff.txt, and cookiejar=baseline.txt. Once you’ve got an appropriate cookie environment set up, you reset cookiejar to newstuff.txt for subsequent curl runs.