All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mock

API Automated Testing

Sign up for free
Home / Effective Strategies / First Steps to Using UniRest PHP

First Steps to Using UniRest PHP

UniRest is a lightweight HTTP request client library that can work with PHP, making it extra useful for developers who usually work with PHP and cURL. Learn more on how you can install UniRest for PHP here!

UniRest PHP emerges as a powerful and versatile solution in streamlining communication between APIs (Application Programming Interfaces) and backend applications. The availability of UniRest's open-source PHP library empowers developers to seamlessly interact with various APIs, facilitating data exchange and enriching the functionality of their web applications.

Are you struggling to write code in PHP for UniRest? Look no further - start considering Apidog right here and right now!

With Apidog, developers can build, test, mock, and document APIs. That means you no longer need to find other apps to download!

To learn more about what Apidog can provide, click the button below!
Apidog An integrated platform for API design, debugging, development, mock, and testing
REAL API Design-first Development Platform. Design. Debug. Test. Document. Mock. Build APIs Faster & Together.

What is UniRest?

UniRest is a lightweight HTTP request client library available in multiple programming languages, including PHP. It can simplify the process of interacting with APIs (Application Programming Interfaces) within your code. Imagine UniRest as a translator that allows your application to communicate with various web services and exchange data easily.

What does the PHP in "UniRest PHP" Mean?

unirest php kong github

In the context of this specific phrase, the PHP in "UniRest PHP" refers to the PHP programming language that the UniRest library is specifically designed for.

UniRest PHP's Key Features

Developers seek to use UniRest PHP due to its ability to enable backend applications to communicate with APIs or web services over HTTP. These are a few notable key features that attract developers using the PHP programming language:

Simplicity and Readability

UniRest PHP is known for its clean and concise syntax. This allows developers to write clear and easy-to-understand code for making HTTP requests. Developers can focus on the core functionality of their application without getting bogged down in complex API interaction details.

Support for Multiple HTTP Methods

UniRest PHP offers built-in support for various HTTP methods like GET, POST, PUT, DELETE, and more. This flexibility allows developers to interact with APIs in a comprehensive way, accommodating different functionalities like retrieving data (GET), sending data (POST), updating data (PUT), or deleting data (DELETE).

Lightweight and Efficient

UniRest PHP is a lightweight library, meaning it does not add significant overhead to your application. This translates to faster development cycles and efficient use of system resources.

Automatic JSON Parsing

For JSON-based APIs, UniRest PHP automatically parses the response into a native PHP object, eliminating the need for manual parsing, and thus saving developers time and effort.

Customization Options

While offering a simple interface, UniRest PHP provides customization options. Developers can set custom headers, timeouts, and default headers for all requests, promoting the code maintainability (DRY - Don't Repeat Yourself) principle.

Broad Authentication Support

UniRest PHP supports various authentication methods like Basic, Digest, and more. This allows developers to securely access APIs requiring authentication.

These features, combined with its open-source nature, make UniRest PHP a valuable tool for developers seeking to streamline API interactions and build robust web applications in PHP.

How to Install UniRest PHP?

This section of the article will highlight steps on how to install the UniRest library in your working environment.

UniRest PHP Installation Via Repository Cloning

A simpler and more straightforward method to install UniRest PHP would be to clone the GitHub repository.

In your IDE, open your project's terminal and type in the command below:

$ git clone git@github.com:Mashape/unirest-php.git 

Afterward, include the following code in your script:

require_once '/path/to/unirest-php/lib/Unirest.php';

However, if you wish to download through the composer, you will need to add this line of code:

require_once 'vendor/autoload.php';

UniRest PHP HTTP Methods

UniRest for PHP has the following HTTP requests supported for developers:


Unirest::get($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL


Unirest::post($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL


Unirest::put($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL


Unirest::patch($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL


Unirest::delete($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL

Sample Code Snippet  for UniRest PHP Get Request

You can make a GET request using the UniRest PHP if you know how to write the code. An example of a simple GET request would be as follows:


require 'vendor/autoload.php'; // Assuming you have Composer set up

// Replace 'https://api.example.com/data' with the actual API endpoint URL
$url = 'https://api.example.com/data';

$response = Unirest::get($url);

if ($response->code == 200) {
  $data = $response->body;  // Parsed response data (may be JSON or other format)
  echo "API response: " . json_encode($data, JSON_PRETTY_PRINT); // Assuming JSON response
} else {
  echo "Error: " . $response->code . " - " . $response->statusText;

(Please note that changes have to be made to fulfill your application's function.)


  1. We first require the autoloader (vendor/autoload.php) assuming you're using Composer for dependency management.
  2. Define the API endpoint URL in the $url variable. Replace it with the actual URL you want to retrieve data from.
  3. Use Unirest::get($url) to send a GET request to the specified URL.
  4. Check the response code using $response->code. A code of 200 indicates a successful request.
  5. If successful, access the parsed response data using $response->body. The format of the data depends on the API (often JSON). Here, we assume JSON and use json_encode for better readability.
  6. If there's an error, the error code and message are accessible through $response->code and $response->statusText respectively.

Create PHP Code For Your App in Seconds With Apidog!

If you are interested in using the UniRest PHP library but have no knowledge of PHP, fear not! Introducing to you Apidog, a comprehensive development platform that provides a myriad of functionalities to improve developers' workflows.

apidog mock interface

Generating PHP Client Code With Apidog

A powerful feature that Apidog possesses (and many programmers can use to save time) is the automatic code generator. This can streamline many developers' workflow, dismissing the time needed to think and troubleshoot self-written code.

generate client code apidog

You will need to download Apidog and create a new blank project. Proceed by creating a new request, and locating the </> button found around the top right corner of the Apidog window, as seen in the image above.

apidog generate php code

A pop-up window will then appear in front of your screen. On the left side of the window, you can see the various programming languages that Apidog can generate for you. As this article discusses UniRest for PHP, let's choose PHP, and you can view the different codes for different frameworks revolving around PHP.

All you need to do now is copy and paste the code into your code project (inside your IDE [Integrated Development Environment]), and you should be good to go.


Designing Your Own API for Projects with Apidog

If you have a specific function that you want to incorporate into your project but have been having difficulty finding one, why not try to create your own API?

new api apidog

Start by press the New API button, as shown in the image above.

add details new api apidog

After clicking the New API button, you should see your Apidog's screen to be identical to the image above. On this page, you can start adding the finer details to your API, such

  1. Defining the HTTP Method: Choose how apps can send requests (GET, POST, etc.) to trigger different features in your API.
  2. Creating API Endpoints: Create specific URLs that apps will use to connect and interact with your API.
  3. Adding details to URLs: Specify any essential information apps need to include within the URL to target specific data. Imagine this like adding keywords to a search query to get exactly what you're looking for.
  4. Writing Clear Documentation: Describe what each URL and its details do within your API. This is like creating user instructions for the apps that will use your API.


UniRest PHP stands out as a compelling choice for developers seeking to streamline API interactions within their PHP applications. Its intuitive syntax, comprehensive feature set, and lightweight nature contribute to faster development cycles, cleaner code, and efficient resource utilization. Whether you are building a simple web application or a complex API-driven system, UniRest PHP empowers you to seamlessly integrate external functionalities and enrich the user experience.

With its active community and open-source nature, UniRest PHP continues to evolve, offering developers a reliable and versatile tool for building robust and efficient web applications in the ever-growing world of APIs.

Join Apidog's Newsletter

Subscribe to stay updated and receive the latest viewpoints anytime.