Download the PHP package sirmonti/shttp without Composer
On this page you can find all versions of the php package sirmonti/shttp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sirmonti/shttp
More information about sirmonti/shttp
Files in sirmonti/shttp
Package shttp
Short Description Wrapper class around file_get_contents function to simplify http and https requests
License MIT
Homepage https://github.com/sirmonti/SHTTP
Informations about the package shttp
SHTTP - Simple HTTP client library
Static version rapper around file_get_contents function to simplify HTTP and HTTPS connections
Description
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.
This class is the static version of simpleHTTP which means there is no need to create an object, methods can be called directly.
Installation
Via composer
This class does not have any external dependency, which means you don't need to do a full installation, you can simply download and drop SHTTP.php file in your project folder.
Features
- Lightweigth
- Very simple interface
- You can disable certificate verification, to allow sites with self-signed or expired certificates
- In HTTPS connections, the certificate chain for the remote is retrieved
- Certificate authentication supported. You can provide your certificate to authenticate agains remote site
- Configurable exception level. You can disable exceptions, fire exceptions only on network errors, or fire exceptions on HTTP errors too
- Responses in PSR7 format. This feature requires an external package installed
Basic usage
This is only a brief explanation, the file SHTTP.md provide a more extensive method reference.
Complete documentation can be found Here.
There are four global parameters: exception level, certificate validation flag, extra HTTP headers and Certificate Authentication.
Certificate validation flag is set via "verifCERT" boolean property, exception level via setExceptionLevel() method, extra HTTP headers via setExtraHeaders() method and Certificate Authentication via setAuthCert() method.
The certificate validation is a boolean flag that enable or disable the validation of the remote certificate on https connections. If the validation is enabled, if the remote site has a faulty certificate (expired, autosigned or for another domain) the connection will be aborted and an error will be fired. In disabled, the error will be ignored and the connection will be executed normally.
There are three exception levels:
- 0: Exceptions are disabled. Errors only set status code responses.
- 1: Exception only on network errors. HTTP errors will be reported as status codes.
- 2: Exception will be fired on any error.
Default values is exception level 1 and certificate validation enabled.
Supported methods are: GET, POST, PUT, HEAD, DELETE and OPTIONS. The request call return the request body and the other data, like response headers, are stored internally and can be retrieved.
Here, the most basic example of using this class
Here, we modify certificate validation and exception level
This library can return responses in PSR7 format, but is neccesary to have a third party library installed. Currently, HttpSoft, Nyholm, Guzzle, Laminas-diactoros and Slim are supported. SHTTP detects which one is installed and will fire an error if none is installed.
PSR7 responses are useful because the main PHP frameworks use this format.
Example getting a PSR7 response
Examples
There are more examples in examples folder.
The most simple usage is to call requests directly
As you see, the object execute direct requests providing the URL.
Here, and example configuring exceptions and certificate acceptance
Here, a POST with a JSON encoded body
And here, a POST with an image as data body
But, What if you need to add an authentication header?
But, If we need an extra header only in a specific request?
The requests from this class use "SHTTP/7.0" as user agent. You can set your own one
Here, a PUT request example
But, How I can get the response headers and result status?
This code will produce this output
LICENSE
This library is licensed under MIT LICENSE