ホームページ  >  記事  >  バックエンド開発  >  Sina の電子メール連絡先リストの取得に失敗しました。

Sina の電子メール連絡先リストの取得に失敗しました。

WBOY
WBOYオリジナル
2016-06-13 13:42:531129ブラウズ

Sina の電子メール連絡先リストを取得できませんでした。助けてください。 ! !

PHP コード
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php
define( "COOKIEJAR", tempnam( ini_get( "upload_tmp_dir" ), "cookie" ) ); // COOKIES を保存するパスを定義します。操作権限が必要です。
define( "TIMEOUT", 1000 ); //タイムアウト設定
/*** 新浪電子メールアドレス帳リストを取得 -- contactssina.class.php*/

クラス連絡先シナ
{

    var $host = "";

    関数 checklogin( $user, $password )
    {
        if ( 空( $user ) || 空( $password ) )
        {
            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 ) )
        {
            0を返します。
        }
        $this->host = $matches[1];
        1を返します。
    }

    関数 getcontacts( $user, $password, &$result )
    {
        if ( !$this->checklogin( $user, $password ) )
        {
            「ログインに失敗しました」を返します。
        }
        $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 = 配列( );
        $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 );
        "結果:" を返します。$bRet;
    }

    関数 readcookies( $file, &$result )
    {
        $fp = fopen( $file, "r" );
        while ( !feof( $fp ) )
        {
            $buffer = fgets( $fp, 4096 );
            $tmp = split( "t", $buffer );
            $result[trim( $tmp[5] )] = トリム( $tmp[6] );
        }
        1を返します。
    }
    
    function _parsedata( $content, &$ar )
    {
        $ar = 配列( );
        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 として $email )
                {
                    $ar[$email] = $value->name;
                }
            }
        }
        $ar を返します。
    }

}

$contactssina = 新しい連絡先;

$res = $contactssina->getcontacts('****@sina.com','***',$result);

print_r($res);
?>

 


 <div class="clear"></div>
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。