Link to home
Start Free TrialLog in
Avatar of csteimonts
csteimonts

asked on

Login To Yahoo With cURL

I Am trying to login to my yahoo account with cURL in order to pull data down from my account, but I am having a heck of a time with authentication.  I keep getting redirected to the login page with leads me to believe it has something to do with the cookies but I'm not entirely sure.  This run from a php script which will be run by cron.  Here is my current code.  thank you so much for your help!
<?
 
$c=curl_init();
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12");
curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($c, CURLOPT_HEADER , TRUE);
curl_setopt($c, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
//curl_setopt($c, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');	
curl_setopt($c, CURLOPT_URL, "https://login.yahoo.com/config/login");
$html=curl_exec($c);
 
////////////
// Get URL
//
preg_match("/action=\"([\S]*)\" /", $html, $action);
$url = $action[1];
 
///////////////////////
// Get Params to pass
//
preg_match_all("/<input type=\"hidden\" name=\"(.*)\" value=\"(.*)\">/", $html, $hidden_vars);
 
$params = array();
 
foreach($hidden_vars[1] as $index => $name)
	$params[$name] = $hidden_vars[2][$index];
 
$params['.done'] 		= "http://basketball.fantasysports.yahoo.com/nba/38554";
$params['.persistent'] 	= "y";
$params['.save'] 		= "Sign In";
$params['login'] 		= $USER;
$params['passwd'] 		= $PASS;
 
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, implode("&", $params));
$html=curl_exec($c);
curl_close($c);
 
print($html);
 
?>

Open in new window

Avatar of Arikael
Arikael
Flag of Switzerland image

try setting
CURLOPT_SSL_VERIFYPEER to false
If you set it to false Curl won't verify the peer's certificate (see here: http://www.php.net/manual/en/function.curl-setopt.php)
Avatar of csteimonts
csteimonts

ASKER

Figured it out, incase anyone else is trying to get it, here is the code i used
<?
 
$c=curl_init();
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_COOKIEJAR, "./cookie.txt");
curl_setopt($c, CURLOPT_COOKIEFILE, "./cookie.txt");
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($c, CURLOPT_URL, "https://login.yahoo.com/config/login?rl=1");
$html=curl_exec($c);
curl_close($c);
 
////////////
// Get URL
//
preg_match("/action=\"([\S]*)\" /", $html, $action);
$url = $action[1];
 
///////////////////////
// Get Params to pass
//
preg_match_all("/<input type=\"hidden\" name=\"(.*)\" value=\"(.*)\">/", $html, $hidden_vars);
 
$params = array();
 
foreach($hidden_vars[1] as $index => $name)
	$params[$name] = $hidden_vars[2][$index];
 
$params['.done'] 		= "http://basketball.fantasysports.yahoo.com/nba/38554";
$params['.persistent'] 	= "y";
$params['.save'] 		= "Sign In";
$params['login'] 		= $USER;
$params['passwd'] 		= $PASS;
 
$c=curl_init();
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_COOKIEJAR, "./cookie.txt");
curl_setopt($c, CURLOPT_COOKIEFILE, "./cookie.txt");
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($c, CURLOPT_REFERER, "https://login.yahoo.com/config/login?rl=1");
$html=curl_exec($c);
curl_close($c);
 
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial