php获取163邮箱通讯录的方法

<?php
/**
 * get address list from 163 mail box
 */
define( "COOKIEJAR", tempnam( "/tmp", "cookie" ) );

class adrSnatcher
{
  /**
    * @desc: login in the 163 mail box
    * @param string $username
    * @param string $password
    * @return int  //the login status
    */
    function login( $username, $password )
    {
        $ch = curl_init( );
       
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $ch, CURLOPT_URL, "https://reg.163.com/logins.jsp" );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password."&type=1" );
        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, "安全退出" ) !== false )
        {
            return 0;
        }
       
        return 1;
    }

  /**
    * @desc: get address list from mail box
    * @param string $username
    * @param string $password
    * @return array  //the address list
    */
    function getAddressList( $username, $password)
    {
        if ( !$this->login( $username, $password ) )
        {
            return 0;
        }
    
        $this->_getcookie();
        $cookies = $this->_readcookies( COOKIEJAR);
        $cookieid = substr( trim( $cookies['Coremail'] ), -32 );
        if ( !$cookieid )
        {
            return 0;
        }
       
        //get the address list page information
        $ch = curl_init( );
        curl_setopt( $ch, CURLOPT_URL, "http://cg5a85.mail.163.com/a/s?sid=".$cookieid."&func=global:sequential" );
        curl_setopt( $ch, CURLOPT_COOKIEFILE, COOKIEJAR );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array( "Content-Type: application/xml" ) );
        $str = "<?xml version=/"1.0/"?><object><array name=/"items/"><object><string name=/"func/">pab:searchContacts</string>" .
               "<object name=/"var/"><array name=/"order/"><object><string name=/"field/">FN</string><boolean name=/"ignoreCase/">true</boolean></object>" .
               "</array></object></object><object><string name=/"func/">user:getSignatures</string></object>" .
               "<object><string name=/"func/">pab:getAllGroups</string></object></array></object>";
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $str );
        curl_setopt( $ch, CURLOPT_TIMEOUT, TIMEOUT );
        ob_start( );
        curl_exec( $ch );
        $contents = ob_get_contents( );
        ob_end_clean( );
        curl_close( $ch );
       
        //get mail list from the page information
        $pattern = "/([//w_-])+@([//w])+([//w.]+)/";
        if ( preg_match_all( $pattern, $contents, $tmpres, PREG_PATTERN_ORDER ) )
        {
            $result = array_unique( $tmpres[0] );
        }
       
        return $result;
    }

  /**
    * get cookie
    */
    function _getcookie( )
    {
        $ch = curl_init( );
        curl_setopt( $ch, CURLOPT_URL, "http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1" );
        curl_setopt( $ch, CURLOPT_COOKIEFILE, COOKIEJAR );
        curl_setopt( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
        ob_start( );
        curl_exec( $ch );
        $contents = ob_get_contents( );
        ob_end_clean( );
        curl_close( $ch );
    }
   
   
    /**
      * get cookie from file
      */
    function _readcookies( $file)
    {
        $result = null;
       
        $fp = fopen( $file, "r" );
        if($fp)
        {
            while ( !feof( $fp ) )
          {
              $buffer = fgets( $fp, 4096 );
              $tmp = split( "/t", $buffer );
              $result[trim( $tmp[5] )] = trim( $tmp[6] );
          }
         
          fclose($fp);
        }
       
        return $result;
    }

}

 
  $username = "";
  $password = "";
 
  $asn = new adrSnatcher();
  $addresslist = $asn->getAddressList($username, $password);
  var_dump($addresslist);
exit;
?>

你可能感兴趣的:(PHP,function,String,list,Cookies,通讯)