I need to get a content from the page that requires authentication and after that need to create a post request for search criterias. I thought i need two cURL requests: first to login and save session data and second to submit a form... i tried several variants. The existing code can login and get the content but only this content that is before submiting the searchform. And also it can't use it session that i save and i the cookie.txt file is not empty. The second cURL requests says it needs the autentication again... My code:
$host = 'http://mydomain.ee/search/?&page=1';
$username = 'myusername';
$password = 'mypassword';
$data = array('tel' => 3, 'ok' => 'Otsi');
$curlHandler = curl_init($host);
curl_setopt($curlHandler, CURLOPT_HEADER, 1);
curl_setopt($curlHandler, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curlHandler, CURLOPT_TIMEOUT, 30);
curl_setopt($curlHandler, CURLOPT_POST, 1);
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, "tel=3");
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, "ok=Otsi");
curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curlHandler, CURL_VERBOSE, TRUE);
curl_setopt($curlHandler, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($curlHandler, CURLOPT_COOKIEJAR, $this->files_path . 'cookie.txt');
curl_setopt($curlHandler, CURLOPT_COOKIEFILE, $this->files_path . 'cookie.txt');
$return = curl_exec($curlHandler);
curl_close($curlHandler);
print_r(htmlspecialchars($return));
echo '<br/><br/>';
$curlHandler = curl_init($host);
curl_setopt($curlHandler, CURLOPT_HEADER, 1);
//curl_setopt($curlHandler, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curlHandler, CURLOPT_TIMEOUT, 30);
curl_setopt($curlHandler, CURLOPT_POST, 2);
//curl_setopt($curlHandler, CURLOPT_POSTFIELDS, "tel=3");
//curl_setopt($curlHandler, CURLOPT_POSTFIELDS, "ok=Otsi");
curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $post);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curlHandler, CURL_VERBOSE, TRUE);
curl_setopt($curlHandler, CURLOPT_COOKIESESSION, TRUE);
//curl_setopt($curlHandler, CURLOPT_COOKIEJAR, $this->files_path . 'cookie.txt');
curl_setopt($curlHandler, CURLOPT_COOKIEFILE, $this->files_path . 'cookie.txt');
$return = curl_exec($curlHandler);
curl_close($curlHandler);
print_r(htmlspecialchars($return));
And like i said: i got html content after first curl_exec($curlHandler); but after second i get: HTTP/1.1 401 Authorization Required Date: Wed, 09 Jul 2014 14:19:20 GMT Server: Apache/2 WWW-Authenticate: Basic realm="Password" Content-Length: 18 Content-Type: text/html; charset=iso-8859-1 Unauthorized (401) Why it can't use it's cookie, filepath is the same and cooki.txt has it's content... how to fix that?
Thanks for any help!