Download the PHP package vanilla/garden-http without Composer

On this page you can find all versions of the php package vanilla/garden-http. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package garden-http

Garden HTTP

CI Tests Packagist Version CLA

Garden HTTP is an unbloated HTTP client library for building RESTful API clients. It's meant to allow you to access people's APIs without having to copy/paste a bunch of cURL setup and without having to double the size of your codebase. You can use this library as is for quick API clients or extend the HttpClient class to make structured API clients that you use regularly.

Installation

Garden HTTP requires PHP 7.4 or higher and libcurl

Garden HTTP is PSR-4 compliant and can be installed using composer. Just add vanilla/garden-http to your composer.json.

Garden request and response objects are PSR-7 compliant as well.

Basic Example

Almost all uses of Garden HTTP involve first creating an HttpClient object and then making requests from it. You can see below a default header is also set to pass a standard header to every request made with the client.

Throwing Exceptions

You can tell the HTTP client to throw an exception on unsuccessful requests.

Exceptions will be thrown with a message indicating the failing response and structured data as well.

Basic Authentication

You can specify a username and password for basic authentication using the auth option.

Extending the HttpClient through subclassing

If you are going to be calling the same API over and over again you might want to extend the HttpClient class to make an API client that is more convenient to reuse.

Extending the HttpClient with middleware

The HttpClient class has an addMiddleware() method that lets you add a function that can modify the request and response before and after being sent. Middleware lets you develop a library of reusable utilities that can be used with any client. Middleware is good for things like advanced authentication, caching layers, CORS support, etc.

Writing middleware

Middleware is a callable that accepts two arguments: an HttpRequest object, and the next middleware. Each middleware must return an HttpResponse object.

You have to call $next or else the request won't be processed by the HttpClient. Of course, you may want to short circuit processing of the request in the case of a caching layer so in that case you can leave out the call to $next.

Example: Modifying the request with middleware

Consider the following class that implements HMAC SHA256 hashing for a hypothetical API that expects more than just a static access token.

This middleware calculates a new authorization header for each request and then adds it to the request. It then calls the $next closure to perform the rest of the request.

The HttpHandlerInterface

In Garden HTTP, requests are executed with an HTTP handler. The currently included default handler executes requests with cURL. However, you can implement the the HttpHandlerInterface however you want and completely change the way requests are handled. The interface includes only one method:

The method is supposed to transform a request into a response. To use it, just pass an HttpRequest object to it.

You can also use your custom handler with the HttpClient. Just pass it to the constructor:

Inspecting requests and responses

Sometimes when you get a response you want to know what request generated it. The HttpResponse class has an getRequest() method for this. The HttpRequest class has a getResponse() method for the inverse.

Exceptions that are thrown from HttpClient objects are instances of the HttpResponseException class. That class has getRequest() and getResponse() methods so that you can inspect both the request and the response for the exception. This exception is of particular use since request objects are created inside the client and not by the programmer directly.

Mocking for Tests

An HttpHandlerInterface implementation and utilities are provided for mocking requests and responses.

Setup

Mocking Requests

Response Sequences

Anywhere you can use a mocked HttpResponse you can also use a MockHttpSequence.

Each item pushed into the sequence will return exactly once. Once that response has been returned it will not be returned again.

If the whole sequence is exhausted it will return 404 responses.

Response Functions

You can make a mock dynamic by providing a callable.

Assertions about requests

Some utilities are provided to make assertions against requests that were made. This can be particularly useful with a wildcard response.


All versions of garden-http with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
ext-curl Version *
ext-json Version *
vanilla/garden-utils Version ^1.1
psr/http-message Version >=1.0
slim/psr7 Version ^1.6
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package vanilla/garden-http contains the following files

Loading the files please wait ....