2012 09 17 测试成功 模拟qq登陆 抓取联系人

<?php
/**
* 获得qq邮箱通讯录列表 -- mail_qq.class.php
*/
require_once 'phpQuery/phpQuery.php';
define("USERAGENT", $_SERVER['HTTP_USER_AGENT']);
define("COOKIEJAR", tempnam('\tmp', "c"));
define("TIMEOUT", 10);

class mail_qq {

var $host = "";

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;
}

function checklogin($user, $password) {
if (empty($user) || empty($password)) {
return 0;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://w.mail.qq.com/cgi-bin/loginpage?f=xhtml");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
$contents = curl_exec($ch);
curl_close($ch);
if (!preg_match("/ssl_edition=(.*)\; Domain/", $contents, $matches)) {
return 0;
}
$this->host = $matches[1];
//echo $this->host;
return 1;
}

function getcontacts($user, $password) {
if (!$this->checklogin($user, $password)) {
return 0;
}
$cookies = array();
// $bRet = $this->readcookies(COOKIEJAR, $cookies);
// if (!$bRet) {
// return 0;
// }
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_REFERER, "http://w.mail.qq.com/cgi-bin/loginpage?f=xhtml");
curl_setopt( $ch, CURLOPT_COOKIEFILE, COOKIEJAR );
curl_setopt( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt($ch, CURLOPT_URL, "http://w43.mail.qq.com/cgi-bin/login");
curl_setopt($ch, CURLOPT_POSTFIELDS, "device=&f=xhtml&tfcont=&uin=" . $user . "&aliastype=@qq.com&pwd=" . $password . "&btlogin=登录&mss=");
curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT);
$contents = curl_exec($ch);
curl_close($ch);
preg_match('/today\?sid\=(.*)&/i', $contents, $matches);
if (empty($matches[1])) {
return 0;
}
$sid = $matches[1];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);

curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT);
curl_setopt($ch, CURLOPT_URL, "http://w43.mail.qq.com/cgi-bin/addr_listall?sid=" . $sid . "&flag=star&s=search&folderid=all&pagesize=10&from=today&fun=slock&page=0&topmails=0&t=addr_listall&loc=today,,,158");
$content = curl_exec($ch);
curl_close($ch);
unlink(COOKIEJAR);
return $content;
}
}

 

$contact = new mail_qq();
$content = $contact->getcontacts($username, $password);
原文地址:https://www.cnblogs.com/lingmou/p/2689096.html