//==================账号信息==================
//用户名
$userName = 'xxxxxxxx';
//密码
$password = 'xxxxxxxx';
//邮箱
$email = $userName . '@163.com';
//==================登录==================
//登录地址(登录地址并不是form表单设置的地址,通过js修改了form的action属性,需要查看登录页面源码才能发现)
$loginUrl = "https://ssl.mail.163.com/entry/coremail/fcg/ntesdoor2?df=mail163_letter&from=web&funcid=loginone&iframe=1&language=-1&passtype=1&product=mail163&net=n&style=-1&race=-2_56_-2_hz&uid={$email}";
//登录时发送的post数据(查看form表单,注意有隐藏域)
$postArray = array(
"url2" => "http://mail.163.com/errorpage/error163.htm",
"savelogin" => 0, "username" => trim($userName), "password" => $password,
);
$postString = '';
foreach($postArray as $key => $value){
$postString .= "{$key}={$value}&";
}
$postString = trim($postString, '&');
//初始化CURL对象
$curl = curl_init();
//设置请求地址
curl_setopt($curl, CURLOPT_URL, $loginUrl);
//禁用后CURL将终止从服务端进行验证
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
//启用时将获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
//启用时会将头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, TRUE);
//设置POST参数
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
//执行给定的CURL会话
//成功时返回 TRUE,失败时返回 FALSE
//然而,如果 CURLOPT_RETURNTRANSFER选项被设置,函数执行成功时会返回执行的结果,失败时返回 FALSE
$html = curl_exec($curl);
//把获取到的数据写入文件中以便查看
//file_put_contents('temp1.txt', $html);
//分割头文件和内容
list($head, $content) = explode("\r\n\r\n", $html, 2);
//把获取到的数据写入文件中以便查看
//file_put_contents('temp2.txt', $head);
//file_put_contents('temp3.txt', $content);
$head = explode("\r\n", $head);
//获取cookie信息
$cookieString = '';
foreach ($head as $value){
if(stripos($value, "Set-Cookie: ") !== false){
$cookieString .= str_replace("Set-Cookie: ", "", $value);
}
}
//从content里分析出sid值(读取通讯录信息的参数)
$startString = 'top.location.href = "';
$endString = '";';
$start = strpos($content, $startString);
$end = strpos($content, $endString);
$tempUrl = substr($content, $start + strlen($startString), $end - $start - strlen($startString));
$tempUrlVals = parse_url($tempUrl);
parse_str($tempUrlVals['query'], $queryVals);
$sid = $queryVals['sid'];
//==================读取邮箱==================
//读取邮箱地址
$readUrl = "http://twebmail.mail.163.com/contacts/call.do?uid={$email}&sid={$sid}&from=webmail&cmd=newapi.getContacts&vcardver=3.0&ctype=all&attachinfos=yellowpage";
//设置请求地址
curl_setopt($curl, CURLOPT_URL, $readUrl);
//设置POST参数
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'order=[{"field":"N","desc":"false"}]');
//注意这里要设置从登录操作中获取的cookie
curl_setopt($curl, CURLOPT_COOKIE, $cookieString);
//禁用头文件输出
curl_setopt($curl, CURLOPT_HEADER, FALSE);
//执行给定的CURL会话
//成功时返回 TRUE,失败时返回 FALSE
//然而,如果 CURLOPT_RETURNTRANSFER选项被设置,函数执行成功时会返回执行的结果,失败时返回 FALSE
$content = curl_exec($curl);
//把获取到的数据写入文件中以便查看
//file_put_contents('temp4.txt', $content);
//关闭一个CURL会话,并释放资源
curl_close($curl);
echo ' ';
print_r(json_decode($content, true));
echo ' ';
|