function checklogin( $user, $password )
{

// $url = "http://passport.sohu.com/sso/login.jsp";       $headers_login = array(
                     'Host'  => 'mail.sohu.com',
                     '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.sohu.com/'
                 ); $url = "http://mail.sohu.com/";
$url = $url."?userid=".urlencode( $user );
$url = $url."&password=".md5( $password );
$url = $url."&appid=1000&persistentcookie=0&isSLogin=1&s=".time( )."&b=1&w=1366&pwdtype=1&v=26";
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login);  
curl_setopt( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
curl_setopt( $ch, CURLOPT_TIMEOUT, TIMEOUT );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HEADER,1);
// ob_start( );
$contents = curl_exec( $ch );
// $contents = ob_get_contents( );
echo $contents;exit; //此处echo时会出现wap邮箱链接
curl_close($ch); //获取二次跳转地址
// preg_match("/<a\shref=\"(.+)\">here<\/a>/", $contents, $mtitle);
// print_r($mtitle);exit;
// $_url1 = $mtitle[1];
// echo $_url1;
// exit;
$_url1 = 'http://mail.sohu.com/bapp/81/main'; //第二步:再次跳转到到上面$_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);
echo $str2;exit;
// ob_end_clean( );
curl_close( $ch );
/* if ( strpos( $contents, "success" ) === false )
{
return 0;
}*/

if ( $str2 != 1 )
{
return 0;
}
return 1;
}

解决方案 »

  1.   

    用httpfox或者HttpWatch 跟踪一下sohu邮箱从登陆到成功显示,究竟跳转了几个地址,每个地址上是否需要cookie,然后模仿之。
      

  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 )
    {

    // $url = "http://passport.sohu.com/sso/login.jsp";       $headers_login = array(
                         'Host'  => 'mail.sohu.com',
                         '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.sohu.com/'
                     ); $url = "http://passport.sohu.com/sso/login.jsp";
    $url = $url."?userid=".urlencode( $user );
    $url = $url."&password=".md5( $password );
    $url = $url."&appid=1000&persistentcookie=0&isSLogin=1&s=".time( )."&b=1&w=1366&pwdtype=1&v=26";
    $ch = curl_init($url);
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login);  
    curl_setopt( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
    curl_setopt( $ch, CURLOPT_TIMEOUT, TIMEOUT );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_HEADER,1);
    // ob_start( );
    $contents = curl_exec( $ch );
    // $contents = ob_get_contents( );
    // echo $contents;exit;
    curl_close($ch); //获取二次跳转地址
    // preg_match("/<a\shref=\"(.+)\">here<\/a>/", $contents, $mtitle);
    // print_r($mtitle);exit;
    // $_url1 = $mtitle[1];
    // echo $_url1;
    // exit;
    // $_url1 = 'http://mail.sohu.com/bapp/81/main';
    //
    // //第二步:再次跳转到到上面$_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);
    // echo $str2;exit;
    //// 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 'checklogin失败';
    }
    $cookies = array( );
    $bRet = $this->readcookies( COOKIEJAR, $cookies );
    // print_r($cookies);exit;
    // print_r($bRet);exit;
    if ( !$bRet && !$cookies['JSESSIONID'] )
    {
    return 'readcookies失败';
    }
    $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" );        $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'
            ); for($i=0;$i<6;$i++)
    {
    $num.=mt_rand(1,9);
    }
    // echo $num;exit;


    curl_setopt( $ch, CURLOPT_URL, "http://mail.sohu.com/jsoplog/log?uc=|mailaddress&r=".$num );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    ob_start( );
    curl_exec( $ch );
    $content = ob_get_contents( );
    echo $content;exit;
    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('****','****',$result);print_r($res);
    ?> 
      

  3.   

    我遇到过这种情况,你要注入cookie才行 !
      

  4.   

    可能是这种情况,只是不知道怎么才能将cookie传递下去
      

  5.   

    curl有设置cookie的参数,写进文件里都行
      

  6.   

    用httpfox,正常的浏览器访问抓一下包,按照浏览的包头去发就可以了
      

  7.   

    麻烦加下我QQ  375865795  最近也在做这个  不过没做SOHU   交流下
      

  8.   

    就是按这种方法去做的,但是对http头之类的有点迷糊,而且sohu邮箱登陆好像跳了好几次,最终更迷糊了
      

  9.   

    你可以不用多次跳转,登录成功之后直接访问你希望获取的数据的页面就可以,记得每次都把所有的包头发过去,尤其是cookie别忘了