页面刷新php实现 — IT技术

2013 年 9 月 9 日5010

120

帮个朋友做一个点击器开始的,后来逐步完善,再次温习 RFC,尝试用 nc 抓取更标准更有效的 request & response headers。

最后就成了下面的这么一个玩意:(在 PHP.4.3.11, --enable-socket 上测试通过^_^),enjoy

<?

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;

}

}

?>

字体[ 进入论坛] [ 推荐给朋友]

0 0