📄 class.http.php
字号:
<?php
/*********************/
/* */
/* Version : 5.1.0 */
/* Author : RM */
/* Comment : 071223 */
/* */
/*********************/
class http
{
var $target;
var $host;
var $port;
var $path;
var $schema;
var $method;
var $params;
var $cookies;
var $_cookies;
var $timeout;
var $useCurl;
var $referrer;
var $userAgent;
var $cookiePath;
var $useCookie;
var $saveCookie;
var $username;
var $password;
var $result;
var $headers;
var $status;
var $redirect;
var $maxRedirect;
var $curRedirect;
var $error;
var $nextToken;
var $debug;
var $debugMsg;
function http( )
{
$this->clear( );
}
function initialize( $config = array( ) )
{
$this->clear( );
foreach ( $config as $key => $val )
{
if ( isset( $this->$key ) )
{
$method = "set".ucfirst( str_replace( "_", "", $key ) );
if ( method_exists( $this, $method ) )
{
$this->$method( $val );
}
else
{
$this->$key = $val;
}
}
}
}
function clear( )
{
$this->host = "";
$this->port = 0;
$this->path = "";
$this->target = "";
$this->method = "GET";
$this->schema = "http";
$this->params = array( );
$this->headers = array( );
$this->cookies = array( );
$this->_cookies = array( );
$this->debug = FALSE;
$this->error = "";
$this->status = 0;
$this->timeout = "25";
$this->useCurl = TRUE;
$this->referrer = "";
$this->username = "";
$this->password = "";
$this->redirect = TRUE;
$this->nextToken = "";
$this->useCookie = TRUE;
$this->saveCookie = TRUE;
$this->maxRedirect = 3;
$this->cookiePath = "cookie.txt";
$this->userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.9";
}
function settarget( $url )
{
if ( $url )
{
$this->target = $url;
}
}
function setmethod( $method )
{
if ( $method == "GET" || $method == "POST" )
{
$this->method = $method;
}
}
function setreferrer( $referrer )
{
if ( $referrer )
{
$this->referrer = $referrer;
}
}
function setuseragent( $agent )
{
if ( $agent )
{
$this->userAgent = $agent;
}
}
function settimeout( $seconds )
{
if ( 0 < $seconds )
{
$this->timeout = $seconds;
}
}
function setcookiepath( $path )
{
if ( $path )
{
$this->cookiePath = $path;
}
}
function setparams( $dataArray )
{
if ( is_array( $dataArray ) )
{
$this->params = array_merge( $this->params, $dataArray );
}
}
function setauth( $username, $password )
{
if ( !empty( $username ) || !empty( $password ) )
{
$this->username = $username;
$this->password = $password;
}
}
function setmaxredirect( $value )
{
if ( !empty( $value ) )
{
$this->maxRedirect = $value;
}
}
function addparam( $name, $value )
{
if ( !empty( $name ) || !empty( $value ) )
{
$this->params[$name] = $value;
}
}
function addcookie( $name, $value )
{
if ( !empty( $name ) || !empty( $value ) )
{
$this->cookies[$name] = $value;
}
}
function usecurl( $value = TRUE )
{
if ( is_bool( $value ) )
{
$this->useCurl = $value;
}
}
function usecookie( $value = TRUE )
{
if ( is_bool( $value ) )
{
$this->useCookie = $value;
}
}
function savecookie( $value = TRUE )
{
if ( is_bool( $value ) )
{
$this->saveCookie = $value;
}
}
function followredirects( $value = TRUE )
{
if ( is_bool( $value ) )
{
$this->redirect = $value;
}
}
function getresult( )
{
return $this->result;
}
function getheaders( )
{
return $this->headers;
}
function getstatus( )
{
return $this->status;
}
function geterror( )
{
return $this->error;
}
function execute( $target = "", $referrer = "", $method = "", $data = array( ) )
{
$this->target = $target ? $target : $this->target;
$this->method = $method ? $method : $this->method;
$this->referrer = $referrer ? $referrer : $this->referrer;
if ( is_array( $data ) && 0 < count( $data ) )
{
$this->params = array_merge( $this->params, $data );
}
if ( is_array( $this->params ) && 0 < count( $this->params ) )
{
$tempString = array( );
foreach ( $this->params as $key => $value )
{
if ( 0 < strlen( trim( $value ) ) )
{
$tempString[] = $key."=".urlencode( $value );
}
}
$queryString = join( "&", $tempString );
}
$this->useCurl = $this->useCurl && in_array( "curl", get_loaded_extensions( ) );
if ( $this->method == "GET" && isset( $queryString ) )
{
$this->target = $this->target."?".$queryString;
}
$urlParsed = parse_url( $this->target );
if ( $urlParsed['scheme'] == "https" )
{
$this->host = "ssl://".$urlParsed['host'];
$this->port = $this->port != 0 ? $this->port : 443;
}
else
{
$this->host = $urlParsed['host'];
$this->port = $this->port != 0 ? $this->port : 80;
}
$this->path = ( isset( $urlParsed['path'] ) ? $urlParsed['path'] : "/" ).( isset( $urlParsed['query'] ) ? "?".$urlParsed['query'] : "" );
$this->schema = $urlParsed['scheme'];
$this->_passcookies( );
if ( is_array( $this->cookies ) && 0 < count( $this->cookies ) )
{
$tempString = array( );
foreach ( $this->cookies as $key => $value )
{
if ( 0 < strlen( trim( $value ) ) )
{
$tempString[] = $key."=".urlencode( $value );
}
}
$cookieString = join( "&", $tempString );
}
if ( $this->useCurl )
{
$ch = curl_init( );
if ( $this->method == "GET" )
{
curl_setopt( $ch, CURLOPT_HTTPGET, TRUE );
curl_setopt( $ch, CURLOPT_POST, FALSE );
}
else
{
if ( isset( $queryString ) )
{
curl_setopt( $ch, CURLOPT_POSTFIELDS, $queryString );
}
curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_HTTPGET, FALSE );
}
if ( $this->username && $this->password )
{
curl_setopt( $ch, CURLOPT_USERPWD, $this->username.":".$this->password );
}
if ( $this->useCookie && isset( $cookieString ) )
{
curl_setopt( $ch, CURLOPT_COOKIE, $cookieString );
}
curl_setopt( $ch, CURLOPT_HEADER, TRUE );
curl_setopt( $ch, CURLOPT_NOBODY, FALSE );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $this->cookiePath );
curl_setopt( $ch, CURLOPT_TIMEOUT, $this->timeout );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent );
curl_setopt( $ch, CURLOPT_URL, $this->target );
curl_setopt( $ch, CURLOPT_REFERER, $this->referrer );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -