<?php
define( "COOKIEJAR", tempnam( ini_get( "upload_tmp_dir" ), "cookie" ) );   //定义COOKIES存放的路径,要有操作的权限
define( "TIMEOUT", 1000 ); //超时设定
/**
 * 获得sina邮箱通讯录列表 -- contactssina.class.php
 */class contactssina
{ var $host = ""; function checklogin( $user, $password )
{
if ( empty( $user ) || empty( $password ) )
{
return 0;
}
$ch = curl_init( );
curl_setopt( $ch, CURLOPT_REFERER, "http://mail.sina.com.cn/index.html" );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, USERAGENT );
curl_setopt( $ch, CURLOPT_TIMEOUT, TIMEOUT );
curl_setopt( $ch, CURLOPT_URL, "http://mail.sina.com.cn/cgi-bin/login.cgi" );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, "&logintype=uid&u=".urlencode( $user )."&psw=".$password );
$contents = curl_exec( $ch );
curl_close( $ch );
if ( !preg_match( "/Location: (.*)\\/cgi\\/index\\.php\\?check_time=(.*)\n/", $contents, $matches ) )
{
return 0;
}
$this->host = $matches[1];
return 1;
} function getcontacts( $user, $password, &$result )
{
if ( !$this->checklogin( $user, $password ) )
{
return '登陆失败';
}
$ch = curl_init( );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_USERAGENT, USERAGENT );
curl_setopt( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
curl_setopt( $ch, CURLOPT_TIMEOUT, TIMEOUT );
curl_setopt( $ch, CURLOPT_URL, "http://mail.sina.com.cn/cgi-bin/login.cgi" );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, "&logintype=uid&u=".urlencode( $user )."&psw=".$password );
curl_exec( $ch );
curl_close( $ch );
$cookies = array( );
$bRet = $this->readcookies( COOKIEJAR, $cookies );
if ( !$bRet && !$cookies['SWEBAPPSESSID'] )
{
return '读取cookie失败';
}
$ch = curl_init( );
curl_setopt( $ch, CURLOPT_COOKIEFILE, COOKIEJAR );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, TIMEOUT );
curl_setopt( $ch, CURLOPT_URL, $this->host."/classic/addr_member.php" );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, "&act=list&sort_item=letter&sort_type=desc" );
$content = curl_exec( $ch );
curl_close( $ch );
$bRet = $this->_parsedata( $content, $result );
return "结果:".$bRet;
} 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;
}

function _parsedata( $content, &$ar )
{
$ar = array( );
if ( !$content )
{
return '获取内容为空';
}
$data = json_decode( $content );
unset( $content );
foreach ( $data->data->contact as $value )
{
if ( preg_match_all( "/[a-z0-9_\\.\\-]+@[a-z0-9\\-]+\\.[a-z]{2,6}/i", $value->email, $matches ) )
{
$emails = array_unique( $matches[0] );
unset( $matches );
foreach ( $emails as $email )
{
$ar[$email] = $value->name;
}
}
}
return $ar;
}}$contactssina = new contactssina;$res = $contactssina->getcontacts('****@sina.com','***',$result);print_r($res);
?> 

解决方案 »

  1.   

    已经解决。<?php
        date_default_timezone_set('Asia/Shanghai');    class httpSina{        private $gurl='';
            private function vlogin($url,$name, $ps){
                //global $gurl;
                $cookie_jar = tempnam('./tmp','cookie');//在当前目录下生成一个随机文件名的临时文件
                $ch = curl_init($url); //初始化curl模块            $fields_post = array(
                                    'logintype'=>'uid',
                                    'u'=>$name,
                                    'domain'=>'sina.com',
                                    'psw'=>$ps,
                                    'btnloginfree'=>'%B5%C7+%C2%BC'
                );
                curl_setopt($ch,CURLOPT_URL,$url);//登录页地址
                curl_setopt($ch, CURLOPT_POST, 1);//post方式提交
                $fields_string = '';
                foreach($fields_post as $key => $value)
                {
                   $fields_string .= $key . '=' . $value . '&';
                }
                $fields_string = rtrim($fields_string , '&');
                curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);//要提交的内容
                //把返回$cookie_jar来的cookie信息保存在$cookie_jar文件中
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
                $headers_login = array(
                                     'Host'  => 'mail.sina.com.cn',
                                     'User-Agent'      => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2',
                                     'Accept'          => 'text/javascript, text/html, application/xml',
                                     'Accept_Language' => 'zh-cn,zh;q=0.5',
                                     'Accept_Encoding' => 'gzip,deflate',
                                     'Accept_Charset'  => 'GB2312,utf-8;q=0.7,*;q=0.7\r\n',
                                     'Keep-Alive'      => '115',
                                     'Connection'      => 'keep-alive',
                                     'X-Requested-With'=> 'XMLHttpRequest',
                                     'Content_Type'    => 'application/x-www-form-urlencoded; charset=UTF-8',
                                     'Referer'         => 'http://mail.sina.com.cn/'
                                 );
                //设定返回的数据是否自动显示
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                //设定是否显示头信息
                curl_setopt($ch, CURLOPT_HEADER, 1);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login);  
                //设定是否输出页面内容
                curl_setopt($ch, CURLOPT_NOBODY, 0);
                $result=curl_exec ($ch);
                //echo $result;
                preg_match('/Location:[^\"].*/', $result, $location);
                if(empty($location)){
                    //exit("登录错误!");
                    return 0;
                }
                list($lc,$urltemp)=explode(':',$location[0],2);
                $this -> gurl = $urltemp;
                curl_close($ch); //get data after login       
                return $cookie_jar;
            }
            
            //获取通讯录列表
            public function getFriendList($username,$password){
                 $result_array = array();
                 $login_url   = 'http://mail.sina.com.cn/cgi-bin/login.cgi';
                 $cookie_file = $this -> vlogin($login_url,$username,$password);    //模拟登陆             if(!$cookie_file){
                     return $result_array;
                 }
                 $this -> gurl = trim($this -> gurl);
                 $ch   = curl_init($this -> gurl);
                 $headers = array(
                                 'Host'       => 'mail3-145.sinamail.sina.com.cn',
                                 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2'
                 );
                 curl_setopt($ch, CURLOPT_URL,$this -> gurl);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 curl_setopt($ch, CURLOPT_HEADER, true);
                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
                 curl_setopt($ch, CURLOPT_POST, 0);
                 curl_setopt($ch, CURLOPT_REFERER, 'http://mail.sina.com.cn/');
                 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                 //将之前保存的cookie信息,一起发送到服务器端
                 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
                 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
                 curl_setopt($ch, CURLOPT_NOBODY, 0);
    //              echo file_get_contents($cookie_file);
                 $result = curl_exec($ch);
    //            echo $result;
        
                 curl_close($ch);
                 preg_match('/Location:[^\"].*/', $result, $location);             list($lc,$urltemp)=explode(':',$location[0],2);
    //             echo $this -> gurl = $urltemp;
                 $this -> gurl=trim($this -> gurl);
                 $server = substr(substr($this -> gurl,strpos($this -> gurl,'//')+2),0,strpos(substr($this -> gurl,strpos($this -> gurl,'//')+2),'/'));
                 $ch   = curl_init($this -> gurl);
                 $headers = array(
                                 'Host'       => $server,
                                 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2'
                 );
                 curl_setopt($ch, CURLOPT_URL,$this -> gurl);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 curl_setopt($ch, CURLOPT_HEADER, true);
                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
                 curl_setopt($ch, CURLOPT_POST, 0);
                 curl_setopt($ch, CURLOPT_REFERER, 'http://mail.sina.com.cn/');
                 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                 //将之前保存的cookie信息,一起发送到服务器端
                 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
                 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
                 curl_setopt($ch, CURLOPT_NOBODY, 0);
                 // echo file_get_contents($cookie_file);
                 $result = curl_exec($ch);
    //             echo $result;
                 curl_close($ch);
        
                 $url = "http://".$server."/classic/address.php?ts=".mktime()."358_1";
                 $ch   = curl_init($url);
                 $headers = array(
                                 'Host'       =>$server,
                                 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2'
                 );
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HEADER, true);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
                curl_setopt($ch, CURLOPT_POST, 0);
                curl_setopt($ch, CURLOPT_REFERER, $this -> gurl);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                 //将之前保存的cookie信息,一起发送到服务器端
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
                curl_setopt($ch, CURLOPT_NOBODY, 0);
                 // echo file_get_contents($cookie_file);
                $result = curl_exec($ch);
                //echo $result;
                curl_close($ch);
            
                $url="http://".$server."/classic/addr_member.php";
                $ch   = curl_init($url);
                $headers = array(
                         'Host'         => $server,
                         'User-Agent'   => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2',
                         'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
                );
                $str ="act=list&sort_item=letter&sort_type=desc";
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
                curl_setopt($ch, CURLOPT_REFERER, $this -> gurl);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                //将之前保存的cookie信息,一起发送到服务器端
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);           // echo file_get_contents($cookie_file);
                $result = curl_exec($ch);
                
                $result_mid[] = json_decode($result);
    // $result_mid = object_array($result_mid);
    /*            preg_match_all("/\"name\":\"(.*)\"/Umsi", $result, $name);
                preg_match_all("/\"email\":\"(.*)\"/Umsi", $result, $email);
                            foreach($name[1] as $k => $user){
                    $result_array[$email[1][$k]] = $user;
                }*/
    // $a[] = $result_mid->0;
    echo "<pre>";
    print_r($result_mid[0]->data->contact);
    // print_r($result_mid[0]['data']['contact']);
    echo "</pre>";
                 
    foreach($name as $k => $user){
                    $result_array[$email[1][$k]] = $user;
                }            curl_close($ch);
                return $result_array;
        }
        
    function object_array($array)
    {
       if(is_object($array))
       {
        $array = (array)$array;
       }
       if(is_array($array))
       {
        foreach($array as $key=>$value)
        {
         $array[$key] = object_array($value);
        }
       }
       return $array;
    }}$a=new httpSina();
    $tmp=$a->getFriendList("****@sina.com","****");if(is_array($tmp)){
     foreach($tmp as $k=>$v){
      echo "<BR>$k = $v";
     }
    }else
     echo $tmp;?>
      

  2.   

    lz,现在的sina的那个还好用不?能用的话,贴一下code呗~~