PHP Consume External REST APIs


In this PHP example, I am going to show you how to call external REST APIs in PHP programming. I am not going to build here any new REST API, but I will call or consume those REST APIs which are already available for testing purpose. But if you want to know then you can check my tutorial about how to build REST APIs in PHP.

The full form of REST acronym is Representational State Transfer that works over stateless HTTP or HTTPS protocol only.

I will use CURL to call the REST URL resources (REST APIs). CURL is a command line tool for transferring data using various network protocols.

php call external rest services


PHP 7.4.23, CURL

Project Directory

It’s assumed that you have setup PHP in your system.

Now I will create a project root directory called php-consume-external-rest-apis anywhere in your system. I may not mention the project root directory in subsequent sections and I will assume that I am talking with respect to the project’s root directory.

CURL Helper

I will create a common helper function for CURL operations on various HTTP methods – GET, POST, PUT, etc. – for calling external REST APIs.

The following code is written into a file common.php.


function perform_http_request($method, $url, $data = false) {
    $curl = curl_init();

    switch ($method) {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data) {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            if ($data) {
                $url = sprintf("%s?%s", $url, http_build_query($data));

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //If SSL Certificate Not Available, for example, I am calling from http://localhost URL

    $result = curl_exec($curl);


    return $result;

In the above function I have used switch case to segregate operations according to the HTTP methods.

I have set SSL verification to false for https protocol because I am not using any valid certificate at client side and more over, I am calling from my localhost over non-secured http protocol. If you are calling http-based REST API URLs then you don’t need to set this option on your localhost.

Call External REST APIs

Here is the PHP file php-consume-external-rest-apis.php that has the necessary code to call external REST APIs for performing operations and fetching data from the server.


require_once 'common.php';

$rest_api_base_url = '';
//GET - list of users
$get_endpoint = '/api/users';

$response = perform_http_request('GET', $rest_api_base_url . $get_endpoint);

echo 'List Of Users' . "\n";
echo $response . "\n";

//GET - single user
$get_endpoint = '/api/users/2';

$response = perform_http_request('GET', $rest_api_base_url . $get_endpoint);

echo 'Single User' . "\n";
echo $response . "\n";

//POST - create new user
$post_endpoint = '/api/users';

$request_data = json_encode(array("name" => "Soumitra", "job" => "Blog Author", "avatar" => ""));

$response = perform_http_request('POST', $rest_api_base_url . $post_endpoint, $request_data);

echo 'New User' . "\n";
echo $response . "\n";

//PUT - update user
$put_endpoint = '/api/users';

$request_data = json_encode(array("name" => "Soumitra", "job" => "Roy Tutorials Author", "avatar" => ""));

$response = perform_http_request('PUT', $rest_api_base_url . $put_endpoint, $request_data);

echo 'Update User' . "\n";
echo $response;

I have loaded the required common file using require_once.

In the above PHP file, I have shown examples on GET, POST and PUT methods.

The base URL for all REST endpoints is same only individual endpoint for each operation is different.

I have fetched list of users as well as single user using HTTP GET method from the external REST API.

For POST and PUT methods, I have sent the data in JSON format using jsong_encode(). The POST method has been used for creating a new user and PUT method has been used for updating the existing user.

Testing – Call External REST APIs

To run your PHP file using CLI, use the following command. Note php-consume-external-rest-apis is the project root directory.

php-consume-external-rest-apis>php php-consume-external-rest-apis.php

You will see the following output in the console:

php consume external rest apis

Hope you got an idea how to call external REST services in PHP.

Source Code


Leave a Reply

Your email address will not be published. Required fields are marked *