作者:guitar402 | 来源:互联网 | 2024-11-03 11:50
本文介绍了一种利用PHPcURL库高效提取Sohu邮箱联系人列表的方法。通过设置错误报告级别、定义Cookie文件路径等关键步骤,确保了代码的稳定性和可靠性。经过实际测试,该方法在2012年3月24日被验证为有效,能够快速准确地获取联系人信息。此外,文章还提供了详细的代码示例和注意事项,帮助开发者更好地理解和应用这一技术。
2012-3-24 经测试可正常使用
getcontacts('email@sohu.com', 'password', $result);
var_dump($lists);
class sohu {
function checklogin($user, $password) {
$ch = curl_init( );
$url = "http://passport.sohu.com/sso/login.jsp";
$url = $url . "?userid=" . urlencode($user);
$url = $url . "&password=" . md5($password);
$url = $url . "&appid=1000&persistentCOOKIE=0&s=" . time() . "&b=1&w=1024&pwdtype=1";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
ob_start( );
curl_exec($ch);
$cOntents= ob_get_contents( );
ob_end_clean( );
curl_close($ch);
if (strpos($contents, "success") === false) {
return 0;
}
return 1;
}
function getcontacts($user, $password, &$result) {
if (!$this->checklogin($user, $password)) {
return '登陆失败';
}
$COOKIEs = array();
$bRet = $this->readCOOKIEs(COOKIEJAR, $COOKIEs);
if (!$bRet && !$COOKIEs['JSESSIONID']) {
return '读取COOKIE失败';
}
$ch = curl_init( );
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
curl_setopt($ch, CURLOPT_URL, "http://mail.sohu.com/bapp/86/main#addressList");
ob_start( );
curl_exec($ch);
$cOntent= ob_get_contents( );
ob_end_clean( );
curl_close($ch);
$bRet = $this->_parsedata($content);
return $bRet;
}
function _parsedata($content) {
preg_match_all("/var addresses = '(.*)';/Umsi", $content, $data);
$numList = json_decode($data[1][0], true);
$cOntactList= array();
foreach ($numList['contact'] as $val) {
$obj['name'] = $val['nickname'];
$obj['nickname'] = $val['pinyin'];
$obj['email'] = $val['email'];
if ($obj['nickname'] == "") {
$obj['nickname'] = $$obj['name'];
}
$contactList[] = $obj;
}
if (!$contactList) {
return CONTACT_ERROR;
} else {
return $contactList;
}
}
function readCOOKIEs($file, &$result) {
$fp = fopen($file, "r");
while (!feof($fp)) {
$buffer = fgets($fp, 4096);
$tmp = split("\t", $buffer);
$result[trim($tmp[5])] = trim($tmp[6]);
}
return 1;
}
}
?>