02 April, 2011

Login to Yahoo mobile using curl

Once i had to login to yahoo mobile for some calendar events manipulation, but since yahoo does not have any api for doing such stuffs so thought of giving it a try using curl. After some tests and hit & triali was able to login yo yahoo mobile
using curl and do my necessary calendar events manipulation.
Below is the code snippet for logging in to yahoo mobile using curl.

$mCookie = "mcookie.cookie";
$reffer = "http://m.yahoo.com/calendar";
$mUrl = "http://m.yahoo.com/calendar";
$ch = curl_init();
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt($ch, CURLOPT_URL, $mUrl);
curl_setopt($ch, CURLOPT_GET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $mCookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $mCookie);
$mRes = curl_exec($ch);

//get the hidden fields

preg_match_all ( "/id=\"LoginModel\" action=\"(.*)\"/", $mRes, $formActionArr);
preg_match_all ( "/name=\"_authurl\" value=\"(.*?)\"/", $mRes, $authUrlArr);
preg_match_all ( "/name=\"_done\" value=\"(.*?)\"/", $mRes, $doneArr);
preg_match_all ( "/name=\"_ts\" value=\"(.*?)\"/", $mRes, $tsArr);
preg_match_all ( "/name=\"_crumb\" value=\"(.*?)\"/", $mRes, $crumbArr);

$authUrl = $authUrlArr[1][0];
$doneUrl = $doneArr[1][0];
$ts = $tsArr[1][0];
$crumb = $crumbArr[1][0];
$signIn = urlencode("Sign In");
$username = "yahoo_username";
$password = "yahoo_password";
$loginUrl = "https://mlogin.yahoo.com/w/login/auth?.ts=".$ts."&_httpHost=m.yahoo.com&.intl=NP&.lang=en";
$postFields = "_authurl=".$authUrl."&_done=".$doneUrl."&_sig=&_src=&_ts=".$ts."&_crumb=".$crumb."&_pc=&_send_userhash=0&_appdata=&_partner_ts=&_is_ysid=&_page=secure&_next=&id=".$username."&password=".$password."&__submit=".$signIn;

//now post the hidden fields
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $mCookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $mCookie);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
$postRes = curl_exec($ch);
echo $postRes;
//we are logged in now
?>

Thats it, Hope this helps someone.