Download the PHP package neon-php/simple-http without Composer
On this page you can find all versions of the php package neon-php/simple-http. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package simple-http
simple-http
Installation
Introduction
simple-http provides a small interface to create really simple http-requests using the Guzzle-HTTP-Client.
Usage
Making Requests
The easiest way to make a new request is by using the static facade class Neon\Http\Facade\Http. This class provides static methods to create a new request instance of the class Neon\Http\Http.
In the simplest way you can make a request by calling a method for the corresponding http verb:
The following methods and their corresponding http verbs are supported:
Setting base url
By using the method setBaseURL you can set a base url that will be used for all following requests:
Framework request methods
Some frameworks may need a special request parameter to be sent to determine the request method. For example, sometimes a put-request is basically a post-request which contains to following request parameter.
To shorten this process the use of framework methods the corresponding functionality can be activated by using the method setFrameworkMethod:
Request inputs
To add input value to your request (post, put, patch, delete) you can call the addParam method to provide key/value pairs before calling the request method:
Alternatively you can add an array as the second parameter of the request method to provide multiple key/value pairs:
To provide query parameters for GET requests, you need to provide the query-key to the input array:
Adding request headers
To add additional request headers you can call the addHeader method before calling the request method:
To quickly add a bearer token to the authorization header you can use the bearer method:
Adding files
Files can be added via the file method. Just provide a key, the file name and the file location:
Response Handling
After making a request, the corresponding method will return an instance of Neon\Http\Response. This object provides several methods to work with the response.
Headers
The method hasHeader checks if a provided header key exists in the response:
The method getHeader returns the header values of the given header key. The values are returned as an array of strings:
Status Code
The method code returns the http status code of the response:
Additionally, there are several methods to check for specific pre-defined status codes:
Response body
The method body returns the response body as a string:
The method bodyRaw returns the response body as an instance of Psr\Http\Message\StreamInterface:
The method json converts the body string to a php array and returns it. The method throws an Neon\Http\Exceptions\RequestException if the body is not a valid json string.
Error Handling
The request methods get, post, put, patch and delete each throw an Neon\Http\Exceptions\RequestException if the request was, for whatever reason, unsuccessfull.