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:
- https://github.com/sirmonti/simplehttp/ - simpleHTTP github project
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.
If request returns a redirection, it must be followed.
public bool $followRedirs
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.
How many redirections must be followed before a "Many redirections" error must be fired
public int $maxfollows
Connection timeout. Connection closes if exceds timeout without response. Default value is ten seconds.
public float $timeout
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
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 |
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 |
Get the configured exception level
public getExceptionLevel(): int
Return Value:
Configured exception level
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
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 |
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 |
Get the extra headers, if any
public getExtraHeaders(): array<int,string>
Return Value:
Configured extra headers
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
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
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
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
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:
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 |
Get the protocol version for the las HTTP request
public protocolVersion(): string
Return Value:
Protocol version
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
Get the status message for the last HTTP request
public respStatus(): string
Return Value:
Status message
Get the response headers for the last HTTP request
public respHeaders(): array<string,string>
Return Value:
Headers
Get the mime type of the response for the last HTTP request
public respMIME(): string
Return Value:
Response data mime type
Get the data returned by the last HTTP request
public respBody(): string
Return Value:
HTTP response
Do a GET HTTP request
public get(string $url, array<string,string> $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
Do a POST HTTP request
public post(string $url, array<string,mixed> $data, array<string,string> $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
Do a POST HTTP request with the body data in JSON format
public postJSON(string $url, mixed $data, array<string,string> $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
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:
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
Do a PUT HTTP request
public put(string $url, array<string,mixed> $data, array<string,string> $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
Do a PUT HTTP request with the body data in JSON format
public putJSON(string $url, mixed $data, array<string,string> $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
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:
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
Do a PATCH HTTP request
public patch(string $url, array $data, array<string,string> $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
Do a PATCH HTTP request with the body data in JSON format
public patchJSON(string $url, mixed $data, array<string,string> $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
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:
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
Do a HEAD HTTP request
public head(string $url, array<string,string> $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
Do a DELETE HTTP request
public delete(string $url, array<string,string> $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
Do an OPTIONS HTTP request
public options(string $url, array<string,string> $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
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