See our review from 7 of the best Open Source Http Client Libraries written in PHP.

Top 7: Best PHP Http Client Libraries

A lot of web applications rely on services provided by third party application through REST APIs. Those apps offer certain endpoints where you can retrieve, update or delete information according to the data, method of your request etc. This however can't be made in your server without a decent Http client that executes the requests for you. There are a lot of libraries that achieve this, so what's the right one for you? According to your requirements, you need to make a desicion by yourself based on the features that the library offers. In this article, we'll share with you the best 7 Http client libraries for PHP that will help you to communicate with applications around the world through APIs easily.

7. Yii2 Httpclient

Github

The Yii2 Httpclient is an extension of the Yii Framework and allows you to implement an useful Http client with a couple of lines of code. In order to send HTTP requests, you'll need to instantiate [[\yii\httpclient\Client]] and use its createRequest() method to create new HTTP request. Then you should configure all request parameters according to your goal and send request. As the result you'll get a [[\yii\httpclient\Response]] instance, which holds all response information and data. For example:

use yii\httpclient\Client;

$client = new Client();
$response = $client->createRequest()
    ->setMethod('POST')
    ->setUrl('http://example.com/api/1.0/users')
    ->setData(['name' => 'John Doe', 'email' => '[email protected]'])
    ->send();
if ($response->isOk) {
    $newUserId = $response->data['id'];
}

6. Buzz

Github

Buzz is a lightweight PHP 5.3 library for issuing HTTP requests. Buzz was created by Kris Wallsmith back in 2010. The project grown very popular over the years with more than 7 million downloads. Since August 2017 Tobias Nyholm is maintaining this library. The idea of Buzz will still be the same, we should have a simple API and mimic browser behavior for easy testing. We should not reinvent the wheel and we should not be as powerful and flexible as other clients (ie Guzzle). We do, however, take performance very seriously.

We do love PSRs and this is a wish list of what PSR we would like to support:

  • PSR-1 (Code style)
  • PSR-2 (Code style)
  • PSR-4 (Auto loading)
  • PSR-7 (HTTP messages)
  • PSR-15 (HTTP middlewares)
  • PSR-17 (HTTP factories)
  • PSR-18 (HTTP client)

Buzz syntax is as well friendly and easy to write:

<?php

$browser = new Buzz\Browser();
$response = $browser->get('http://www.google.com');

echo $browser->getLastRequest()."\n";
// $response is an object. 
// You can use $response->getContent() or $response->getHeaders() to get only one part of the response.
echo $response; 

5. Requests

Github

Requests is a HTTP library written in PHP, exclusively written to be easy to use. It is roughly based on the API from the excellent Requests Python library. Requests is ISC Licensed (similar to the new BSD license) and has no dependencies, except for PHP 5.2+. Despite PHP’s use as a language for the web, its tools for sending HTTP requests are severely lacking. cURL has an interesting API, to say the least, and you can’t always rely on it being available. Sockets provide only low level access, and require you to build most of the HTTP response parsing yourself. It's syntax is pretty friendly and easy to use:

$headers = array('Accept' => 'application/json');
$options = array('auth' => array('user', 'pass'));
$request = Requests::get('https://api.github.com/gists', $headers, $options);

var_dump($request->status_code);
// int(200)

var_dump($request->headers['content-type']);
// string(31) "application/json; charset=utf-8"

var_dump($request->body);
// string(26891) "[...]"

Requests allows you to send HEAD, GET, POST, PUT, DELETE, and PATCH HTTP requests. You can add headers, form data, multipart files, and parameters with simple arrays, and access the response data in the same way. Requests uses cURL and fsockopen, depending on what your system has available, but abstracts all the nasty stuff out of your way, providing a consistent API.

4. Httplug

Github

PHP-HTTP is the next step in standardizing HTTP interaction for PHP packages. It builds on top of PSR-7, which defines interfaces for HTTP requests and responses. PSR-7 does not describe, however, the way you should create requests or send them. PHP-HTTP aims to fill that gap by offering an HTTP client interface: HTTPlug.

PHP-HTTP has three goals:

  1. Encourage package developers to depend on the simple HTTPlug interface instead of concrete HTTP clients.
  2. Provide good quality HTTP-related packages to the PHP community.
  3. Over time, make HTTPlug a PHP Standards Recommendation (PSR) so that clients will directly implement the HTTPlug interface and our adapters are no longer needed.

HTTPlug abstracts from HTTP clients written in PHP, offering a simple interface. It also provides an implementation-independent plugin system to build pipelines regardless of the HTTP client implementation used.

3. Httpful

Github

Httpful is a simple, chainable, readable PHP library intended to make speaking HTTP sane. It lets the developer focus on interacting with APIs instead of sifting through curl set_opt pages and is an ideal PHP REST client. Httpful features:

  • Readable HTTP Method Support (GET, PUT, POST, DELETE, HEAD, and OPTIONS)
  • Custom Headers
  • Automatic "Smart" Parsing
  • Automatic Payload Serialization
  • Basic Auth
  • Client Side Certificate Auth
  • Request "Templates"
// Make a request to the GitHub API with a custom
// header of "X-Trvial-Header: Just as a demo".
$url = "https://api.github.com/users/nategood";
$response = \Httpful\Request::get($url)
    ->expectsJson()
    ->withXTrivialHeader('Just as a demo')
    ->send();
 
echo "{$response->body->name} joined GitHub on " .
                        date('M jS', strtotime($response->body->created_at)) ."\n";

2. Unirest PHP

Github

Unirest is a set of lightweight HTTP libraries available in multiple languages, built and maintained by Mashape, who also maintain the open-source API Gateway Kong. This library features:

  • Utility methods to call GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH requests
  • Supports form parameters, file uploads and custom body entities
  • Supports gzip
  • Supports Basic, Digest, Negotiate, NTLM Authentication natively
  • Customizable timeout
  • Customizable default headers for every request (DRY)
  • Automatic JSON parsing into a native object for JSON responses
$headers = array('Accept' => 'application/json');
$query = array('foo' => 'hello', 'bar' => 'world');

$response = Unirest\Request::post('http://mockbin.com/request', $headers, $query);

$response->code;        // HTTP Status code
$response->headers;     // Headers
$response->body;        // Parsed body
$response->raw_body;    // Unparsed body

1. Guzzle

Github

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Guzzle features:

  • Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...
  • Can send both synchronous and asynchronous requests using the same interface.
  • Uses PSR-7 interfaces for requests, responses, and streams. This allows you to utilize other PSR-7 compatible libraries with Guzzle.
  • Abstracts away the underlying HTTP transport, allowing you to write environment and transport agnostic code; i.e., no hard dependency on cURL, PHP streams, sockets, or non-blocking event loops.
  • Middleware system allows you to augment and compose client behavior.
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'

If you know another awesome Http Client Library for PHP, please share it with the community in the comment box.


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors