PHPackages                             itabrezshaikh/unirest-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. itabrezshaikh/unirest-php

ActiveLibrary

itabrezshaikh/unirest-php
=========================

Unirest PHP

2.0(10y ago)11.4kMITPHP

Since Mar 24Pushed 10y ago1 watchersCompare

[ Source](https://github.com/itabrezshaikh/unirest-php)[ Packagist](https://packagist.org/packages/itabrezshaikh/unirest-php)[ RSS](/packages/itabrezshaikh-unirest-php/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

Unirest for PHP
===============

[](#unirest-for-php)

Unirest is a set of lightweight HTTP libraries available in [multiple languages](http://unirest.io).

Features
--------

[](#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

Requirements
------------

[](#requirements)

- [cURL](http://php.net/manual/en/book.curl.php)

Installation
------------

[](#installation)

### Using [Composer](https://getcomposer.org)

[](#using-composer)

To install unirest-php with Composer, just add the following to your `composer.json` file:

```
{
    "require-dev": {
        "apimatic/unirest-php": "2.*"
    }
}
```

or by running the following command:

```
composer require apimatic/unirest-php
```

This will get you the latest version of the reporter and install it. If you do want the master, untagged, version you may use the command below:

```
composer require apimatic/php-test-reporter:@dev-master
```

Composer installs autoloader at `./vendor/autoloader.php`. to include the library in your script, add:

```
require_once 'vendor/autoload.php';
```

If you use Symfony2, autoloader has to be detected automatically.

### Install from source

[](#install-from-source)

Unirest-PHP requires PHP `v5.4+`. Download the PHP library from Github, then include `Unirest.php` in your script:

```
git clone git@github.com:apimatic/unirest-php.git
```

```
require_once '/path/to/unirest-php/src/Unirest.php';
```

Usage
-----

[](#usage)

### Creating a Request

[](#creating-a-request)

So you're probably wondering how using Unirest makes creating requests in PHP easier, let's look at a working example:

```
$headers = array("Accept" => "application/json");
$body = array("foo" => "hellow", "bar" => "world");

$response = Unirest\Unirest::post("http://mockbin.com/request", $headers, $body);

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

### File Uploads

[](#file-uploads)

To upload files in a multipart form representation use the return value of `Unirest\File::add($path)` as the value of a parameter:

```
$headers = array("Accept" => "application/json");
$body = array("file" => Unirest\File::add("/tmp/file.txt"));

$response = Unirest\Unirest::post("http://mockbin.com/request", $headers, $body);
```

### Custom Entity Body

[](#custom-entity-body)

Sending a custom body such as a JSON Object rather than a string or form style parameters we utilize json\_encode for the body:

```
$headers = array("Accept" => "application/json");
$body =   json_encode(array("foo" => "hellow", "bar" => "world"));

$response = Unirest\Unirest::post("http://mockbin.com/request", $headers, $body);
```

### Authentication

[](#authentication)

First, if you are using [Mashape](https://www.mashape.com/):

```
// Mashape auth
Unirest\Unirest::setMashapeKey('');
```

Otherwise, passing a username, password *(optional)*, defaults to Basic Authentication:

```
// basic auth
Unirest\Unirest::auth('username', 'password');
```

The third parameter, which is a bitmask, will Unirest which HTTP authentication method(s) you want it to use for your proxy authentication.

If more than one bit is set, Unirest *(at PHP's libcurl level)* will first query the site to see what authentication methods it supports and then pick the best one you allow it to use. *For some methods, this will induce an extra network round-trip.*

**Supported Methods**

MethodDescription`CURLAUTH_BASIC`HTTP Basic authentication. This is the default choice`CURLAUTH_DIGEST`HTTP Digest authentication. as defined in [RFC 2617](http://www.ietf.org/rfc/rfc2617.txt)`CURLAUTH_DIGEST_IE`HTTP Digest authentication with an IE flavor. *The IE flavor is simply that libcurl will use a special "quirk" that IE is known to have used before version 7 and that some servers require the client to use.*`CURLAUTH_NEGOTIATE`HTTP Negotiate (SPNEGO) authentication. as defined in [RFC 4559](http://www.ietf.org/rfc/rfc4559.txt)`CURLAUTH_NTLM`HTTP NTLM authentication. A proprietary protocol invented and used by Microsoft.`CURLAUTH_NTLM_WB`NTLM delegating to winbind helper. Authentication is performed by a separate binary application. *see [libcurl docs](http://curl.haxx.se/libcurl/c/CURLOPT_HTTPAUTH.html) for more info*`CURLAUTH_ANY`This is a convenience macro that sets all bits and thus makes libcurl pick any it finds suitable. libcurl will automatically select the one it finds most secure.`CURLAUTH_ANYSAFE`This is a convenience macro that sets all bits except Basic and thus makes libcurl pick any it finds suitable. libcurl will automatically select the one it finds most secure.`CURLAUTH_ONLY`This is a meta symbol. OR this value together with a single specific auth value to force libcurl to probe for un-restricted auth and if not, only that single auth algorithm is acceptable.```
// custom auth method
Unirest\Unirest::proxyAuth('username', 'password', CURLAUTH_DIGEST);
```

Previous versions of **Unirest** support *Basic Authentication* by providing the `username` and `password` arguments:

```
$response = Unirest\Unirest::get("http://mockbin.com/request", null, null, "username", "password");
```

**This has been deprecated, and will be completely removed in `v.3.0.0` please use the `Unirest\Unirest::auth()` method instead**

### Request Object

[](#request-object)

```
Unirest\Unirest::get($url, $headers = array(), $parameters = null)
Unirest\Unirest::post($url, $headers = array(), $body = null)
Unirest\Unirest::put($url, $headers = array(), $body = null)
Unirest\Unirest::patch($url, $headers = array(), $body = null)
Unirest\Unirest::delete($url, $headers = array(), $body = null)
```

- `url` - Endpoint, address, or uri to be acted upon and requested information from.
- `headers` - Request Headers as associative array or object
- `body` - Request Body as associative array or object

You can send a request with any [standard](http://www.iana.org/assignments/http-methods/http-methods.xhtml) or custom HTTP Method:

```
$request = Unirest\Unirest::prepare(Unirest\Method::LINK, $url, $headers = array(), $body);

$request = Unirest\Unirest::prepare('CHECKOUT', $url, $headers = array(), $body);

//and then
$response = $request->getResponse();
```

### Response Object

[](#response-object)

Upon recieving a response Unirest returns the result in the form of an Object, this object should always have the same keys for each language regarding to the response details.

- `code` - HTTP Response Status Code (Example `200`)
- `headers` - HTTP Response Headers
- `body` - Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays.
- `raw_body` - Un-parsed response body

### Advanced Configuration

[](#advanced-configuration)

You can set some advanced configuration to tune Unirest-PHP:

#### Custom JSON Decode Flags

[](#custom-json-decode-flags)

Unirest uses PHP's [JSON Extension](http://php.net/manual/en/book.json.php) for automatically decoding JSON responses. sometime you may want to return associative arrays, limit the depth of recursion, or use any of the [customization flags](http://php.net/manual/en/json.constants.php).

To do so, simply set the desired options using the `jsonOpts` request method:

```
Unirest\Unirest::jsonOpts(true, 512, JSON_NUMERIC_CHECK & JSON_FORCE_OBJECT & JSON_UNESCAPED_SLASHES);
```

#### Timeout

[](#timeout)

You can set a custom timeout value (in **seconds**):

```
Unirest\Unirest::timeout(5); // 5s timeout
```

#### Proxy

[](#proxy)

Set the proxy to use for the upcoming request.

you can also set the proxy type to be one of `CURLPROXY_HTTP`, `CURLPROXY_HTTP_1_0`, `CURLPROXY_SOCKS4`, `CURLPROXY_SOCKS5`, `CURLPROXY_SOCKS4A`, and `CURLPROXY_SOCKS5_HOSTNAME`.

*check the [cURL docs](http://curl.haxx.se/libcurl/c/CURLOPT_PROXYTYPE.html) for more info*.

```
// quick setup with default port: 1080
Unirest\Unirest::proxy('10.10.10.1');

// custom port and proxy type
Unirest\Unirest::proxy('10.10.10.1', 8080, CURLPROXY_HTTP);

// enable tunneling
Unirest\Unirest::proxy('10.10.10.1', 8080, CURLPROXY_HTTP, true);
```

##### Proxy Authenticaton

[](#proxy-authenticaton)

Passing a username, password *(optional)*, defaults to Basic Authentication:

```
// basic auth
Unirest\Unirest::proxyAuth('username', 'password');
```

The third parameter, which is a bitmask, will Unirest which HTTP authentication method(s) you want it to use for your proxy authentication.

If more than one bit is set, Unirest *(at PHP's libcurl level)* will first query the site to see what authentication methods it supports and then pick the best one you allow it to use. *For some methods, this will induce an extra network round-trip.*

See [Authentication](#authentication) for more details on methods supported.

```
// basic auth
Unirest\Unirest::proxyAuth('username', 'password', CURLAUTH_DIGEST);
```

#### Default Request Headers

[](#default-request-headers)

You can set default headers that will be sent on every request:

```
Unirest\Unirest::defaultHeader("Header1", "Value1");
Unirest\Unirest::defaultHeader("Header2", "Value2");
```

You can do set default headers in bulk:

```
Unirest\Unirest::defaultHeaders(array(
    "Header1" => "Value1",
    "Header2" => "Value2"
));
```

You can clear the default headers anytime with:

```
Unirest\Unirest::clearDefaultHeaders();
```

#### SSL validation

[](#ssl-validation)

You can explicitly enable or disable SSL certificate validation when consuming an SSL protected endpoint:

```
Unirest\Unirest::verifyPeer(false); // Disables SSL cert validation
```

By default is `true`.

#### Utility Methods

[](#utility-methods)

```
// alias for `curl_getinfo`
Unirest\Unirest::getInfo()

// returns internal cURL handle
Unirest\Unirest::getCurlHandle()
```

---

Made with ♥ from the [Mashape](https://www.mashape.com/) team

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3687d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/043600b1f55d62efca40eca4e24542b341c381bee19d335016b5297c6d4dae76?d=identicon)[pepipost](/maintainers/pepipost)

---

Top Contributors

[![subnetmarco](https://avatars.githubusercontent.com/u/964813?v=4)](https://github.com/subnetmarco "subnetmarco (83 commits)")[![ahmadnassri](https://avatars.githubusercontent.com/u/183195?v=4)](https://github.com/ahmadnassri "ahmadnassri (73 commits)")[![zeeshanejaz](https://avatars.githubusercontent.com/u/2345363?v=4)](https://github.com/zeeshanejaz "zeeshanejaz (18 commits)")[![esseguin](https://avatars.githubusercontent.com/u/1429412?v=4)](https://github.com/esseguin "esseguin (12 commits)")[![i-tabu](https://avatars.githubusercontent.com/u/9974389?v=4)](https://github.com/i-tabu "i-tabu (6 commits)")[![michelezonca](https://avatars.githubusercontent.com/u/392758?v=4)](https://github.com/michelezonca "michelezonca (6 commits)")[![motdotla](https://avatars.githubusercontent.com/u/3848?v=4)](https://github.com/motdotla "motdotla (5 commits)")[![nijikokun](https://avatars.githubusercontent.com/u/198276?v=4)](https://github.com/nijikokun "nijikokun (5 commits)")[![andreyvital](https://avatars.githubusercontent.com/u/967317?v=4)](https://github.com/andreyvital "andreyvital (3 commits)")[![thibaultcha](https://avatars.githubusercontent.com/u/1125431?v=4)](https://github.com/thibaultcha "thibaultcha (3 commits)")[![nikkobautista](https://avatars.githubusercontent.com/u/278104?v=4)](https://github.com/nikkobautista "nikkobautista (2 commits)")[![jasir](https://avatars.githubusercontent.com/u/115066?v=4)](https://github.com/jasir "jasir (2 commits)")[![qpleple](https://avatars.githubusercontent.com/u/774546?v=4)](https://github.com/qpleple "qpleple (2 commits)")[![samsullivan](https://avatars.githubusercontent.com/u/3789442?v=4)](https://github.com/samsullivan "samsullivan (1 commits)")[![sonicaghi](https://avatars.githubusercontent.com/u/1213643?v=4)](https://github.com/sonicaghi "sonicaghi (1 commits)")

### Embed Badge

![Health badge](/badges/itabrezshaikh-unirest-php/health.svg)

```
[![Health](https://phpackages.com/badges/itabrezshaikh-unirest-php/health.svg)](https://phpackages.com/packages/itabrezshaikh-unirest-php)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
