Download the PHP package pluggit/http-client without Composer

On this page you can find all versions of the php package pluggit/http-client. 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 http-client

Pluggit Http

Small PSR7 compatible library to perform http requests based on pre-configured set of requests

Scrutinizer Code Quality Code Coverage Build Status

TL;DR

Table of contents

Installation

Require the package as usual

To use Guzzle as a sender (the default option), you have to require it manually

Requirements

Note: This package relies on the implementation of guzzle for the PSR-7: HTTP message interfaces

Additional requirements

Guzzle

The library allows to use different senders to execute requests; and adapter for guzzlehttp/guzzle it's included in the library but you must require the dependency manually

Optional requirements

Yaml parsing

If you want to use a Yaml file as a configuration source you can: a) Use you own parser and pass the config as an array b) Require symfony/yaml as dependency and pass the yaml filepath to the builder

Logging

Do you want to know why your request is failing? No problem, the library accepts any Psr-3 logger logger implementation (like Monolog) to write debug and error messages.

For a quick start you can use Symfony's console package to have debug messages in the console

You will now be able to activate the console debug mode

Pimple

If you use pimple for building your dependencies you can use the provider Cmp\Http\Provider\HttpClientServiceProvider to register the multi client.

NOTE: See the annex for how to register the provider

Compatibility

This library has been tested with the following PHP versions and setups

PHP guzzlehttp/psr7 guzzlehttp/guzzle result
5.5.25 1.3.1 6.2.2 :white_check_mark:
5.6.16 1.3.1 6.2.2 :white_check_mark:
7.0.7 1.3.1 6.2.2 :white_check_mark:
hhvm 1.3.1 6.2.2 :white_check_mark:

Usage

The library allows to build requests from a set configuration values, execute them trough the client and get a response

Defining a configuration

The configuration is what tells the client how to build the requests, let's see some examples of configuration

The first line defines a name for a service, in this case my_users_api

NOTE: You can see a full-fledged sample configuration file in config-sample/requests.yaml for working with a REST API

Service configuration values

Request configuration values

The request configuration allows the set almost the same options as the service configuration, overwriting the values provided for the service, this allows to define a general behaviour for a service but tweak some options for specific requests, like for example, allowing a longer timeout

Underlying client options

The request options allows to customize the client behaviour without the library having to know specific details about how to handle them.

Taking the provided integration with Guzzle as examples, we could have:

Configuration placeholders

The library allows to indicate the presence of dynamic values in this configuration parameters as placeholders

Placeholder rules
Replacing a placeholder in the request

To replace the placeholders, pass the values as paramters when creating the request

Configuring the builder

The builder (Cmp\Http\ClientBuilder) is an object that will help you create you client, easing the customization process. This are the options available:

Once you've configured the builder you're ready to create clients for your http services

Building the client

There are 2 different clients that allow to create and execute requests:

NOTE: You should try to inject always a ServiceClientInterface in your services, this will prevent sideeffects triggering requests from other services by mistake

The available methods in the clients are:

They also provide some shortcuts to ease the use:

Building a multi client

The easiest way to built the client is to use the method build on the builder

Building a service client

To build a service client specify the name of the service when building the client in the last step of the builder

Customizing the builds

The client require 3 dependencies to work, the builder has methods to override all of them with custom implementations

Sender

This is the class that sends the requests, it has to implement the Cmp\Http\Sender\SenderInterface and return a Psr-7 compatible response

The builder will try to use the provided Cmp\Http\Sender\GuzzleSender by default, to specify a different one use:

Even if you want to use guzzle, ou can also customize the internal client used

Config

This is the only required dependency, a configuration defining the available the requests

If symfony/yaml is installed, you can pass a filepath with a yaml configuration

Logger

You can pass a Psr\Log\LoggerInterface to receive debug and error messages

If you want to debug the raw http request/responses in the console you can activate the debug output in the builder (You'll need to have symfony/console installed)

Creating a request

To create a request you need to identify the service and the request that you want. Additionally you can pass dynamic values to substitute placeholders

The requests created are Cmp\Http\Message\Request intances, implementing Psr\Http\Message\RequestInterface, making them suitable to share between libraries and frameworks

The library request object provides some helpers to work with them in an easier way:

Customising the request creation

If you provide a request factory to the builder that complais with Cmp\Http\RequestFactoryInterface, you'll be able to extend the Request class and change the behaviour.

You may ask "hey, why would I like to do this for?"

For example to:

Sending the request

After sending the request you'll receive a Cmp\Http\Message\Response instance compatible with Psr\Http\Message\ResponseInterface

This response object also provides some helper methods:

Exceptions

Not everything works as expected; to make your life easier handling this, the library provides a small set of exceptions:


REST API integration example

I'm going to show you how to perform the most common operation in a REST API.
Lets imagine that we want to interact with an API endpoint to manage our application users. The user entity looks like:

Now let's define the typical requests in our configuration file

Building the API client

Let's build a service client for our api, as per the configuration, all the requests will include a basic authentication

List users

This call would return an array of users

Get a user

This call will inject the user id in the path of the uri to request the user

Create a user

Here we're going to replace the placeholders in the body array, if the api only accepts a json body, we have to add the correct content type header, like in the example so the request is created correctly

Replace a user

We can replace placeholders in both path or body params at the same time

Update a user

When we update some fields only, we don't known in advance what fields we are going to send, so it's better to decide later

Delete a user

Deleting a user is even easier


Annexes

Pimple Service Provider

The library includes a service provider for Pimple to register a client in an easy way: Cmp\Http\Provider\HttpClientServiceProvider The provider will register the builder Cmp\Http\ClientBuilder object at key http_client.builder and a general purpouse multi-client Cmp\Http\Client\MultiClient in http_client.client, but it also accepts alias for both services

The options that the provider accepts are:


Development environment

To build the test environment you'll need docker and docker-compose installed:

Running the tests

You can run the tests only for a php version like this

Code-coverage

You can build a report for code coverage in HTML format. It will be available in bin/code-coverage

Stop the environment

Delete the environment

You can delete the docker images for a total clean-up


All versions of http-client with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
psr/log Version ^2.0
guzzlehttp/psr7 Version ^1.0
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 pluggit/http-client contains the following files

Loading the files please wait ....