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

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

WBOY
WBOY원래의
2016-05-25 16:45:381949검색

我们使用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으로 문의하세요.