Current Location: Home> Latest Articles> Practical Guide to Encapsulating API Calls in PHP

Practical Guide to Encapsulating API Calls in PHP

gitbox 2025-08-04

Introduction

In modern web development, interacting with APIs has become a routine task. As a widely-used server-side language, PHP offers robust capabilities for sending HTTP requests, especially through tools like cURL. This article shares a practical example of how to encapsulate the process of calling APIs in PHP, making it reusable and easy to maintain across projects.

Basic Concept of Encapsulation

To make the code more maintainable and reusable, it's a good practice to wrap API call logic into a dedicated class. This approach helps organize request parameters, methods, and response handling more cleanly and makes it easier to scale or debug the code later.

Creating the Wrapper Class

First, create a new PHP file named ApiRequest.php, where we will define a class responsible for making API calls.


class ApiRequest {
    // ...
}

Constructor and Class Properties

We'll define private properties to hold the API URL, HTTP method (like GET or POST), and request parameters. These will be initialized through the constructor.


class ApiRequest {
    private $url;
    private $method;
    private $params;

    public function __construct($url, $method, $params) {
        $this->url = $url;
        $this->method = $method;
        $this->params = $params;
    }
}

This structure allows the developer to pass all the necessary request details when creating an instance of the class.

Sending the API Request

We'll use cURL to send the HTTP request. The sendRequest() method will encapsulate the logic for handling both GET and POST requests.


class ApiRequest {
    // ...

    public function sendRequest() {
        $ch = curl_init();

        if ($this->method === 'POST') {
            curl_setopt($ch, CURLOPT_URL, $this->url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->params));
        } else {
            $query = http_build_query($this->params);
            curl_setopt($ch, CURLOPT_URL, $this->url . '?' . $query);
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);

        return $response;
    }
}

This method checks the request method and constructs the appropriate cURL configuration. It then returns the raw response from the API.

Handling API Responses

To simplify usage, we can also decode the API response—typically in JSON format—and return it as an associative array.


class ApiRequest {
    // ...

    public function sendRequest() {
        // ... request code
        $response = curl_exec($ch);
        curl_close($ch);

        if ($response) {
            $data = json_decode($response, true);
            return $data;
        } else {
            return false;
        }
    }
}

This logic attempts to decode the JSON response. If decoding is successful, the parsed data is returned; otherwise, the method returns false to indicate failure.

Conclusion

Through this example, we demonstrated how to encapsulate an API call using PHP, including class design, cURL request construction, and response handling. This pattern improves code clarity, reduces duplication, and makes it easier to integrate and manage APIs across your web applications.