12 May, 2010

Yahoo login using curl

Following code can be useful if you are trying to sync your custom php calendar with yahoo calendar using curl. Please check php manual for detailed description about setting curl options. The script for login to yahoo using curl in php is simple and short and can be of help at times for developers like me.

$serviceUrl = "http://calendar.yahoo.com/";
$authUrl = "http://login.yahoo.com/config/login?";
$userAgent = "YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; http://search.yahoo.com/)";
$referer = "http://my.yahoo.com";
$login = "yourusername";
$password = "yourpassword";
$numPostData = 22;
$cookieFileJar = "cookie.txt";
$cookie = 0;
$postData = "login=$login&passwd=$password&.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=" . $serviceUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

// Set the referrer
curl_setopt($ch, CURLOPT_REFERER, $referer);

// Set the authentication url
curl_setopt($ch, CURLOPT_URL, $authUrl);

// Set number of post fields
curl_setopt($ch, CURLOPT_POST, $numPostData);

//Set post data in key=value pair such as login=yourusername
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

//Set filename for storing cookie information
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFileJar);

//Set ffilename for checking the stored cookie information
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);

//Set option for cookie
curl_setopt($ch, CURLOPT_COOKIE, $cookie);

//set this to output the result as string and not output directly ot browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//set this value to 1 if you want to redirect to the url you provided as service url
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

//Set this option if you do not want to verify ssl
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

//set this option if you do not want to verify peer's certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

//now execute the curl
$res = curl_exec($ch);

//check if the username and password is valid
if ((preg_match("/invalid/i", $res)) || (preg_match("/not yet taken/i", $res))) {
echo "Invalid Login";
}
else {
//if CURLOPT_FOLLOWLOCATION is set to 1 then after logging in successfully user is directed to url that is specified as service url
echo "Logged In";
}
?>

3 comments:

Anonymous said...

This is great - but it doesn't seem to work. When I use this code and then try to follow to the next page by setting curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1), the next page still asks me to sign in and doesn't load any of my profile specific data...

Anonymous said...

i modified the above code a bit . but still i cant make it work. the problem is yaho is implementing a captcha challange for this kind of automated headers. do you have any idea ho to make it work again without alreting the captcha challange ?

Sudhir Bastakoti said...

Hi,
Thanks for the comment, the code was working fine previously, i'm also checking to bypass the captcha, will post a working version