Documentation

simpleHTTP
in package

Wrapper class around file_get_contents function. This class is not intended to compete with full featured network frameworks, as Guzzle or Swoole, but to provide a simple and convenient solution to use web services or access web resources

Tags
see
https://github.com/sirmonti/simplehttp/

simpleHTTP github project

author

Francisco Monteagudo francisco@monteagudo.net

version
8.0.0
license

https://opensource.org/licenses/MIT (MIT License)

copyright

(c) 2024, Francisco Monteagudo A ver

Table of Contents

Properties

$followRedirs  : bool
If request returns a redirection, it must be followed.
$maxfollows  : int
How many redirections must be followed before a "Many redirections" error must be fired
$reqFullURI  : bool
On the request command, send the full URI instead the path.
$timeout  : float
Connection timeout. Connection closes if exceds timeout without response. Default value is ten seconds.
$verifCERT  : bool
Enable/Disable certificate verification on https connections.
$exceptlevel  : int
Exception level. You can edit this value to change default value

Methods

__construct()  : mixed
Constructor
delete()  : string
Do a DELETE HTTP request
get()  : string
Do a GET HTTP request
getCertchain()  : array<string|int, mixed>
Get the certificate chain from the visited site
getExceptionLevel()  : int
Get the configured exception level
getExtraHeaders()  : array<int, string>
Get the extra headers, if any
getPeerCert()  : OpenSSLCertificate|null
Get the peer certificate from the visited site
getProxy()  : mixed
Get the proxy parameters
getSendBody()  : string
Get the body that has been sent on last request
getSendHeaders()  : array<string|int, mixed>
Get the headers that has been sent on last request
head()  : string
Do a HEAD HTTP request
options()  : string
Do an OPTIONS HTTP request
patch()  : string
Do a PATCH HTTP request
patchJSON()  : string
Do a PATCH HTTP request with the body data in JSON format
patchRAW()  : string
Do a PATCH HTTP request with the body in a custom format
post()  : string
Do a POST HTTP request
postJSON()  : string
Do a POST HTTP request with the body data in JSON format
postRAW()  : string
Do a POST HTTP request with the body in a custom format
protocolVersion()  : string
Get the protocol version for the las HTTP request
PSRResponse()  : ResponseInterface
Retrieve a PSR7 Response
put()  : string
Do a PUT HTTP request
putJSON()  : string
Do a PUT HTTP request with the body data in JSON format
putRAW()  : string
Do a PUT HTTP request with the body in a custom format
respBody()  : string
Get the data returned by the last HTTP request
respCode()  : int
Get the status code for the last HTTP request
respHeaders()  : array<string, string>
Get the response headers for the last HTTP request
respMIME()  : string
Get the mime type of the response for the last HTTP request
respStatus()  : string
Get the status message for the last HTTP request
setAuthCert()  : mixed
Set local certificate/key pair to authenticate connections
setExceptionLevel()  : void
Set exception level
setExtraHeaders()  : mixed
Define a set of extra headers to be attached to following requests
setProxy()  : bool
Set the proxy server

Properties

$followRedirs

If request returns a redirection, it must be followed.

public bool $followRedirs = \true

$maxfollows

How many redirections must be followed before a "Many redirections" error must be fired

public int $maxfollows = 20

$reqFullURI

On the request command, send the full URI instead the path.

public bool $reqFullURI = \false

For example, instead send "GET /test.html HTTP/1.1" command to the server, script will send "GET http://www.example.com/test.html HTTP/1.1". Include full URI breaks standard, but is neccesary if connect to a proxy.

$timeout

Connection timeout. Connection closes if exceds timeout without response. Default value is ten seconds.

public float $timeout = 10.0

$verifCERT

Enable/Disable certificate verification on https connections.

public bool $verifCERT = \true

When connecting to a https site, the program verify if the certificate is valid and fires an error if not. Disabling certificate validation you can prevent this error and connect to sites with faulty certificate. You can edit this value to change default value.

$exceptlevel

Exception level. You can edit this value to change default value

private int $exceptlevel = 1

Expected values:

  • 0: No exceptions
  • 1: Exception only on network errors or invalid arguments
  • 2: Exception on HTTP errors (4XX and 5XX errors) too

Methods

__construct()

Constructor

public __construct([int $elevel = 1 ][, bool $verifCert = true ]) : mixed

There are two optional parameters, the interrupt level and the certificate verification flag. When connecting to a https site, the program verify if the certificate is valid and fires an error if not. Disabling certificate validation you can prevent this error and connect to sites with bogus certificate.

There are three interruption levels:

  • 0: No exceptions fired. Operations results are returned in httpcode and httpstatus
  • 1: Exceptions only on network errors or bad formed URLs. HTTP errors don't fire exceptions
  • 2: All errors fire an exception.
Parameters
$elevel : int = 1

(optiona) Set interruption level

$verifCert : bool = true

(optional) Enable or disable certificate verification

delete()

Do a DELETE HTTP request

public delete(string $url[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

URL to retrieve

$headers : array<string, string> = []

Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Data retrieved

get()

Do a GET HTTP request

public get(string $url[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

URL to retrieve

$headers : array<string, string> = []

Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Data retrieved

getCertchain()

Get the certificate chain from the visited site

public getCertchain() : array<string|int, mixed>

When connecting to a https site, the certificate chain for the remote site is retrieved, allowing extra validations. This method returns an array with the complete certificate chain of the visited site. The certificates can be proccesed with the openssl_x509_* set of functions.

Return values
array<string|int, mixed>

Certificate chain

getExceptionLevel()

Get the configured exception level

public getExceptionLevel() : int
Return values
int

Configured exception level

getExtraHeaders()

Get the extra headers, if any

public getExtraHeaders() : array<int, string>
Return values
array<int, string>

Configured extra headers

getPeerCert()

Get the peer certificate from the visited site

public getPeerCert() : OpenSSLCertificate|null

When connecting to a https site, the certificate chain for the remote site is retrieved, allowing extra validations. This method returns the certificate of the visited site. The certificate can be proccesed with the openssl_x509_* set of functions.

Return values
OpenSSLCertificate|null

Peer site certificate

getProxy()

Get the proxy parameters

public getProxy(string &$host, int &$port) : mixed
Parameters
$host : string

Filled with proxy host name or IP

$port : int

Filled with proxy port

getSendBody()

Get the body that has been sent on last request

public getSendBody() : string

If you call this method before any request, it will return an empty string.

Return values
string

Body sent on last request

getSendHeaders()

Get the headers that has been sent on last request

public getSendHeaders() : array<string|int, mixed>

If you call this method before any request, it will return default headers.

Return values
array<string|int, mixed>

Header sent on last request

head()

Do a HEAD HTTP request

public head(string $url[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

URL to retrieve

$headers : array<string, string> = []

Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Data retrieved

options()

Do an OPTIONS HTTP request

public options(string $url[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

URL to retrieve

$headers : array<string, string> = []

Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Data retrieved

patch()

Do a PATCH HTTP request

public patch(string $url, array<string|int, mixed> $data[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

POST destination URL

$data : array<string|int, mixed>

Associative array with POST parameters

$headers : array<string, string> = []

(optional) Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Response data

patchJSON()

Do a PATCH HTTP request with the body data in JSON format

public patchJSON(string $url, mixed $data[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

POST destination URL

$data : mixed

Data to include in the body

$headers : array<string, string> = []

(optional) Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Response data

patchRAW()

Do a PATCH HTTP request with the body in a custom format

public patchRAW(string $url, string $mime, mixed $data[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

POST destination URL

$mime : string

MIME type of the data

$data : mixed

Data to include in the body

$headers : array<string, string> = []

(optional) Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Response data

post()

Do a POST HTTP request

public post(string $url, array<string, mixed> $data[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

POST destination URL

$data : array<string, mixed>

Associative array with POST parameters

$headers : array<string, string> = []

(optional) Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Response data

postJSON()

Do a POST HTTP request with the body data in JSON format

public postJSON(string $url, mixed $data[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

POST destination URL

$data : mixed

Data to include in the body

$headers : array<string, string> = []

(optional) Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Response data

postRAW()

Do a POST HTTP request with the body in a custom format

public postRAW(string $url, string $mime, mixed $data[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

POST destination URL

$mime : string

MIME type of the data

$data : mixed

Data to include in the body

$headers : array<string, string> = []

(optional) Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Response data

protocolVersion()

Get the protocol version for the las HTTP request

public protocolVersion() : string
Return values
string

Protocol version

PSRResponse()

Retrieve a PSR7 Response

public PSRResponse() : ResponseInterface

This method return the result for the last request in a PSR7 message. To use this method you must have installed one of the following packages: httpsoft/http-message, nyholm/psr7, guzzle/psr7, laminas/laminas-diactoros or slim/psr7

This method fires an Error if there isn't any PSR7 package installed

Tags
throws
Error

If there isn't any PSR7 package installed

Return values
ResponseInterface

Message in PSR7 format

put()

Do a PUT HTTP request

public put(string $url, array<string, mixed> $data[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

POST destination URL

$data : array<string, mixed>

Associative array with POST parameters

$headers : array<string, string> = []

(optional) Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Response data

putJSON()

Do a PUT HTTP request with the body data in JSON format

public putJSON(string $url, mixed $data[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

POST destination URL

$data : mixed

Data to include in the body

$headers : array<string, string> = []

(optional) Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Response data

putRAW()

Do a PUT HTTP request with the body in a custom format

public putRAW(string $url, string $mime, mixed $data[, array<string, string> $headers = [] ]) : string
Parameters
$url : string

POST destination URL

$mime : string

MIME type of the data

$data : mixed

Data to include in the body

$headers : array<string, string> = []

(optional) Extra HTTP headers

Tags
throws
InvalidArgumentException

on invalid parameters

throws
RuntimeException

on network error

Return values
string

Response data

respBody()

Get the data returned by the last HTTP request

public respBody() : string
Return values
string

HTTP response

respCode()

Get the status code for the last HTTP request

public respCode() : int

Normally, the status code is the return code from the HTTP connection (200,404,500, ..), but this class adds two extra codes:

  • -1: Invalid schema. Only http:// and https:// is supported
  • -2: Invalid argument. Data passed to the method call is not valid
  • -3: Network error. Network connection failed
Return values
int

Status code

respHeaders()

Get the response headers for the last HTTP request

public respHeaders() : array<string, string>
Return values
array<string, string>

Headers

respMIME()

Get the mime type of the response for the last HTTP request

public respMIME() : string
Return values
string

Response data mime type

respStatus()

Get the status message for the last HTTP request

public respStatus() : string
Return values
string

Status message

setAuthCert()

Set local certificate/key pair to authenticate connections

public setAuthCert(string $certfile[, string $keyfile = '' ][, string $passphrase = '' ]) : mixed

The parameters are the paths to the files containing the certificates encoded in PEM format. If the certificate and the private key are stored in different files, you must provide both.

Parameters
$certfile : string

File with the certificate in PEM format

$keyfile : string = ''

(optional) File with the private key in PEM format

$passphrase : string = ''

(optional) Passphrase if keys are encrypted

setExceptionLevel()

Set exception level

public setExceptionLevel(int $level) : void

This method configures the use of exceptions on an error. There are three exception levels

  • 0: No exceptions fired. Operations results are returned in httpcode and httpstatus
  • 1: Exceptions only on network errors or bad formed URLs. HTTP errors don't fire exceptions
  • 2: All errors fire an exception.
Parameters
$level : int

Exception level

setExtraHeaders()

Define a set of extra headers to be attached to following requests

public setExtraHeaders([array<int, string> $headers = [] ]) : mixed
Parameters
$headers : array<int, string> = []

Extra headers to set

setProxy()

Set the proxy server

public setProxy([string $host = '' ][, int $port = 8080 ]) : bool

You provide the host name or IP address and port

Parameters
$host : string = ''

Proxy host

$port : int = 8080

Proxy port

Return values
bool

Proxy has been set OK


        
On this page

Search results