PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » 入门和基础知识 » 贡献一下最新自娱作品:Transferer
本页主题: 贡献一下最新自娱作品:Transferer 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

自由的龙

该用户目前不在线
级别: 中级程序员
精华: 1
发帖: 1264
威望: 1267 点
金钱: 12660 PYMB
贡献值: 0 点
在线时间:0(小时)
注册时间:2006-04-16
最后登录:2006-06-27

贡献一下最新自娱作品:Transferer


<?php
define( 'NL', "rn" );

class Transferer
{
  var $mHost;
  var $mPort;
  var $mRequest;
  var $mResponse;
  var $mCookies;
  var $mParams;
  var $mAgent;
  var $mError;

  function Transferer ( $host = '', $port = 80 )
  {
    $this->mHost = '';
    $this->mPort = 0;
    $this->mRequest = '';
    $this->mResponse = '';
    $this->mCookies = '';
    $this->mParams = '';
    $this->mAgent = 'Transferer/1.0 (compatible; Snakevil; ERiW)';
    $this->mError = FALSE;

    if ( strLen( $host ) && $port )
    {
        $this->Open( $host, $port );
    }
    return;
  }

  function SetAgent ( $agent = '' )
  {
    if ( strLen( $agent ) )
    {
        // Personal User-Agent.
        $this->mAgent = $agent;
        return TRUE;
    }
    return FALSE;
  }

  function Open ( $host = '', $port = 0 )
  {
    if ( $this->mError )
    {
        return FALSE;
    }
    if ( 0 === strPos( $host, 'http://' ) )
    {
        // Remove "http://".
        $host = subStr( $host, 7 );
    }
    if ( strLen( $host ) - 1 === strrPos( $host, '/' ) )
    {
        // Remove "/" at last.
        $host = subStr( $host, 0, -1 );
    }
    if ( ! strLen( $host ) || ! $port )
    {
        // No Host or No Port.
        return FALSE;
    }
    if ( getHostByName( $host ) == $host )
    {
        // Invalid Domain.
        return FALSE;
    }
    $this->mHost = $host;
    $this->mPort = $port;
    return TRUE;
  }

  function Close ()
  {
    $this->mError = FALSE;
    return TRUE;
  }

  function ClearError ()
  {
    $this->mError = FALSE;
    $this->mResponse = '';
    return TRUE;
  }

  function AddParam ( $name = '', $value = '' )
  {
    if ( ! strLen( $name ) )
    {
        // Params Name should have content.
        return FALSE;
    }
    $name .= '=';
    if ( FALSE !== strPos( $this->mParams, $name ) )
    {
        // Ignore Exist Param.
        return FALSE;
    }
    if ( ! is_string( $value ) )
    {
        $value = strVal( $value );
    }
    $name .= urlEncode( $value );
    if ( strLen( $this->mParams ) )
    {
        $this->mParams .= '&';
    }
    $this->mParams .= $name;
    return TRUE;
  }

  function Send ( $method = 'get', $uri = '', $fromUrl = '', $updCookie = FALSE )
  {
    if ( $this->mError )
    {
        return FALSE;
    }
    $method = strToUpper( $method );
    if ( ! in_array( $method, array( 'GET', 'POST' ) ) )
    {
        // HTTP Method should be one in GET or POST.
        return FALSE;
    }
    $ii = strPos( $uri, '?' );
    if ( $ii )
    {
        $uri = subStr( $uri, 0, $ii );
    }
    $this->mRequest = '';
    $this->mResponse = '';
    switch ( $method )
    {
        case 'GET':
          if ( strLen( $this->mParams ) )
          {
            $uri .= '?' . $this->mParams;
          }
          $this->mRequest = 'GET /' . $uri . ' HTTP/1.1' . NL
          . 'Accept: */*' . NL;
          if ( strLen( $fromUrl ) )
          {
            $this->mRequest .= 'Referer: ' . $fromUrl . NL;
          }
          $this->mRequest .= 'Accept-Language: zh-cn' . NL
          . 'Accept-Encoding: gzip, deflate' . NL
          . 'User-Agent: ' . $this->mAgent . NL
          . 'Host: ' . $this->mHost . NL
          . 'Connection: close' . NL;
          if ( strLen( $this->mCookies ) )
          {
            $this->mRequest .= 'Cookie: ' . $this->mCookies . NL;
          }
          $this->mRequest .= NL;
          break;
        case 'POST':
          $this->mRequest = 'POST /' . $uri . ' HTTP/1.1' . NL
          . 'Accept: */*' . NL;
          if ( strLen( $fromUrl ) )
          {
            $this->mRequest .= 'Referer: ' . $fromUrl . NL;
          }
          $this->mRequest .= 'Accept-Language: zh-cn' . NL
          . 'Content-Type: application/x-www-form-urlencoded' . NL
          . 'Accept-Encoding: gzip, deflate' . NL
          . 'User-Agent: ' . $this->mAgent . NL
          . 'Host: ' . $this->mHost . NL
          . 'Content-Length: ' . strLen( $this->mParams ) . NL
          . 'Connection: close' . NL
          . 'Cache-Control: no-cache' . NL;
          if ( strLen( $this->mCookies ) )
          {
            $this->mRequest .= 'Cookie: ' . $this->mCookies . NL;
          }
          $this->mRequest .= NL
          . $this->mParams . NL;
          break;
        default:
    }
    $this->mParams = '';
    $h_sock = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
    if ( FALSE === $h_sock )
    {
        // Fail to Create Socket.
        $this->mResponse = socket_strerror( socket_last_error() );
        $this->mError = TRUE;
    }
    // ::TODO:: Set Write and Read Timeout Here...
    $m_result = socket_connect( $h_sock, $this->mHost, $this->mPort );
    if ( ! $m_result )
    {
        // Fail to Connect to Server.
        $this->mResponse = socket_strerror( socket_last_error( $h_sock ) );
        $this->Close();
        $this->mError = TRUE;
        return FALSE;
    }
    // Send Request Data through TCP Socket.
    $m_result = socket_write( $h_sock, $this->mRequest, strLen( $this->mRequest ) );
    if ( FALSE === $m_result )
    {
        // Fail to Send Request Headers.
        $this->mResponse = socket_strerror( socket_last_error( $h_sock ) );
        $this->Close();
        $this->mError = TRUE;
        return FALSE;
    }
    $m_result = socket_read( $h_sock, 1024, PHP_BINARY_READ );
    if ( FALSE === $m_result )
    {
        // Fail to Receive Response Headers.
        $this->mResponse = socket_strerror( socket_last_error( $h_sock ) );
        $this->Close();
        $this->mError = TRUE;
        return FALSE;
    }
    $a_data = explode( NL . NL, $m_result );
    $this->mResponse = $a_data[0];
    if ( $updCookie )
    {
        // Regroup Exist Cookies.
        $a_cookies = array();
        if ( strLen( $this->mCookies ) )
        {
          $a_tmp = explode( '; ', $this->mCookies );
          for ( $ii = 0, $jj = count( $a_tmp ); $ii < $jj; $ii++ )
          {
            $kk = strPos( $a_tmp[$ii], '=' );
            $s_name = subStr( $a_tmp[$ii], 0, $kk );
            $s_value = subStr( $a_tmp[$ii], $kk + 1 );
            $a_cookies[$s_name] = $s_value;
          }
        }
        // Check Each Header.
        $a_tmp = explode( NL, $this->mResponse );
        for ( $ii = 0, $jj = count( $a_tmp ); $ii < $jj; $ii++ )
        {
          if ( 0 === strPos( $a_tmp[$ii], 'Set-Cookie: ' ) )
          {
            $a_tmp2 = explode( '; ', subStr( $a_tmp[$ii], 12 ) );
            $kk = strPos( $a_tmp2[0], '=' );
            $s_name = subStr( $a_tmp2[0], 0, $kk );
            $s_value = subStr( $a_tmp2[0], $kk + 1 );
            $i_now = time();
            $i_expires = $i_now + 3600;
            if ( isSet( $a_tmp2[1] ) && 0 === strPos( $a_tmp[1], 'expires=' ) )
            {
                $i_expires = strtotime( subStr( $a_tmp2[1], 8 ) );
            }
            if ( $i_now > $i_expires && isSet( $a_cookies[$s_name] ) )
            {
                unset( $a_cookies[$s_name] );
            }
            if ( $i_now <= $i_expires )
            {
                $a_cookies[$s_name] = $s_value;
            }
          }
        }
        // Update Cookies.
        $a_tmp = array();
        reset( $a_cookies );
        while ( list( $s_name, $s_value ) = each( $a_cookies ) )
        {
          $a_tmp[] = $s_name . '=' . $s_value;
        }
        $this->mCookies = implode( '; ', $a_tmp );
    }
    socket_clear_error( $h_sock );
    socket_close( $h_sock );
    return TRUE;
  }

  function GetError ()
  {
    if ( ! $this->mError )
    {
        return '';
    }
    return $this->mResponse;
  }

  function GetRequest ()
  {
    return $this->mRequest;
  }

  function GetResponse ()
  {
    return $this->mResponse;
  }

  function GetCookies ()
  {
    return $this->mCookies;
  }
}
?>
顶端 Posted: 2006-04-16 13:54 | [楼 主]
PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » 入门和基础知识

时:01-09 02:56 Copyright © 2006 phpwhy.com 权
ICP05060669

曳息 -