php curlメールアドレスsohuを取得

3175 ワード

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 protected]', '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;

    }



}

?>