php curl 获取 邮箱通讯录 sohu

2012-3-24 经测试可正常使用

<?php
error_reporting(0);
define("COOKIEJAR", tempnam(ini_get("upload_tmp_dir"), "cookie"));   //定义COOKIES存放的路径,要有操作的权限
define("TIMEOUT", 10000); //超时设定
/**
 * author : 郭钱
 */
$contacts = new sohu();

$lists = $contacts->getcontacts('email@sohu.com', 'password', $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/86/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;
    }

}
?> 

 

posted @ 2012-03-24 18:26  前望  阅读(745)  评论(1编辑  收藏  举报