Code: Select all
function getData()
{
if ($this->type == 'POST')
{
$this->login();
$data_pack = $this->prepareVars($this->vars);
$curl = &new cURLManager($this->URL);
$curl->setOption(CURLOPT_HEADER, true);
$curl->setOption(CURLOPT_POST, true);
$curl->setOption(CURLOPT_POSTFIELDS, $data_pack);
$curl->setOption(CURLOPT_RETURNTRANSFER, true);
$curl->setOption(CURLOPT_COOKIEFILE, 'cookie.txt');
$curl->setOption(CURLOPT_COOKIEJAR, 'cookie.txt');
$curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
$curl->setOption(CURLOPT_SSL_VERIFYHOST, false);
$curl->setOption(CURLOPT_REFERER, 'https://www.site.com/search.aspx');
$curl->setOption(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
return $curl->getOutput();
}
}
function login()
{
$vars = array('...');
$data_pack = $this->prepareVars($vars);
$curl = &new cURLManager('https://www.site.com/login.aspx?ReturnUrl=search.aspx');
$curl->setOption(CURLOPT_HEADER, true);
$curl->setOption(CURLOPT_POST, true);
$curl->setOption(CURLOPT_POSTFIELDS, $data_pack);
$curl->setOption(CURLOPT_RETURNTRANSFER, true);
$curl->setOption(CURLOPT_COOKIEFILE, 'cookie.txt');
$curl->setOption(CURLOPT_COOKIEJAR, 'cookie.txt');
$curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
$curl->setOption(CURLOPT_SSL_VERIFYHOST, false);
$curl->setOption(CURLOPT_FOLLOWLOCATION, true);
$curl->setOption(CURLOPT_REFERER, 'https://www.site.com/login.aspx');
$curl->setOption(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
$data = $curl->getOutput();
}I'll explain the behaviour I am getting. Let's say, I have no cookie.txt file (fresh request). After running my script (calling the getData() method) for the first time, the cookie.txt is created but I do not get any search results (only the form). Just a note, I DO know that the POST vars I send are correct as well as that login does not fail. After running the script the second time (with cookie.txt already present), I DO get the search results. I really don't get why this works after the second time only. Since the cookie is already present, I recieve whatever search results I need without any problem with each next request.
You could say, hey you got the cookie and it works when it's there, so what's the prob? The problem is, that the login session expires after 20 mins of inactivity, so, if I let the cookie rot without doing any search requests for 20 mins, I get no search results again. So I need to be able to re-login and get the search results again whenever I want, which, as I described, does not work with my code (it only works after running it twice).
When writing the code, this is the process I imagined and wanted to follow:
1. cURL logs into login.aspx and gets a fresh cookie.txt
2. It then POSTs my vars to search.aspx together with sending cookie.txt to the server and as a result gets the search results.
But for some reason it doesn't work correctly as I explained above. Do you guys have some hints regarding the problem? What should I try/correct? Any help would be nice.