首页 php教程 php手册 php利用CURL函数登入163邮箱并获取自己的通讯录

php利用CURL函数登入163邮箱并获取自己的通讯录

May 25, 2016 pm 04:45 PM
curl函数

我们使用phpmailer登录邮件发邮件也是使用了curl原理来实现模仿用户(www.phprm.com)发邮件了,今天看了两个利用CURL函数登入163邮箱并获取自己的通讯录的例子.

学习了一些CURL的基础知识并做了这个示例,关于CURL的知识可以从php的官网上查看,点击查看.

示例代码调试方式:把$userName和$password换成自己163邮箱的用户名和密码即可.

注意:用户名和密码一定要正确,否则报错,没有做容错处理,示例代码如下:

<?php
//==================账号信息==================
//用户名
$userName = &#39;xxxxxxxx&#39;;
//密码
$password = &#39;xxxxxxxx&#39;;
//邮箱
$email = $userName . &#39;@163.com&#39;;
//==================登录==================
//登录地址(登录地址并不是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 = &#39;&#39;;
foreach ($postArray as $key => $value) {
    $postString.= "{$key}={$value}&";
}
$postString = trim($postString, &#39;&&#39;);
//初始化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(&#39;temp1.txt&#39;, $html);
//分割头文件和内容
list($head, $content) = explode("\r\n\r\n", $html, 2);
//把获取到的数据写入文件中以便查看
//file_put_contents(&#39;temp2.txt&#39;, $head);
//file_put_contents(&#39;temp3.txt&#39;, $content);
$head = explode("\r\n", $head);
//获取cookie信息
$cookieString = &#39;&#39;;
foreach ($head as $value) {
    if (stripos($value, "Set-Cookie: ") !== false) {
        $cookieString.= str_replace("Set-Cookie: ", "", $value);
    }
}
//从content里分析出sid值(读取通讯录信息的参数)
$startString = &#39;top.location.href = "&#39;;
$endString = &#39;";</script>&#39;;
$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[&#39;query&#39;], $queryVals);
$sid = $queryVals[&#39;sid&#39;];
//==================读取邮箱==================
//读取邮箱地址
$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, &#39;order=[{"field":"N","desc":"false"}]&#39;);
//注意这里要设置从登录操作中获取的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(&#39;temp4.txt&#39;, $content);
//关闭一个CURL会话,并释放资源
curl_close($curl);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r(json_decode($content, true));
echo &#39;
登录后复制
'; ?>

例子二,这个更高级一些可以输入信息,代码如下:

<html> 
<head> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/> 
<title>登陆163邮箱</title> 
</head> 
<body> 
<form id="login" action="mail.php" method="POST"> 
<label for="username">用户名:</label><input id="username" name="username" type="text" /> 
<label for="password">密码:</label><input id="password" name="password" type="password" /> 
<input type="submit" value="取得通讯录列表" /> 
</form> 
</body> 
</html>
登录后复制

mail.php,代码如下:

<?php
define("COOKIEJAR", ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "cookie.txt");
if (!isset($_POST[&#39;username&#39;]) || !isset($_POST[&#39;username&#39;])) {
    echo &#39;ERROR QUEST&#39;;
    exit();
}
$username = $_POST[&#39;username&#39;];
$pos = strrpos($username, &#39;@&#39;);
if ($pos === false) {
    $username.= "@163.com";
}
$password = $_POST[&#39;password&#39;];
//登陆163邮箱,获得登陆后的cookie
$options = array(
    CURLOPT_URL => "https://reg.163.com/logins.jsp",
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => "username=" . $username . "&password=" . $password,
    CURLOPT_COOKIEJAR => COOKIEJAR,
);
curl_quest($options);
//利用上一步获得的cookie进一步取得cookie
$options2 = array(
    CURLOPT_URL => "http://fm163.163.com/coremail/fcg/ntesdoor2?verifycookie=1&lightweight=1",
    CURLOPT_COOKIEFILE => COOKIEJAR,
    CURLOPT_COOKIEJAR => COOKIEJAR,
);
curl_quest($options2);
//分析cookie文件,取得coremail.
$cookiefile = file_get_contents(COOKIEJAR);
preg_match(&#39;|Coremail.*?%(\S*)|&#39;, $cookiefile, $sid);
//发送获得通讯录xml请求.
$postStr3 = &#39;<?xml version="1.0"<object><array name="items"><object><string name="func">pab:searchContacts</string 
><object name="var"><array name="order"><object><string name="field">FN</string><boolean name="ignoreCase" 
>true</boolean></object></array></object></object><object><string name="func">user:getSignatures</string 
></object><object><string name="func">pab:getAllGroups</string></object></array></object>&#39;;
$options3 = array(
    CURLOPT_URL => &#39;http://eg1a120.mail.163.com/a/s?sid=&#39; . $sid[1] . &#39;&func=global:sequential&#39;,
    CURLOPT_HTTPHEADER => array(
        &#39;Content-Type: application/xml&#39;
    ) ,
    CURLOPT_POST => 1,
    CURLOPT_COOKIEFILE => COOKIEJAR,
    CURLOPT_POSTFIELDS => $postStr3,
    CURLOPT_REFERER => &#39;http://eg1a120.mail.163.com/a/f/js3/0811050934/index_v12.htm&#39;,
);
$mailsxml = curl_quest($options3, true);
//输出获得的通讯录xml
header("Content-Type: application/xml");
echo $mailsxml;
function curl_quest($ops, $return = false) {
    $ch = curl_init();
    curl_setopt_array($ch, $ops);
    ob_start();
    curl_exec($ch);
    if ($return) {
        $content = ob_get_contents();
    }
    curl_close($ch);
    ob_end_clean();
    if ($return) {
        return $content;
    }
}
?>
登录后复制


文章地址:

转载随意^^请带上本文地址!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)