现在的位置: 首页 > 综合 > 正文

php模拟点击器,刷新,清除ie的get和post请求

2014年01月31日 ⁄ 综合 ⁄ 共 5261字 ⁄ 字号 评论关闭
  1. <?
  2. //在 PHP.4.3.11, socket 上测试通过
  3. define( 'NL', "/r/n" );
  4.  
  5. class Transferer
  6. {
  7.     var $mHost;
  8.     var $mPort;
  9.     var $mRequest;
  10.     var $mResponse;
  11.     var $mCookies;
  12.     var $mParams;
  13.     var $mAgent;
  14.     var $mError;
  15.  
  16.     function Transferer ( $host = '', $port = 80 )
  17.     {
  18.         $this->mHost = '';
  19.         $this->mPort = 0;
  20.         $this->mRequest = '';
  21.         $this->mResponse = '';
  22.         $this->mCookies = '';
  23.         $this->mParams = '';
  24.         $this->mAgent = 'Transferer/1.0 (compatible; Snakevil; ERiW)';
  25.         $this->mError = FALSE;
  26.  
  27.         if ( strLen( $host ) && $port )
  28.         {
  29.             $this->Open( $host, $port );
  30.         }
  31.         return;
  32.     }
  33.  
  34.     function SetAgent ( $agent = '' )
  35.     {
  36.         if ( strLen( $agent ) )
  37.         {
  38.             // Personal User-Agent.
  39.             $this->mAgent = $agent;
  40.             return TRUE;
  41.         }
  42.         return FALSE;
  43.     }
  44.  
  45.     function Open ( $host = '', $port = 0 )
  46.     {
  47.         if ( $this->mError )
  48.         {
  49.             return FALSE;
  50.         }
  51.         if ( 0 === strPos( $host, 'http://' ) )
  52.         {
  53.             // Remove "http://".
  54.             $host = subStr( $host, 7 );
  55.         }
  56.         if ( strLen( $host ) - 1 === strrPos( $host, '/' ) )
  57.         {
  58.             // Remove "/" at last.
  59.             $host = subStr( $host, 0, -1 );
  60.         }
  61.         if ( ! strLen( $host ) || ! $port )
  62.         {
  63.             // No Host or No Port.
  64.             return FALSE;
  65.         }
  66.         if ( getHostByName( $host ) == $host )
  67.         {
  68.             // Invalid Domain.
  69.             return FALSE;
  70.         }
  71.         $this->mHost = $host;
  72.         $this->mPort = $port;
  73.         return TRUE;
  74.     }
  75.  
  76.     function Close ()
  77.     {
  78.         $this->mError = FALSE;
  79.         return TRUE;
  80.     }
  81.  
  82.     function ClearError ()
  83.     {
  84.         $this->mError = FALSE;
  85.         $this->mResponse = '';
  86.         return TRUE;
  87.     }
  88.  
  89.     function AddParam ( $name = '', $value = '' )
  90.     {
  91.         if ( ! strLen( $name ) )
  92.         {
  93.             // Params Name should have content.
  94.             return FALSE;
  95.         }
  96.         $name .= '=';
  97.         if ( FALSE !== strPos( $this->mParams, $name ) )
  98.         {
  99.             // Ignore Exist Param.
  100.             return FALSE;
  101.         }
  102.         if ( ! is_string( $value ) )
  103.         {
  104.             $value = strVal( $value );
  105.         }
  106.         $name .= urlEncode( $value );
  107.         if ( strLen( $this->mParams ) )
  108.         {
  109.             $this->mParams .= '&';
  110.         }
  111.         $this->mParams .= $name;
  112.         return TRUE;
  113.     }
  114.  
  115.     function Send ( $method = 'get', $uri = '', $fromUrl = '', $updCookie = FALSE )
  116.     {
  117.         if ( $this->mError )
  118.         {
  119.             return FALSE;
  120.         }
  121.         $method = strToUpper( $method );
  122.         if ( ! in_array( $method, array( 'GET', 'POST' ) ) )
  123.         {
  124.             // HTTP Method should be one in GET or POST.
  125.             return FALSE;
  126.         }
  127.         $ii = strPos( $uri, '?' );
  128.         if ( $ii )
  129.         {
  130.             $uri = subStr( $uri, 0, $ii );
  131.         }
  132.         $this->mRequest = '';
  133.         $this->mResponse = '';
  134.         switch ( $method )
  135.         {
  136.             case 'GET':
  137.                 if ( strLen( $this->mParams ) )
  138.                 {
  139.                     $uri .= '?' . $this->mParams;
  140.                 }
  141.                 $this->mRequest = 'GET /' . $uri . ' HTTP/1.1' . NL
  142.                 . 'Accept: */*' . NL;
  143.                 if ( strLen( $fromUrl ) )
  144.                 {
  145.                     $this->mRequest .= 'Referer: ' . $fromUrl . NL;
  146.                 }
  147.                 $this->mRequest .= 'Accept-Language: zh-cn' . NL
  148.                 . 'Accept-Encoding: gzip, deflate' . NL
  149.                 . 'User-Agent: ' . $this->mAgent . NL
  150.                 . 'Host: ' . $this->mHost . NL
  151.                 . 'Connection: close' . NL;
  152.                 if ( strLen( $this->mCookies ) )
  153.                 {
  154.                     $this->mRequest .= 'Cookie: ' . $this->mCookies . NL;
  155.                 }
  156.                 $this->mRequest .= NL;
  157.                 break;
  158.             case 'POST':
  159.                 $this->mRequest = 'POST /' . $uri . ' HTTP/1.1' . NL
  160.                 . 'Accept: */*' . NL;
  161.                 if ( strLen( $fromUrl ) )
  162.                 {
  163.                     $this->mRequest .= 'Referer: ' . $fromUrl . NL;
  164.                 }
  165.                 $this->mRequest .= 'Accept-Language: zh-cn' . NL
  166.                 . 'Content-Type: application/x-www-form-urlencoded' . NL
  167.                 . 'Accept-Encoding: gzip, deflate' . NL
  168.                 . 'User-Agent: ' . $this->mAgent . NL
  169.                 . 'Host: ' . $this->mHost . NL
  170.                 . 'Content-Length: ' . strLen( $this->mParams ) . NL
  171.                 . 'Connection: close' . NL
  172.                 . 'Cache-Control: no-cache' . NL;
  173.                 if ( strLen( $this->mCookies ) )
  174.                 {
  175.                     $this->mRequest .= 'Cookie: ' . $this->mCookies . NL;
  176.                 }
  177.                 $this->mRequest .= NL
  178.                 . $this->mParams . NL;
  179.                 break;
  180.             default:
  181.         }
  182.         $this->mParams = '';
  183.         $h_sock = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
  184.         if ( FALSE === $h_sock )
  185.         {
  186.             // Fail to Create Socket.
  187.             $this->mResponse = socket_strerror( socket_last_error() );
  188.             $this->mError = TRUE;
  189.         }
  190.         // ::TODO:: Set Write and Read Timeout Here...
  191.         $m_result = socket_connect( $h_sock, $this->mHost, $this->mPort );
  192.         if 
【上篇】
【下篇】

抱歉!评论已关闭.