📄 class.http.php
字号:
curl_setopt( $ch, CURLOPT_VERBOSE, FALSE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, $this->redirect );
curl_setopt( $ch, CURLOPT_MAXREDIRS, $this->maxRedirect );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
$content = curl_exec( $ch );
$contentArray = explode( "\r\n\r\n", $content );
$status = curl_getinfo( $ch );
$resHeader = array_shift( $contentArray );
$this->result = implode( $contentArray, "\r\n\r\n" );
$this->_parseheaders( $resHeader );
$this->_seterror( curl_error( $ch ) );
curl_close( $ch );
}
else
{
$filePointer = fsockopen( $this->host, $this->port, $errorNumber, $errorString, $this->timeout );
if ( !$filePointer )
{
$this->_seterror( "Failed opening http socket connection: ".$errorString." (".$errorNumber.")" );
return FALSE;
}
$requestHeader = $this->method." ".$this->path." HTTP/1.1\r\n";
$requestHeader .= "Host: ".$urlParsed['host']."\r\n";
$requestHeader .= "User-Agent: ".$this->userAgent."\r\n";
$requestHeader .= "Content-Type: application/x-www-form-urlencoded\r\n";
if ( $this->useCookie && $cookieString != "" )
{
$requestHeader .= "Cookie: ".$cookieString."\r\n";
}
if ( $this->method == "POST" )
{
$requestHeader .= "Content-Length: ".strlen( $queryString )."\r\n";
}
if ( $this->referrer != "" )
{
$requestHeader .= "Referer: ".$this->referrer."\r\n";
}
if ( $this->username && $this->password )
{
$requestHeader .= "Authorization: Basic ".base64_encode( $this->username.":".$this->password )."\r\n";
}
$requestHeader .= "Connection: close\r\n\r\n";
if ( $this->method == "POST" )
{
$requestHeader .= $queryString;
}
fwrite( $filePointer, $requestHeader );
$responseHeader = "";
$responseContent = "";
do
{
$responseHeader .= fread( $filePointer, 1 );
} while ( !preg_match( "/\\r\\n\\r\\n\$/", $responseHeader ) );
$this->_parseheaders( $responseHeader );
if ( $this->status == "302" && $this->redirect == TRUE )
{
if ( $this->curRedirect < $this->maxRedirect )
{
$newUrlParsed = parse_url( $this->headers['location'] );
if ( $newUrlParsed['host'] )
{
$newTarget = $this->headers['location'];
}
else
{
$newTarget = $this->schema."://".$this->host."/".$this->headers['location'];
}
$this->port = 0;
$this->status = 0;
$this->params = array( );
$this->method = "GET";
$this->referrer = $this->target;
++$this->curRedirect;
$this->result = $this->execute( $newTarget );
}
else
{
$this->_seterror( "Too many redirects." );
return FALSE;
}
}
else
{
if ( $this->headers['transfer-encoding'] != "chunked" )
{
while ( !feof( $filePointer ) )
{
$responseContent .= fgets( $filePointer, 128 );
}
}
else
{
while ( $chunkLength = hexdec( fgets( $filePointer ) ) )
{
$responseContentChunk = "";
$readLength = 0;
while ( $readLength < $chunkLength )
{
$responseContentChunk .= fread( $filePointer, $chunkLength - $readLength );
$readLength = strlen( $responseContentChunk );
}
$responseContent .= $responseContentChunk;
fgets( $filePointer );
}
}
$this->result = chop( $responseContent );
}
}
return $this->result;
}
function _parseheaders( $responseHeader )
{
$headers = explode( "\r\n", $responseHeader );
$this->_clearheaders( );
if ( $this->status == 0 )
{
if ( !eregi( $match = "^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$", $headers[0], $matches ) )
{
$this->_seterror( "Unexpected HTTP response status" );
return FALSE;
}
$this->status = $matches[1];
array_shift( $headers );
}
foreach ( $headers as $header )
{
$headerName = strtolower( $this->_tokenize( $header, ":" ) );
$headerValue = trim( chop( $this->_tokenize( "\r\n" ) ) );
if ( isset( $this->headers[$headerName] ) )
{
if ( gettype( $this->headers[$headerName] ) == "string" )
{
$this->headers[$headerName] = array(
$this->headers[$headerName]
);
}
$this->headers[$headerName][] = $headerValue;
}
else
{
$this->headers[$headerName] = $headerValue;
}
}
if ( $this->saveCookie && isset( $this->headers['set-cookie'] ) )
{
$this->_parsecookie( );
}
}
function _clearheaders( )
{
$this->headers = array( );
}
function _parsecookie( )
{
if ( gettype( $this->headers['set-cookie'] ) == "array" )
{
$cookieHeaders = $this->headers['set-cookie'];
}
else
{
$cookieHeaders = array(
$this->headers['set-cookie']
);
}
$cookie = 0;
for ( ; $cookie < count( $cookieHeaders ); ++$cookie )
{
$cookieName = trim( $this->_tokenize( $cookieHeaders[$cookie], "=" ) );
$cookieValue = $this->_tokenize( ";" );
$urlParsed = parse_url( $this->target );
$domain = $urlParsed['host'];
$secure = "0";
$path = "/";
$expires = "";
while ( ( $name = trim( urldecode( $this->_tokenize( "=" ) ) ) ) != "" )
{
$value = urldecode( $this->_tokenize( ";" ) );
switch ( $name )
{
case "path" :
$path = $value;
continue;
case "domain" :
$domain = $value;
continue;
case "secure" :
$secure = $value != "" ? "1" : "0";
default :
break;
}
}
$this->_setcookie( $cookieName, $cookieValue, $expires, $path, $domain, $secure );
}
}
function _setcookie( $name, $value, $expires = "", $path = "/", $domain = "", $secure = 0 )
{
if ( strlen( $name ) == 0 )
{
return $this->_seterror( "No valid cookie name was specified." );
}
if ( strlen( $path ) == 0 || strcmp( $path[0], "/" ) )
{
return $this->_seterror( $path." is not a valid path for setting cookie {$name}." );
}
if ( !( $domain == "" ) )
{
if ( !strpos( $domain, ".", $domain[0] == "." ? 1 : 0 ) )
{
}
}
else
{
return $this->_seterror( $domain." is not a valid domain for setting cookie {$name}." );
}
$domain = strtolower( $domain );
if ( !strcmp( $domain[0], "." ) )
{
$domain = substr( $domain, 1 );
}
$name = $this->_encodecookie( $name, true );
$value = $this->_encodecookie( $value, false );
$secure = intval( $secure );
$this->_cookies[] = array(
"name" => $name,
"value" => $value,
"domain" => $domain,
"path" => $path,
"expires" => $expires,
"secure" => $secure
);
}
function _encodecookie( $value, $name )
{
if ( $name )
{
return str_replace( "=", "%25", $value );
}
return str_replace( ";", "%3B", $value );
}
function _passcookies( )
{
if ( is_array( $this->_cookies ) && 0 < count( $this->_cookies ) )
{
$urlParsed = parse_url( $this->target );
$tempCookies = array( );
foreach ( $this->_cookies as $cookie )
{
if ( !$this->_domainmatch( $urlParsed['host'], $cookie['domain'] ) && !( 0 === strpos( $urlParsed['path'], $cookie['path'] ) ) && !empty( $cookie['secure'] ) || !( $urlParsed['protocol'] == "https" ) )
{
$tempCookies[$cookie['name']][strlen( $cookie['path'] )] = $cookie['value'];
}
}
foreach ( $tempCookies as $name => $values )
{
krsort( $values );
foreach ( $values as $value )
{
$this->addcookie( $name, $value );
}
}
}
}
function _domainmatch( $requestHost, $cookieDomain )
{
if ( "." != $cookieDomain[0] )
{
return $requestHost == $cookieDomain;
}
if ( substr_count( $cookieDomain, "." ) < 2 )
{
return false;
}
return substr( ".".$requestHost, 0 - strlen( $cookieDomain ) ) == $cookieDomain;
}
function _tokenize( $string, $separator = "" )
{
if ( !strcmp( $separator, "" ) )
{
$separator = $string;
$string = $this->nextToken;
}
$character = 0;
for ( ; $character < strlen( $separator ); ++$character )
{
if ( gettype( $position = strpos( $string, $separator[$character] ) ) == "integer" )
{
$found = isset( $found ) ? min( $found, $position ) : $position;
}
}
if ( isset( $found ) )
{
$this->nextToken = substr( $string, $found + 1 );
return substr( $string, 0, $found );
}
$this->nextToken = "";
return $string;
}
function _seterror( $error )
{
if ( $error != "" )
{
$this->error = $error;
return $error;
}
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -