HTTP Client
Introduction
Mantle provides a fluent and expressive wrapper around the WordPress HTTP API allowing you to quickly make HTTP requests with external services. The HTTP Client combined with support for testing and faking Remote Requests provides you a full solution for making and testing external HTTP requests end-to-end.
Making Requests
The HTTP Client is installed by default with Mantle. It also supports being used outside of the framework.
- Framework Use
- Standalone Use
The Http Client is included with Mantle out of the box.
Making requests with the HTTP Client can be done using any HTTP verb. Requests
can be made using the Http facade class or by instantiating Mantle\Http_Client\Http_Client
directly:
use Mantle\Facade\Http;
$response = Http::get( 'https://example.org/' );
You can also use other HTTP methods fluently on the Http facade:
use Mantle\Facade\Http;
$response = Http::post( 'https://example.org/', [
'name' => 'Mantle',
] );
Install the Http Client package via Composer:
composer require mantle-framework/http-client
Making requests with the HTTP Client can be done using any HTTP verb. Requests
can be made by instantiating Mantle\Http_Client\Factory directly:
use Mantle\Http_Client\Factory;
$http = new Factory();
$response = $http->get( 'https://example.org/' );
// Or using a fluent interface:
$response = $http
->timeout( 10 )
->get( 'https://example.org/' );
Request Data
Many HTTP requests include some sort of a payload for POST, PUT, PATCH,
and DELETE methods. These methods access an array of data as their second
argument. By default these will be sent using JSON:
- Framework Use
- Standalone Use
use Mantle\Facade\Http;
Http::post( 'http://example.org/endpoint', [
'name' => 'Mantle',
] );
use Mantle\Http_Client\Factory;
Factory::create()->post( 'http://example.org/endpoint', [
'name' => 'Mantle',
] );
Sending a Body
Requests can also be made using a form body or a raw body:
- Framework Use
- Standalone Use
use Mantle\Facade\Http;
Http::as_form()->post( 'http://example.org/endpoint', [
'name' => 'Mantle',
] );
Http::with_body( 'raw-body' )->post( 'http://example.org/endpoint' );
use Mantle\Http_Client\Factory;
Factory::create()->as_form()->post( 'http://example.org/endpoint', [
'name' => 'Mantle',
] );
Factory::create()->with_body( 'raw-body' )->post( 'http://example.org/endpoint' );
GET Request Query Parameters
When making GET requests, you can either pass a URL with a query string
directly or pass an array of key / value pairs as the second argument:
- Framework Use
- Standalone Use
use Mantle\Facade\Http;
// The actual URL will be https://example.org/query?name=mantle
Http::get( 'https://example.org/query', [
'name' => 'mantle',
] );
use Mantle\Http_Client\Factory;
// The actual URL will be https://example.org/query?name=mantle
Factory::create()->get( 'https://example.org/query', [
'name' => 'mantle',
] );
Headers
Headers can be added to a request using the with_header() and with_headers()
methods:
- Framework Use
- Standalone Use
Http::with_headers( [
'X-Api-Key' => 'password',
] )->post( 'https://example.org' );
Http::with_header( 'X-Special-Header', 'value' )->post( 'https://example.org' );
use Mantle\Http_Client\Factory;
Factory::create()->with_headers( [
'X-Api-Key' => 'password',
] )->post( 'https://example.org' );
Factory::create()->with_header( 'X-Special-Header', 'value' )->post( 'https://example.org' );
The accept method can be used to specify the content type that your
application is expecting in response:
- Framework Use
- Standalone Use
Http::accept( 'text/plain' )->post( 'https://example.org' );
Http::accept_json()->post( 'https://example.org' );
use Mantle\Http_Client\Factory;
Factory::create()->accept( 'text/plain' )->post( 'https://example.org' );
Factory::create()->accept_json()->post( 'https://example.org' );