I am Trying to login to 2shared with curl-php but for some reason it just returns me login page and does not set proper cookies in cookie file. Below is my code. Thanks for any help.
$user = "";
$pass = "";
$cookie = "cookie.txt";
$jsonp = 'jsonp'.time();
if (file_exists($cookie)) {
unlink($cookie);
}
$post = array(
"login" => $user,
"password" => $pass,
"callback" => $jsonp
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.2shared.com/login?callback=".$jsonp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Requested-With: XMLHttpRequest'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.2shared.com/');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0");
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8" );
$return = curl_exec($ch);
curl_close($ch);
echo $return;
EDIT:
When I login via browser and watch traffic via HTTP analyzer i noticed after hitting login button it returns this data and redirect to loginRedirect object and i notice it set some cookies which does not appears while I am doing php-curl request:
{
"ok":true,
"rejectReason":"",
"loginRedirect":"http://www.2shared.com/account/homeDoorway.jsp;jsessionid=3F253C7C641C7A8402D4AC9872C1CEAE.dc282?rand=0.8112776952920494",
"loggedIn":"myemail@email.com",
"needActivation":false
}
But when trying to login with curl-php above code it return me this data:
jsonp1339804887({
"ok":true,
"rejectReason":"",
"loginRedirect":"http://www.2shared.com/login.jsp?sessionUnavailable=1",
"loggedIn":"",
"needActivation":false
})
As always when doing web scraping, the key is to compare with a recorded session done manually with a browser (like with LiveHTTPHeaders or similar tools). Then make sure that your script is sending a request as similar as the recorded one as possible.
If you had done that, you would’ve seen that…
The login form on 2shared doesn’t seem to use a multipart formpost, so your passing of $array to CURLOPT_POSTFIELDS is wrong. It should simply be a string in the form of “login=$name&password=$secret”. This said, this may not be the only flaw in your approach.