I've been trying to get some page data from this website using cURL. Page is behind authorization, using cookies, without SSL.
I checked a lot of manuals and examples of setting up php cURL script, but none seemed to be working.
Every time I run my script, cookie file updates, but I get empty string as a result. If I set CURLOPT_FOLLOWLOCATION to 1, I get login page as a result. So I assume, original script returns redirect back to login page.
I tried messing with CURLOPT_USERAGENT, CURLOPT_REFERER, but it didn't help.
Also, if I manually set CURLOPT_COOKIE, PHPSESSID (from real login session using browser and human input), it works fine.
So, here's my code:
<?php
set_time_limit(10);
define('USERNAME', 'username');
define('PASSWORD', 'password');
define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
define('COOKIE_FILE', 'cookie.txt');
define('LOGIN_FORM_URL', 'http://website/auth');
define('LOGIN_ACTION_URL', 'http://website/distribution/index');
$postValues = array(
'login_msisdn' => USERNAME,
'password' => PASSWORD
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, LOGIN_FORM_URL);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, realpath(COOKIE_FILE));
//curl_setopt($curl, CURLOPT_COOKIE, "PHPSESSID=relkdrgg94gfdgfg834g");
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, LOGIN_FORM_URL);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
curl_exec($curl);
if(curl_errno($curl)){
throw new Exception(curl_error($curl));
}
curl_close($curl);
$curl = curl_init()
curl_setopt($curl, CURLOPT_URL, LOGIN_ACTION_URL);
curl_setopt($curl, CURLOPT_COOKIEFILE, realpath(COOKIE_FILE));
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($curl);
echo $html;