加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2023-06-07 13:21:40 所属栏目:PHP教程 来源:
导读:我们使用phpmailer登录邮件发邮件也是使用了curl原理来实现模仿用户(www.Cuoxin.com)发邮件了,今天看了两个利用CURL函数登入163邮箱并获取自己的通讯录的例子.

学习了一些CURL的基础知识并做了这个示例,关于CURL的
我们使用phpmailer登录邮件发邮件也是使用了curl原理来实现模仿用户(www.Cuoxin.com)发邮件了,今天看了两个利用CURL函数登入163邮箱并获取自己的通讯录的例子.

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

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

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

<?php 
//==================账号信息================== 
//用户名 
$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 = '";</script>'; 
$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); 
(www.Cuoxin.com)//关闭一个CURL会话,并释放资源 
curl_close($curl); 
echo '<pre>'; 
print_r(json_decode($content, true)); 
echo '</pre>'; 
例子二,这个更高级一些可以输入信息,代码如下:

<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['username']) || !isset($_POST['username']) ){ 
        echo 'ERROR QUEST'; 
        exit(); 
    } 
     
    $username = $_POST['username']; 
    $pos = strrpos($username,'@'); 
    if($pos === false){ 
        $username .= "@163.com"; 
    } 
    $password = $_POST['password']; 
     
    //登陆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('|Coremail.*?%(\S*)|',$cookiefile,$sid); 
     
    //发送获得通讯录xml请求. 
    $postStr3 = '<?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>'; 
    $options3 = array( 
       CURLOPT_URL => 'http://eg1a120.mail.163.com/a/s?sid='.$sid[1].'&func=global:sequential', 
       CURLOPT_HTTPHEADER => array('Content-Type: application/xml'), 
      CURLOPT_POST => 1, 
      CURLOPT_COOKIEFILE => COOKIEJAR, 
      CURLOPT_POSTFIELDS => $postStr3, 
      CURLOPT_REFERER => 'http://eg1a120.mail.163.com/a/f/js3/0811050934/index_v12.htm', 
    ); 
    $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; 
        } 
    } 
 
?> 

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章