Download the PHP package sirmonti/simplehttp without Composer
On this page you can find all versions of the php package sirmonti/simplehttp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sirmonti/simplehttp
More information about sirmonti/simplehttp
Files in sirmonti/simplehttp
Package simplehttp
Short Description Wrapper class around file_get_contents function to simplify http and https requests
License MIT
Homepage https://github.com/sirmonti/simpleHTTP
Informations about the package simplehttp
simpleHTTP - Simple HTTP client library
Wrapper 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
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 simpleHTTP.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 simpleHTTP.md provide a more extensive method reference.
Complete documentation can be found Here.
You create an object of the simpleHTTP class. The constructor has two parameters, the exception level and the remote certificate validation.
Here we create an object with an exception level 2 and certificate validation disabled:
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 bogus 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.
About the exception levels, there are three 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, a basic example
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. simpleHTTP 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.
This is the most simple usage:
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 "simpleHTTP/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