最近发现线上登录Yahoo邮箱取地址薄的代码不能用了,调试发现是登录方式变换了,特通过HttpFox关注,重构代码如下:
define("USERAGENT","Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)");
define("COOKIEJAR",tempnam("/tmp/cookie/","cookie"));
define("TIMEOUT",10);
define("REFER", "http://mail.cn.yahoo.com");
define("LOGIN_HOME", "http://mail.cn.yahoo.com");
define("LOGINURL", "https://edit.bjs.yahoo.com/config/login?");
........
/**
*get login parameters
*@return @mixed
*
*/
public function getParam()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, LOGIN_HOME);
curl_setopt($ch, CURLOPT_USERAGENT,USERAGENT);
curl_setopt($ch, CURLOPT_COOKIEJAR,COOKIEJAR);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
preg_match_all("/name=\".challenge\" value=\"(.*?)\"/", $contents, $arr_challenge);
$challenge = $arr_challenge[1][0];
preg_match_all("/name=.done value=\"(.*?)\"/", $contents, $arr_done);
$done = $arr_done[1][0];
return array('challenge'=>$challenge,
'done'=>$done);
}
/**
*
*@return boolean
*/
public function checkLogin($user,$password)
{
$params = $this->getParam();
$done = $params['done'];
$challenge = $params['challenge'];
$postfields = ".intl=cn&.done={$done}&.src=ym&.cnrid=ymhp_&challenge=${challenge}&login={$user}&passwd={$password}&submit=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_REFERER, REFER);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT,USERAGENT);
curl_setopt($ch, CURLOPT_COOKIEJAR,COOKIEJAR);
curl_setopt($ch,CURLOPT_TIMEOUT,TIMEOUT);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postfields);
$r = curl_exec($ch);
curl_close($ch);
if(strpos($r,"Caps Lock")) {
return False;
} else {
return True;
}
}