Dotclear 2.24
Loading...
Searching...
No Matches
netHttp Class Reference

HTTP Client. More...

Inheritance diagram for netHttp:
netSocket HttpClient akismet dcStoreReader feedReader htmlValidator xmlrpcClient pingsAPI xmlrpcClientMulticall

Public Member Functions

 __construct ($host, int $port=80, ?int $timeout=null)
 
 get (string $path, $data=false)
 
 post (string $path, $data, ?string $charset=null)
 
 getStatus ()
 
 getContent ()
 
 getHeaders ()
 
 getHeader ($header)
 
 getCookies ()
 
 getRequestURL ()
 
 setHost (string $host, int $port=80)
 
 setProxy ($host, int $port=8080)
 
 setTimeout (int $timeout)
 
 setUserAgent (string $user_agent)
 
 setAuthorization (?string $username, ?string $password)
 
 setMoreHeader (string $header)
 
 voidMoreHeaders ()
 
 setCookies (array $cookies)
 
 useSSL (bool $flag)
 
 useGzip (bool $flag)
 
 setPersistCookies (bool $flag)
 
 setPersistReferers (bool $flag)
 
 setHandleRedirects (bool $flag)
 
 setMaxRedirects (int $num)
 
 setHeadersOnly (bool $flag)
 
 setDebug (bool $flag)
 
 setOutput (?string $out)
 
- Public Member Functions inherited from netSocket
 __construct (string $host, int $port, int $timeout=10)
 
 __destruct ()
 
 host (?string $host=null)
 
 port (?int $port=null)
 
 timeout (?int $timeout=null)
 
 setBlocking (bool $block)
 
 open ()
 
 close ()
 
 write ($data)
 
 flush ()
 
 isOpen ()
 

Static Public Member Functions

static quickGet (string $url, ?string $output=null)
 
static quickPost (string $url, array $data, ?string $output=null)
 
static initClient (string $url, string &$path)
 
static readURL (string $url, bool &$ssl, string &$host, int &$port, string &$path, string &$user, string &$pass)
 

Protected Member Functions

 buildQueryString ($data)
 
 doRequest ()
 
 buildRequest ()
 
 outputOpen ()
 
 outputClose ()
 
 outputWrite ($content)
 
 debug (string $msg, $object=false)
 
- Protected Member Functions inherited from netSocket
 iterator ()
 

Protected Attributes

 $host
 
 $port
 
 $path
 
 $method
 
 $postdata = ''
 
 $post_charset
 
 $cookies = []
 
 $referer
 
 $accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,image/webp,*/*'
 
 $accept_encoding = 'gzip'
 
 $accept_language = 'en-us'
 
 $user_agent = 'Clearbricks HTTP Client'
 
 $more_headers = []
 
 $timeout = 10
 
 $use_ssl = false
 
 $use_gzip = false
 
 $persist_cookies = true
 
 $persist_referers = true
 
 $debug = false
 
 $handle_redirects = true
 
 $max_redirects = 5
 
 $headers_only = false
 
 $username
 
 $password
 
 $proxy_host
 
 $proxy_port
 
 $status
 
 $status_string
 
 $headers = []
 
 $content = ''
 
 $redirect_count = 0
 
 $cookie_host = ''
 
 $output = null
 
 $output_h = null
 
- Protected Attributes inherited from netSocket
 $_host
 
 $_port
 
 $_transport = ''
 
 $_timeout
 
 $_handle
 

Detailed Description

HTTP Client.

Features:

  • Implements a useful subset of the HTTP 1.0 and 1.1 protocols.
  • Includes cookie support.
  • Ability to set the user agent and referal fields.
  • Can automatically handle redirected pages.
  • Can be used for multiple requests, with any cookies sent by the server resent for each additional request.
  • Support for gzip encoded content, which can dramatically reduce the amount of bandwidth used in a transaction.
  • Object oriented, with static methods providing a useful shortcut for simple requests.
  • The ability to only read the page headers - useful for implementing tools such as link checkers.
  • Support for file uploads.

This class is fully based on Simon Willison's HTTP Client in version 0.9 of 6th April 2003 - http://scripts.incutio.com/httpclient/

Changes since fork:

  • PHP7.4+ with Exception support
  • Charset support in POST requests
  • Proxy support through HTTP_PROXY_HOST and HTTP_PROXY_PORT or setProxy()
  • SSL support (if possible)
  • Handles redirects on other hosts
  • Configurable output

Constructor & Destructor Documentation

◆ __construct()

__construct (   $host,
int  $port = 80,
?int  $timeout = null 
)

Constructor.

Takes the web server host, an optional port and timeout.

Parameters
string$hostServer host
int$portServer port
int$timeoutConnection timeout (in seconds)

References $host, $port, $timeout, setHost(), setProxy(), and setTimeout().

Member Function Documentation

◆ get()

get ( string  $path,
  $data = false 
)

GET Request

Executes a GET request for the specified path. If $data is specified, appends it to a query string as part of the get request. $data can be an array of key value pairs, in which case a matching query string will be constructed. Returns true on success.

Parameters
string$pathRequest path
bool | array$dataRequest parameters
Returns
bool

References $path, buildQueryString(), and doRequest().

◆ post()

post ( string  $path,
  $data,
?string  $charset = null 
)

POST Request

Executes a POST request for the specified path. If $data is specified, appends it to a query string as part of the get request. $data can be an array of key value pairs, in which case a matching query string will be constructed. Returns true on success.

Parameters
string$pathRequest path
array | string$dataRequest parameters
string$charsetRequest charset
Returns
bool

References $path, buildQueryString(), and doRequest().

Referenced by akismet\callFunc(), htmlValidator\perform(), and akismet\verify().

◆ buildQueryString()

buildQueryString (   $data)
protected

Query String Builder

Prepares Query String for HTTP request. $data is an associative array of arguments.

Parameters
array | string$dataQuery data
Returns
string

Referenced by get(), and post().

◆ doRequest()

doRequest ( )
protected

◆ buildRequest()

buildRequest ( )
protected

Prepare Request

Prepares HTTP request and returns an array of HTTP headers.

Returns
array

Reimplemented in dcStoreReader, feedReader, and xmlrpcClient.

References $accept, $accept_encoding, $accept_language, $headers, $host, $path, $post_charset, $postdata, $referer, $user_agent, and getRequestURL().

Referenced by doRequest().

◆ outputOpen()

outputOpen ( )
protected

Open Output

Initializes output handler if $output property is not null and is a valid resource stream.

Referenced by doRequest().

◆ outputClose()

outputClose ( )
protected

Close Output

Closes output module if exists.

Referenced by doRequest().

◆ outputWrite()

outputWrite (   $content)
protected

Write Output

Writes data to output module.

Parameters
string$contentData content

References $content.

Referenced by doRequest().

◆ getStatus()

getStatus ( )

Get Status

Returns the status code of the response - 200 means OK, 404 means file not found, etc.

Returns
int

References $status.

Referenced by akismet\callFunc(), dcStoreReader\parse(), feedReader\parse(), htmlValidator\perform(), dcStoreReader\withCache(), and feedReader\withCache().

◆ getContent()

getContent ( )

Get Content

Returns the content of the HTTP response. This is usually an HTML document.

Returns
string

References $content.

Referenced by akismet\callFunc(), dcStoreReader\parse(), feedReader\parse(), htmlValidator\perform(), akismet\verify(), dcStoreReader\withCache(), and feedReader\withCache().

◆ getHeaders()

getHeaders ( )

Response Headers

Returns the HTTP headers returned by the server as an associative array.

Returns
array

References $headers.

◆ getHeader()

getHeader (   $header)

Response Header

Returns the specified response header, or false if it does not exist.

Parameters
string$headerHeader name
Returns
string|false

Referenced by doRequest().

◆ getCookies()

getCookies ( )

Cookies

Returns an array of cookies set by the server.

Returns
array

References $cookies.

◆ getRequestURL()

getRequestURL ( )

Request URL

Returns the full URL that has been requested.

Returns
string

References $path, $port, netSocket\host(), and netSocket\port().

Referenced by buildRequest(), xmlrpcClient\buildRequest(), and doRequest().

◆ setHost()

setHost ( string  $host,
int  $port = 80 
)

Sets server host and port.

Parameters
string$hostServer host
int$portServer port

References $host, $port, netSocket\host(), and netSocket\port().

Referenced by __construct(), doRequest(), feedReader\getFeed(), and dcStoreReader\getModulesXML().

◆ setProxy()

setProxy (   $host,
int  $port = 8080 
)

Sets proxy host and port.

Parameters
string$hostProxy host
int$portProxy port

References $host, and $port.

Referenced by __construct().

◆ setTimeout()

setTimeout ( int  $timeout)

Sets connection timeout.

Parameters
int$timeoutConnection timeout (in seconds)

References $timeout, and netSocket\timeout().

Referenced by __construct(), and dcStoreReader\__construct().

◆ setUserAgent()

setUserAgent ( string  $user_agent)

User Agent String

Sets the user agent string to be used in the request. Default is "Clearbricks HTTP Client".

Parameters
string$user_agentUser agent string

References $user_agent.

Referenced by dcStoreReader\__construct().

◆ setAuthorization()

setAuthorization ( ?string  $username,
?string  $password 
)

HTTP Authentication

Sets the HTTP authorization username and password to be used in requests. Don't forget to unset this in subsequent requests to different servers.

Parameters
string$usernameUser name
string$passwordPassword

References $password, and $username.

Referenced by xmlrpcClient\__construct(), doRequest(), feedReader\getFeed(), and dcStoreReader\getModulesXML().

◆ setMoreHeader()

setMoreHeader ( string  $header)

Add Header

Sets additionnal header to be sent with the request.

Parameters
string$headerFull header definition

Referenced by htmlValidator\perform().

◆ voidMoreHeaders()

voidMoreHeaders ( )

Empty additionnal headers

◆ setCookies()

setCookies ( array  $cookies)

Set Cookies

Sets the cookies to be sent in the request. Takes an array of name value pairs.

Parameters
array$cookiesCookies array

References $cookies.

◆ useSSL()

useSSL ( bool  $flag)

Enable / Disable SSL

Sets SSL connection usage.

Parameters
bool$flagEnable/Disable SSL

Referenced by xmlrpcClient\__construct(), doRequest(), feedReader\getFeed(), and dcStoreReader\getModulesXML().

◆ useGzip()

useGzip ( bool  $flag)

Use Gzip

Specifies if the client should request gzip encoded content from the server (saves bandwidth but can increase processor time). Default behaviour is false.

Parameters
bool$flagEnable/Disable Gzip

◆ setPersistCookies()

setPersistCookies ( bool  $flag)

Persistant Cookies

Specify if the client should persist cookies between requests. Default behaviour is true.

Parameters
bool$flagEnable/Disable Persist Cookies

◆ setPersistReferers()

setPersistReferers ( bool  $flag)

Persistant Referrers

Specify if the client should use the URL of the previous request as the referral of a subsequent request. Default behaviour is true.

Parameters
bool$flagEnable/Disable Persistant Referrers

◆ setHandleRedirects()

setHandleRedirects ( bool  $flag)

Enable / Disable Redirects

Specify if the client should automatically follow redirected requests. Default behaviour is true.

Parameters
bool$flagEnable/Disable Redirects

◆ setMaxRedirects()

setMaxRedirects ( int  $num)

Maximum Redirects

Set the maximum number of redirects allowed before the client quits (mainly to prevent infinite loops) Default is 5.

Parameters
int$numMaximum redirects value

◆ setHeadersOnly()

setHeadersOnly ( bool  $flag)

Headers Only

If true, the client only retrieves the headers from a page. This could be useful for implementing things like link checkers. Defaults to false.

Parameters
bool$flagEnable/Disable Headers Only

◆ setDebug()

setDebug ( bool  $flag)

Debug mode

Should the client run in debug mode? Default behaviour is false.

Parameters
bool$flagEnable/Disable Debug Mode

References debug().

◆ setOutput()

setOutput ( ?string  $out)

Set Output

Output module init. If $out is null, then output will be directed to STDOUT.

Parameters
string | null$outOutput stream

◆ quickGet()

static quickGet ( string  $url,
?string  $output = null 
)
static

Quick Get

Static method designed for running simple GET requests. Returns content or false on failure.

Parameters
string$urlRequest URL
string$outputOptionnal output stream
Returns
string|false

References $output, and $path.

◆ quickPost()

static quickPost ( string  $url,
array  $data,
?string  $output = null 
)
static

Quick Post

Static method designed for running simple POST requests. Returns content or false on failure.

Parameters
string$urlRequest URL
array$dataArray of parameters
string$outputOptionnal output stream
Returns
string|false

References $output, and $path.

◆ initClient()

static initClient ( string  $url,
string &  $path 
)
static

Quick Init

Returns a new instance of the class. $path is an output variable.

Parameters
string$urlRequest URL
string$pathResulting path
Returns
netHttp|false

References $host, $path, and $port.

Referenced by dcUpdate\download(), dcStore\download(), dcUpdate\getVersionInfo(), dcFilterFairTrackbacks\isSpam(), and adminLangs\process().

◆ readURL()

static readURL ( string  $url,
bool &  $ssl,
string &  $host,
int &  $port,
string &  $path,
string &  $user,
string &  $pass 
)
static

Read URL

Parses an URL and fills $ssl, $host, $port, $path, $user and $pass variables. Returns true on succes.

Parameters
string$urlRequest URL
boolean$ssltrue if HTTPS URL
string$hostHost name
int$portServer Port
string$pathPath
string$userUsername
string$passPassword
Returns
boolean

References $host, $path, and $port.

◆ debug()

debug ( string  $msg,
  $object = false 
)
protected

Debug

This method is the method the class calls whenever there is debugging information available. $msg is a debugging message and $object is an optional object to be displayed (usually an array). Default behaviour is to display the message and the object in a red bordered div. If you wish debugging information to be handled in a different way you can do so by creating a new class that extends netHttp and over-riding the debug() method in that class.

Parameters
string$msgDebug message
mixed$objectVariable to print_r

References debug().

Referenced by debug(), doRequest(), and setDebug().

Field Documentation

◆ $host

◆ $port

◆ $path

◆ $method

$method
protected

◆ $postdata

$postdata = ''
protected

Referenced by buildRequest().

◆ $post_charset

$post_charset
protected

Referenced by buildRequest().

◆ $cookies

$cookies = []
protected

Referenced by doRequest(), getCookies(), and setCookies().

◆ $referer

$referer
protected

Referenced by buildRequest(), and akismet\callFunc().

◆ $accept

$accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,image/webp,*/*'
protected

Referenced by buildRequest().

◆ $accept_encoding

$accept_encoding = 'gzip'
protected

Referenced by buildRequest().

◆ $accept_language

$accept_language = 'en-us'
protected

Referenced by buildRequest().

◆ $user_agent

$user_agent = 'Clearbricks HTTP Client'
protected

◆ $more_headers

$more_headers = []
protected

◆ $timeout

$timeout = 10
protected

Referenced by __construct(), and setTimeout().

◆ $use_ssl

$use_ssl = false
protected

◆ $use_gzip

$use_gzip = false
protected

◆ $persist_cookies

$persist_cookies = true
protected

◆ $persist_referers

$persist_referers = true
protected

◆ $debug

$debug = false
protected

◆ $handle_redirects

$handle_redirects = true
protected

◆ $max_redirects

$max_redirects = 5
protected

◆ $headers_only

$headers_only = false
protected

◆ $username

$username
protected

Referenced by setAuthorization().

◆ $password

$password
protected

Referenced by setAuthorization().

◆ $proxy_host

$proxy_host
protected

Referenced by doRequest().

◆ $proxy_port

$proxy_port
protected

Referenced by doRequest().

◆ $status

$status
protected

Referenced by getStatus().

◆ $status_string

$status_string
protected

◆ $headers

◆ $content

◆ $redirect_count

$redirect_count = 0
protected

◆ $cookie_host

$cookie_host = ''
protected

◆ $output

$output = null
protected

Referenced by quickGet(), and quickPost().

◆ $output_h

$output_h = null
protected

The documentation for this class was generated from the following file: