A parallel HTTP client written in pure PHP

v1.7 2017-12-08 11:58 UTC

This package is not auto-updated.

Last update: 2021-02-17 06:26:28 UTC


This is a powerful HTTP client written in pure PHP code, dose not require any other PHP extension. It help you easy to send HTTP request and handle its response.

  • Process multiple requests in parallel
  • Full support for HTTP methods, including GET, POST, HEAD, ...
  • Customizable HTTP headers, full support for Cookie, X-Server-Ip
  • Follows 301/302 redirect, can set the maximum times
  • Supports Keep-Alive, reuse connection to the same host
  • Supports HTTPS with openssl
  • Allow to upload file via POST method
  • Detailed information in DEBUG mode
  • Free and open source, release under MIT license


PHP >= 5.4.0


Install from an Archive File

Extract the archive file downloaded from httpclient-master.zip to your project. And then add the library file into your program:

require '/path/to/httpclient.inc.php';

Install via Composer

If you do not have Composer, you may install it by following the instructions at getcomposer.org.

You can then install this library using the following command:

php composer.phar require "hightman/httpclient:*"


Quick to use

We have defined some shortcut methods, they can be used as following:

use hightman\http\Client;

$http = new Client();

// 1. display response contents
echo $http->get('http://www.baidu.com');
echo $http->get('http://www.baidu.com/s', ['wd' => 'php']);

// 2. capture the response object, read the meta information
$res = $http->get('http://www.baidu.com');

// 3. post request
$res = $http->post('http://www.your.host/', ['field1' => 'value1', 'field2' => 'value2']);
if (!$res->hasError()) {
   echo $res->body;    // response content
   echo $res->status;  // response status code

// 4. head request
$res = $http->head('http://www.baidu.com');

// delete request
$res = $http->delete('http://www.your.host/request/uri');

// 5. restful json requests
// there are sismilar api like: postJson, putJson
$data = $http->getJson('http://www.your.host/request/uri');

$data = $http->postJson('http://www.your.host/reqeust/uri', ['key1' => 'value1', 'key2' => 'value2']);

Customize request

You can also customize various requests by passing in Request object.

use hightman\http\Client;
use hightman\http\Request;

$http = new Client();
$request = new Request('http://www.your.host/request/uri');

// set method
// add headers
$request->setHeader('user-agent', 'test robot');

// specify host ip, this will skip DNS resolver
$request->setHeader('x-server-ip', '');

// add post fields
$request->addPostField('name', 'value');
$request->addPostFile('upload', '/path/to/file');
$request->addPostFile('upload_virtual', 'virtual.text', 'content of file ...');

// or you can specify request body directly
$request->setBody('request body ...');

// you also can specify JSON data as request body
// this will set content-type header to 'application/json' automatically.
$request->setJsonBody(['key' => 'value']);

// execute the request
$response = $http->exec($request);

Multiple get in parallel

A great features of this library is that we can execute multiple requests in parallel. For example, executed three requests simultaneously, the total time spent is one of the longest, rather than their sum.

use hightman\http\Client;
use hightman\http\Request;
use hightman\http\Response;

// Define callback as function, its signature:
// (callback) (Response $res, Request $req, string|integer $key);
function test_cb($res, $req, $key)
   echo '[' . $key . '] url: ' . $req->getUrl() . ', ';
   echo 'time cost: ' . $res->timeCost . ', size: ' . number_format(strlen($res->body)) . "\n";

// or you can define callback as a class implemented interface `ParseInterface`.
class testCb implements \hightman\http\ParseInterface
  public function parse(Response $res, Request $req, $key)
    // your code here ...

// create client object with callback parser
$http = new \hightman\http\Client('test_cb');

// or specify later as following
$http->setParser(new testCb);

// Fetch multiple URLs, it returns after all requests are finished.
// It may be slower for the first time, because of DNS resolover.
$results = $http->mget([
  'baidu' => 'http://www.baidu.com/',
  'sina' => 'http://news.sina.com.cn/',
  'qq' => 'http://www.qq.com/',

// show all results
// print_r($results);

Note: There are other methods like: mhead, mpost, mput ... If you need handle multiple different requests, you can pass an array of Request objects into Client::exec($reqs).

Export and reused cookies

This library can intelligently manage cookies, default store cookies in memory and send them on need. We can export all cookies after Client object destoried.


Add bearer authorization token

$http->setHeader('authorization', 'Bearer ' . $token);
// or add header for request object
$request->setHeader('authorization', 'Bearer ' . $token);

Use socks5 proxy

Connection::useSocks5('', 1080); // or with username & password
Connection::useSocks5('', 1080, 'guest', 'guest'); // or disable it

Enable debug mode

You can turn on debug mode via Client::debug('open'). This will display many debug messages to help you find out problem.


Because of Client class also use HeaderTrait, you can use Client::setHeader() to specify global HTTP headers for requests handled by this client object.

Contact me

If you have any questions, please report on github issues