php模拟点击器,刷新,清除ie的get和post请求
2014年01月31日
⁄ 综合
⁄ 共 5261字 ⁄ 字号
小 中 大
<?
//在 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