无涉 2012-10-15 15:53:35 28611次浏览 4条回复 0 0 0

soho

<?php
error_reporting(0);
define("COOKIEJAR", tempnam(ini_get("upload_tmp_dir"), "cookie"));   //定义COOKIES存放的路径,要有操作的权限
define("TIMEOUT", 10000); //超时设定

$contacts = new sohu();
 
$lists = $contacts->getcontacts('testwo@sohu.com', '1234567890', $result);
var_dump($lists);
 
class sohu {
 
    function checklogin($user, $password) {
        $ch = curl_init( );
        $url = "http://passport.sohu.com/sso/login.jsp";
        $url = $url . "?userid=" . urlencode($user);
        $url = $url . "&password=" . md5($password);
        $url = $url . "&appid=1000&persistentcookie=0&s=" . time() . "&b=1&w=1024&pwdtype=1";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
        curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
        ob_start( );
        curl_exec($ch);
        $contents = ob_get_contents( );
        ob_end_clean( );
        curl_close($ch);
        if (strpos($contents, "success") === false) {
            return 0;
        }
        return 1;
    }

    function getcontacts($user, $password, &$result) {
        if (!$this->checklogin($user, $password)) {
            return '登陆失败';
        }
        $cookies = array();
        $bRet = $this->readcookies(COOKIEJAR, $cookies);
        if (!$bRet && !$cookies['JSESSIONID']) {
            return '读取cookie失败';
        }
        $ch = curl_init( );
        curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
        curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
        curl_setopt($ch, CURLOPT_URL, "http://mail.sohu.com/bapp/22/main#addressList");
        ob_start( );
        curl_exec($ch);
        $content = ob_get_contents( );
        ob_end_clean( );
        curl_close($ch);
        $bRet = $this->_parsedata($content);
        return $bRet;
    }

    function _parsedata($content) {
        preg_match_all("/var addresses = '(.*)';/Umsi", $content, $data);
        $numList = json_decode($data[1][0], true);
        $contactList = array();
        foreach ($numList['contact'] as $val) {
            $obj['name'] = $val['nickname'];
            $obj['nickname'] = $val['pinyin'];
            $obj['email'] = $val['email'];
            if ($obj['nickname'] == "") {
                $obj['nickname'] = $$obj['name'];
            }
            $contactList[] = $obj;
        }
        if (!$contactList) {
            return CONTACT_ERROR;
        } else {
            return $contactList;
        }
    }

    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;
    }
 
}
?>
  • 回复于 2012-10-15 15:54 举报

    gmail

    <?php
    error_reporting(E_ALL);
    $username = "testwo@gmail.com";
    $password = "1234567890";
     
    $gmail = new mail_gmail();
    $result = $gmail->getAddressList($username, $password);
     
    $data = json_decode($result, true);
    $list = array();
    foreach ($data['feed']['entry'] as $entry) {
        $list['username'] = $entry['title']['$t'];
        $list['email']= $entry['gd$email'][0]['address'];
        $lists[] = $list;
    }
     
    var_dump($lists);
     
     
    class mail_gmail {
        function getAddressList($username, $password) {
            $login_url = "https://www.google.com/accounts/ClientLogin";
            $fields = array(
                'Email' => $username,
                'Passwd' => $password,
                'service' => 'cp', // <== contact list service code
                'source' => 'test-google-contact-grabber',
                'accountType' => 'GOOGLE',
            );
     
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $login_url);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($curl);
     
            $returns = array();
            foreach (explode("\n", $result) as $line) {
                $line = trim($line);
                if (!$line)
                    continue;
                list($k, $v) = explode("=", $line, 2);
                $returns[$k] = $v;
            }
            curl_close($curl);
     
    // step 2: grab the contact list
            $feed_url = "http://www.google.com/m8/feeds/contacts/$username/full?alt=json&max-results=250";
            $header = array(
                'Authorization: GoogleLogin auth=' . $returns['Auth'],
            );
     
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $feed_url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     
            $result = curl_exec($curl);
            curl_close($curl);
            return $result;
        }
     
    }
    ?>
    
  • 回复于 2012-10-15 15:55 举报

    126

    <?php
    error_reporting(0);
    define("COOKIEJAR", tempnam("./assets/tmp", "c1_"));
    define('TIMEOUT', 60);
    $e126=new mail_126();
    $lists = $e126->getAddressList('test@126.com', 'test');
    var_dump($lists);
     
    class mail_126 {
     
        public function login($username, $password) {
            //第一步:初步登陆
            $cookies = array();
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     
            curl_setopt($ch, CURLOPT_URL, "https://reg.163.com/logins.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26verifycookie%3D1%26language%3D0%26style%3D-1");
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, "username=" . $username . "&password=" . $password);
     
            curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $str = curl_exec($ch);
            file_put_contents('./temp/126result.txt', $str);        //需要创建文件
            curl_close($ch);
     
            //获取redirect_url跳转地址,可以从126result.txt中查看,通过正则在$str返回流中匹配该地址
            preg_match("/replace\(\"(.*?)\"\)\;/", $str, $mtitle);
            $_url1 = $mtitle[1];
     
            //file_put_contents('./126resulturl.txt', $redirect_url);
            //第二步:再次跳转到到上面$_url1
            $ch = curl_init($_url1);
     
            curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
            curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            $str2 = curl_exec($ch);
            curl_close($ch);
            if (strpos($str2, "安全退出") !== false) {
                return 0;
            }
            return 1;
        }
     
        /**
         * 获取邮箱通讯录-地址
         * @param $user
         * @param $password
         * @param $result
         * @return array
         */
        public function getAddressList($username, $password) {
            if (!$this->login($username, $password)) {
                return 0;
            }
     
            $header = $this->_getheader($username);
            if (!$header['sid']) {
                return 0;
            }
     
            //测试找出sid(很重要)和host
            file_put_contents('./temp/host.txt', $header['host']);
            file_put_contents('./temp/sid.txt', $header['sid']);
            //开始进入模拟抓取
            $ch = curl_init();
            //curl_setopt($ch, CURLOPT_URL, "http://" . $header['host'] . "/a/s?sid=" . $header['sid'] . "&func=global:sequential");
            //参考联系人下载地址
            curl_setopt($ch, CURLOPT_URL, "http://" . $header['host'] . "/js4/s?sid=" . $header['sid'] . "&func=global:sequential");
            curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));
            $str = "<?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>";
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
            curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
            ob_start();
            $data = curl_exec($ch);
            $contents = ob_get_contents();
     
            ob_end_clean();
            curl_close($ch);
     
            //get mail list from the page information username && emailaddress
            preg_match_all("/<string\s*name=\"EMAIL;PREF\">(.*)<\/string>/Umsi", $contents, $mails);
            preg_match_all("/<string\s*name=\"FN\">(.*)<\/string>/Umsi", $contents, $names);
            $users = array();
            foreach ($names[1] as $k => $user) {
                //$user = iconv($user,'utf-8','gb2312');
                $users['username'] = $user;
                $users['email'] = $mails[1][$k];
                $lists[] = $users;
            }
            if (!$lists) {
                return '您的邮箱中尚未有联系人';
            }
     
            return $lists;
        }
     
        /**
         * Get Header info
         */
        private function _getheader($username) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://entry.mail.126.com/cgi/ntesdoor?hid=10010102&lightweight=1&verifycookie=1&language=0&style=-1&username=" . $username . "@126.com");
            curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);  //当前使用的cookie
            curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);   //服务器返回的新cookie
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_NOBODY, true);
            $content = curl_exec($ch);
     
            preg_match_all('/Location:\s*(.*?)\r\n/i', $content, $regs);
            $refer = $regs[1][0];
            preg_match_all('/http\:\/\/(.*?)\//i', $refer, $regs);
            $host = $regs[1][0];
            preg_match_all("/sid=(.*)/i", $refer, $regs);
            $sid = $regs[1][0];
     
            curl_close($ch);
            return array('sid' => $sid, 'refer' => $refer, 'host' => $host);
        }
     
    }
    ?>
    
  • 回复于 2012-10-15 15:57 举报

    163

    <?php
         error_reporting(0);
         
         //邮箱用户名(不带@163.com后缀的)
         $user       = 'test';
         //邮箱密码
         $pass       = 'test';
         //目标邮箱
         //$mail_addr = [email]uenucom@163.com[/email]';
         
         //登陆
         $url            = 'http://reg.163.com/logins.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1';
    
         $ch             = curl_init($url);
         //创建一个用于存放cookie信息的临时文件
         $cookie         = tempnam('.','~');
         $referer_login = 'http://mail.163.com';
         //返回结果存放在变量中,而不是默认的直接输出
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_HEADER, true);
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
         curl_setopt($ch, CURLOPT_POST, true);
         
         curl_setopt($ch, CURLOPT_REFERER, $referer_login);
         
         $fields_post = array(
                 'username'=> $user,
                 'password'=> $pass,
                 'verifycookie'=>1,
                 'style'=>-1,
                 'product'=> 'mail163',
                 'selType'=>-1,
                 'secure'=>'on'
         );
         
         $headers_login = array(
             'User-Agent'        => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0',
             'Referer'           => 'http://www.163.com'
         );
         
         $fields_string = '';
         
         foreach($fields_post as $key => $value)
         {
             $fields_string .= $key . '=' . $value . '&';
         }
         
         $fields_string = rtrim($fields_string , '&');
         
         curl_setopt($ch, CURLOPT_COOKIESESSION, true);
         //关闭连接时,将服务器端返回的cookie保存在以下文件中
         curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login);
         curl_setopt($ch, CURLOPT_POST, count($fields_string));
         curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
         
         $result= curl_exec($ch);
         curl_close($ch);
         
         
         //跳转
         $url='http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=-1&username=' . $user;
         
         $ch = curl_init($url);
         
         $headers = array(
             'User-Agent'        => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0'
         );
         
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_HEADER, true);
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
         //将之前保存的cookie信息,一起发送到服务器端
         curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
         curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
         $result = curl_exec($ch);
         curl_close($ch);
         
         //取得sid
         preg_match('/sid=[^\"].*/', $result, $location);
         $sid = substr($location[0], 4, -1);
         file_put_contents('./result.txt', $sid);
         
         
         //通讯录地址
         $url='http://g4a30.mail.163.com/jy3/address/addrlist.jsp?sid='.$sid.'&gid=all';
         $ch = curl_init($url);
         
         $headers = array(
             'User-Agent'        => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0'
         );
         
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_HEADER, true);
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
         curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
         curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
         $result = curl_exec($ch);
         curl_close($ch);
         file_put_contents('./result2.txt', $result);
         unlink($cookie);
         
         //开始抓取内容
         preg_match_all('/<td class="Ibx_Td_addrName"><a[^>]*>(.*?)<\/a><\/td><td class="Ibx_Td_addrEmail"><a[^>]*>(.*?)<\/a><\/td>/i', $result,$infos,PREG_SET_ORDER);
         //1:姓名2:邮箱
         print_r($infos);
     
    ?>
    
  • 回复于 2012-10-16 09:05 举报

    没试过,先顶一个,谢谢楼主的分享精神!

您需要登录后才可以回复。登录 | 立即注册