Skip to content

Latest commit

 

History

History
1207 lines (519 loc) · 16.6 KB

simpleHTTP.md

File metadata and controls

1207 lines (519 loc) · 16.6 KB

simpleHTTP

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

  • Full name: \simpleHTTP

See Also:

Properties

verifCERT

Enable/Disable certificate verification on https connections.

public bool $verifCERT

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.


followRedirs

If request returns a redirection, it must be followed.

public bool $followRedirs

reqFullURI

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

public bool $reqFullURI

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.


maxfollows

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

public int $maxfollows

timeout

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

public float $timeout

exceptlevel

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

private int $exceptlevel

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:

Parameter Type Description
$elevel int (optiona) Set interruption level
$verifCert bool (optional) Enable or disable certificate verification

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:

Parameter Type Description
$level int Exception level

getExceptionLevel

Get the configured exception level

public getExceptionLevel(): int

Return Value:

Configured exception level


setProxy

Set the proxy server

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

You provide the host name or IP address and port

Parameters:

Parameter Type Description
$host string Proxy host
$port int Proxy port

Return Value:

Proxy has been set OK


getProxy

Get the proxy parameters

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

Parameters:

Parameter Type Description
$host string Filled with proxy host name or IP
$port int Filled with proxy port

setExtraHeaders

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

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

Parameters:

Parameter Type Description
$headers array<int,string> Extra headers to set

getExtraHeaders

Get the extra headers, if any

public getExtraHeaders(): array&lt;int,string&gt;

Return Value:

Configured extra headers


getSendHeaders

Get the headers that has been sent on last request

public getSendHeaders(): array

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

Return Value:

Header sent on last request


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 Value:

Body sent on last request


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 Value:

Peer site certificate


getCertchain

Get the certificate chain from the visited site

public getCertchain(): array

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 Value:

Certificate chain


setAuthCert

Set local certificate/key pair to authenticate connections

public setAuthCert(string $certfile, string $keyfile = &#039;&#039;, string $passphrase = &#039;&#039;): 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:

Parameter Type Description
$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

protocolVersion

Get the protocol version for the las HTTP request

public protocolVersion(): string

Return Value:

Protocol version


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 Value:

Status code


respStatus

Get the status message for the last HTTP request

public respStatus(): string

Return Value:

Status message


respHeaders

Get the response headers for the last HTTP request

public respHeaders(): array&lt;string,string&gt;

Return Value:

Headers


respMIME

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

public respMIME(): string

Return Value:

Response data mime type


respBody

Get the data returned by the last HTTP request

public respBody(): string

Return Value:

HTTP response


get

Do a GET HTTP request

public get(string $url, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$url string URL to retrieve
$headers array<string,string> Extra HTTP headers

Return Value:

Data retrieved

Throws:

on invalid parameters

on network error


post

Do a POST HTTP request

public post(string $url, array&lt;string,mixed&gt; $data, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$url string POST destination URL
$data array<string,mixed> Associative array with POST parameters
$headers array<string,string> (optional) Extra HTTP headers

Return Value:

Response data

Throws:

on invalid parameters

on network error


postJSON

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

public postJSON(string $url, mixed $data, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$url string POST destination URL
$data mixed Data to include in the body
$headers array<string,string> (optional) Extra HTTP headers

Return Value:

Response data

Throws:

on invalid parameters

on network error


postRAW

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

public postRAW(string $url, string $mime, mixed $data, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$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

Return Value:

Response data

Throws:

on invalid parameters

on network error


put

Do a PUT HTTP request

public put(string $url, array&lt;string,mixed&gt; $data, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$url string POST destination URL
$data array<string,mixed> Associative array with POST parameters
$headers array<string,string> (optional) Extra HTTP headers

Return Value:

Response data

Throws:

on invalid parameters

on network error


putJSON

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

public putJSON(string $url, mixed $data, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$url string POST destination URL
$data mixed Data to include in the body
$headers array<string,string> (optional) Extra HTTP headers

Return Value:

Response data

Throws:

on invalid parameters

on network error


putRAW

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

public putRAW(string $url, string $mime, mixed $data, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$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

Return Value:

Response data

Throws:

on invalid parameters

on network error


patch

Do a PATCH HTTP request

public patch(string $url, array $data, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$url string POST destination URL
$data array Associative array with POST parameters
$headers array<string,string> (optional) Extra HTTP headers

Return Value:

Response data

Throws:

on invalid parameters

on network error


patchJSON

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

public patchJSON(string $url, mixed $data, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$url string POST destination URL
$data mixed Data to include in the body
$headers array<string,string> (optional) Extra HTTP headers

Return Value:

Response data

Throws:

on invalid parameters

on network error


patchRAW

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

public patchRAW(string $url, string $mime, mixed $data, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$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

Return Value:

Response data

Throws:

on invalid parameters

on network error


head

Do a HEAD HTTP request

public head(string $url, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$url string URL to retrieve
$headers array<string,string> Extra HTTP headers

Return Value:

Data retrieved

Throws:

on invalid parameters

on network error


delete

Do a DELETE HTTP request

public delete(string $url, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$url string URL to retrieve
$headers array<string,string> Extra HTTP headers

Return Value:

Data retrieved

Throws:

on invalid parameters

on network error


options

Do an OPTIONS HTTP request

public options(string $url, array&lt;string,string&gt; $headers = []): string

Parameters:

Parameter Type Description
$url string URL to retrieve
$headers array<string,string> Extra HTTP headers

Return Value:

Data retrieved

Throws:

on invalid parameters

on network error


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

Return Value:

Message in PSR7 format

Throws:

If there isn't any PSR7 package installed



Automatically generated on 2024-04-27