Problems logging in to Yahoo using cURL in php
I am having problem logging in to Yahoo with the PHP code I have written. The code was working earlier, but for some reason it stopped working. It always shows Yahoo login page with my username at the top like, Hi Sudhir
at the header as if I'm logged in already, along with my yahoo username, but displays password field and Login button again saying Please Verify your password
.
Though I tried to resend the request once more taking the new form action https://login.yahoo.com/config/login_verify2?
, but it keeps on displaying the same page. Following is my complete Yahoo login code.
Please suggest what am I doing wrong or am I missing something?
<?php
$yahooCalUrl = "http://calendar.yahoo.com";
$ch = curl_init($yahooCalUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies_new.txt");
//curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies_new.txt");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$resCalUrl = curl_exec($ch);
//echo $resCalUrl;
$loginUrl = "https://login.yahoo.com/config/login?";
//get the hidden values
$searchStr = "/name=\".u\" value=\"(.*?)\"/";
preg_match($searchStr, $resCalUrl, $matches);
$u = $matches[1];
$searchStr1 = "/name=\".challenge\" value=\"(.*?)\"/";
preg_match($searchStr1, $resCalUrl, $matches1);
$challenge = $matches1[1];
$searchStr2 = "/name=\".pd\" value=\"(.*?)\"/";
preg_match($searchStr2, $resCalUrl, $matches2);
$pd = $matches2[1];
$username = "my_yahoo_username";
$password = "my_yahoo_password";
//$mdPassword = md5($password);
//$finalPass = $mdPassword . $challenge;
//$hash = md5($finalPass);
//$finalHash = urlencode($hash);
$done = urlencode("http://calendar.yahoo.com");
$postFields = ".tries=1&.src=fpctx&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.bypass=&.partner=&.u=".$u."&.v=0&.challenge=".$challenge."&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.chkP=Y&.done=http%3A%2F%2Fwww.yahoo.com%2F&.pd=fpctx_ver%3D0%26c%3D%26ivt%3D%26sg%3D&pad=3&aad=3&login=".$username."&passwd=".$password."&.save=&passwd_raw=";
//$postFields = ".tries=1&.src=&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.bypass=&.partner=&.u=".$u."&.v=0&.challenge=".$challenge."&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=1&.chkP=Y&.pd=".$pd."&pad=6&aad=6&.persistent=&.save=1&login=".$username."&passwd=".$password."&.done=".$done;
curl_setopt($ch, curlOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_REFERER, $loginUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true)开发者_高级运维;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies_new.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies_new.txt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$finalLoggedIn = curl_exec($ch);
echo $finalLoggedIn;
?>
Though I tried using the password hash, i.e. combining the dynamic channel value of Yahoo with my password, but it didn't work out that way either.
Looks like no one has responded to this. And I know its an annoying issue.
I see where you are setting the cookie name/jar... but not opening the file to read/write. The solution was more than likley to use fopen()
on the file first...
$cookie_jar_file = "my_cookies_new.txt";
$fp = fopen($cookie_jar_file, "w");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar_file);
Also:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
Do not forget to close the opened file and delete it at the end of your script.
fclose($fp);
unlink($cookie_jar_file);
Hope this helps someone. Here is how I would do it though:
1: Get the info from Yahoo that wou will need.
// *********************************************************************************************
// GET CHALLENGE
// *********************************************************************************************
$cs = curl_init();
curl_setopt($cs, CURLOPT_USERAGENT, $agent);
curl_setopt($cs, CURLOPT_REFERER, "http://m.google.com/");
curl_setopt($cs, CURLOPT_URL, "https://login.yahoo.com/config/login_verify2?");
// curl_setopt($cs, CURLOPT_POSTFIELDS, "login=".$id."&passwd=".$pass."&.src=&.tries=5&.bypass=&.partner=&.md5=&.hash=&.intl=us&.tries=1&.challenge=ydKtXwwZarNeRMeAufKa56.oJqaO&.u=dmvmk8p231bpr&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.v=0&.chkP=N&.last=&.done=http://m.yahoo.com/w/ygo-mail/");
curl_setopt($cs, CURLOPT_COOKIEJAR, $cookie_jar_file);
curl_setopt($cs, CURLOPT_COOKIEFILE, $cookie_jar_file);
curl_setopt($cs, CURLOPT_HEADER, 1);
//curl_setopt($cs, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cs, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cs, CURLOPT_TIMEOUT, 30);
curl_setopt($cs, CURLOPT_CONNECTTIMEOUT,5);
// curl_setopt($cs, CURLOPT_HTTPPROXYTUNNEL, 1);
// curl_setopt($cs, CURLOPT_PROXY, $proxy);
curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, 2);
$y_login_page = curl_exec($cs);
//print $y_login_page;
curl_close($cs);
$proceed = true;
if(preg_match("/type=\"hidden\" name=\".challenge\" value=\"(.*?)\"/", $y_login_page, $challenge)){
print "<PRE>";
print_r($challenge);
print"</PRE>";
print "FOUND CHALLENGE STRING<BR>";
$chal = $challenge[1];
}
else {
print "\nNO CHALLENGE STRING";
$proceed = false;
}
if(preg_match("/type=\"hidden\" name=\".u\" value=\"(.*?)\"/", $y_login_page, $array_u)){
print "<PRE>";
print_r($array_u);
print"</PRE>";
print "FOUND .U STRING<BR>";
$u = $array_u[1];
}
else {
print "\nNO U STRING";
$proceed = false;
}
if(preg_match("/type=\"hidden\" name=\".pd\" value=\"(.*?)\"/", $y_login_page, $array_pd)){
print "<PRE>";
print_r($array_pd);
print"</PRE>";
print "FOUND .PD STRING<BR>";
$pd = $array_pd[1];
}
if(preg_match("/ts=(.*?)&/", $y_login_page, $array_ts)){
print "<PRE>";
print_r($array_ts);
print"</PRE>";
print "FOUND TIME STAMP STRING<BR>";
$xts = $array_ts[1];
}
Then Use The Info You Just Collected With Your Username And Pass To Login.
// *********************************************************************************************
// CURL SESSION #1: AUTHENTICATE THE USER ID ON YAHOO'S SERVER
// *********************************************************************************************
$cs = curl_init();
curl_setopt($cs, CURLOPT_USERAGENT, $agent);
curl_setopt($cs, CURLOPT_REFERER, "http://m.google.com/");
curl_setopt($cs, CURLOPT_URL, "http://login.yahoo.com/config/login?");//http://login.yahoo.com/config/login?
curl_setopt($cs, CURLOPT_POSTFIELDS, ".tries=1&.src=&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.lang=en-US&.bypass=&.partner=&.u=".$u."&.v=0&.challenge=".$chal."&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.chkP=Y&.done=http://calendar.yahoo.com&.pd=".$pd."&.ws=0&.cp=0&nr=0&pad=5&aad=5&login=".$id."&passwd=".$pass."&.persistent=y&.save=");
curl_setopt($cs, CURLOPT_COOKIEJAR, $cookie_jar_file);
curl_setopt($cs, CURLOPT_COOKIEFILE, $cookie_jar_file);
curl_setopt($cs, CURLOPT_HEADER, 1);
curl_setopt($cs, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cs, CURLOPT_TIMEOUT, 30);
curl_setopt($cs, CURLOPT_CONNECTTIMEOUT,5);
//curl_setopt($cs, CURLOPT_HTTPPROXYTUNNEL, 1);
//curl_setopt($cs, CURLOPT_PROXY, $proxy);
$y_login_page = curl_exec($cs);
print $y_login_page;
The PostFeilds here has a field named "done".
Set that to done=http://calendar.yahoo.com
Or wherever you want to go after logging in.
精彩评论