<?//在 PHP.4.3.11, socket 上测试通过define( 'NL', "/r/n" );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;}}//例子$o_trans = new Transferer( 'www.domain.com', 80 ); // start work$o_trans->Send( 'get', '', '', TRUE ); // receive base cookie$o_trans->AddParam( 'username', 'myname' ); // set login user name$o_trans->AddParam( 'password', 'mypass' ); // set login user password$o_trans->AddParam( 'submit', 'LOGIN' ); // add login button$o_trans->Send( 'post', 'login.php', 'http://www.domain.com/', TRUE ); // login and update cookie$o_trans->Send( 'get', 'click.php', 'http://www.domain.com/referer.php' ); // try click$o_trans->Close();?>
php模拟点击器,刷新,清除ie的get和post请求
最新推荐文章于 2025-10-10 12:01:18 发布
本文介绍了一个使用PHP实现的网络请求处理类,支持GET和POST请求,能够管理HTTP头部信息、Cookies,并通过TCP套接字进行数据收发。此外,还提供了错误处理、参数设置及Cookies更新等功能。
1650

被折叠的 条评论
为什么被折叠?



