<?php
define( "COOKIEJAR", tempnam( ini_get( "upload_tmp_dir" ), "cookie" ) );   //定义COOKIES存放的路径,要有操作的权限
define( "TIMEOUT", 1000 ); //超时设定
/**
 * sohu -- contactssina.class.php
 */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://www51.mail.sohu.com/webapp/contact" );
ob_start( );
curl_exec( $ch );
$content = ob_get_contents( );
ob_end_clean( );
curl_close( $ch );
$bRet = $this->_parsedata( $content, $result );
return $bRet;
} function _parsedata( $content, &$ar )
{
$ar = array( );
if ( !$content )
{
return '获取内容为空';;
}
$data = json_decode( $content );
// print_r($data);
unset( $content );
foreach ( $data->listString 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;
} 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;
}}$contacts = new sohu;$res = $contacts->getcontacts('****@sohu.com','****',$result);print_r($res);
?> 

解决方案 »

  1.   

    你之前那个邮箱联系人的帖子还没结呢,我还等着得分呢,嘿嘿。我去看看sohu网站,是不是更新了。
      

  2.   

     <?php 
     define( "COOKIEJAR", tempnam( ini_get( "upload_tmp_dir" ), "cookie" ) );   //定义COOKIES存放的路径,要有操作的权限 
     define( "TIMEOUT", 1000 ); //超时设定 
     /** 
      * sohu -- contactssina.class.php 
      */ 
      
     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/79/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]);
    $contactList = array();
    foreach ($numList->contact as $val){
    $obj = new stdClass;
    $obj->name = $val->nickname;
    $obj->nickname = $val->pinyin;
    $obj->emailAddress = $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; 
      } 
      
     } 
      
     $contacts = new sohu; 
      
     $res = $contacts->getcontacts('[email protected]','584521',$result); 
      
     print_r($res); 
     ?> 
    帮你随便改了点东西,整体都没变,$res就是你要的联系人信息数组列表。
      

  3.   

    http://hi.baidu.com/kxn308/blog/item/d9cc564dcedee3f7d62afc71.html
    去年做的 也是网上找的 然后修改的
    不知道 网页内容变了没
      

  4.   

    请问下现在这个问题解决了么?我试过这个代码,登录成功,但就是获取不到好友列表内容
    $content = ob_get_contents( );我想是这个获取不到内容...求高手赐教