It seems like you are using an ad blocker. To enhance your experience and support our website, please consider:

  1. Signing in to disable ads on our site. Our users don't see ads
  2. Or, Sign up if you don't have an account yet
  3. Or, disable your adblocker by doing this:
    • Click on the adblocker icon in your browser's toolbar.
    • Select "Pause on this site" or a similar option for lancecourse.com.

How to use PHP to consume a RESTful API using SausageHTTP

By Clinton Nzedimma

PHP is a general-purpose programming language, which means it can be used to create many types of applications from command-based application, through websites, and APIs, and even desktop applications. This article will be explaining briefly what an HTTP request is and how to use it to consume a RESTful API using PHP.

What is an HTTP request?

When you want to access a website, you type the URL in the browser, and if you have an internet connection, the browser displays the website and its content as soon as possible. This is you making an HTTP request.

You are requesting a browser to give you information using the HyperText Transfer Protocol. Every time you click a link on the webpage, you are making a request to the server. You have the cognitive ability to react and process the data you see because you are a human.

What is a RESTful API?

When you visit a webpage, you react and process the data because it is arranged in a format you can understand but a computer cannot do the same because it has an IQ of zero. This means you are smarter than a computer, right? Awesome.

The inability of a computer to understand and process data brought about the development of a concept known as Representational State Application Programming Interface. This is an API that uses HTTP requests to GET, PUT, POST and DELETE data. This concept allows a programmer to write a simple script or program that allows a computer in programmatic sense access and surf the web in a way it can understand.

Let’s get to it

I developed a simple PHP library called SausageHTTP that allows you to make an HTTP request quickly, below is how it’s used for a RESTful API. You can try to look inside the code of SausageHTTP. What you have inside is what you are supposed to write every time you need to make an HTTP request. This little library actually reduces the load on you.

So, we start by downloading the SausageHTTP from its GitHub repo then create a folder for the project. Now create an ‘index.php’ file inside like this:

index.php

<?php
    // Let's get to it

?>

Include SausageHTTP to your script and you are good to go. Our index.php will look like this now:

index.php

<?php
    // including SausageHTTP
    require 'SausageHTTP.php';

?>

Make a GET request

HTTP requests of GET type are used to make a demand to HTTP servers. Let’s make a simple GET request to a dummy REST API that will return us some data in JSON format.

<?php
    // including SausageHTTP
    require 'SausageHTTP.php';

    $client = new SausageHTTP();
    $client->setRequest([
        'URL' => 'https://jsonplaceholder.typicode.com/comments',
        'METHOD' => 'GET',
        'OPTIONS' => [],
    ]);

    echo $client->response;

?>

This is like typing `https://jsonplaceholder.typicode.com/comments' in your browser address bar and letting the browser to go and get that resource, except that this time around your code plays the role your browser just played -go and get the resource.

Ensure you have an internet connection and execute the code; you should get the response below in JSON (JavaScript Object Notation)

sausage4

The data above is dummy data. If you had worked with AJAX in JavaScript, you may have used it to get JSON data from a server while building your app. The difference here is that HTTP requests in PHP are Synchronous in contrast to the Asynchronous approach in JavaScript to the same concept.

In order to send data to the server using the GET method, you must do the following. Let’s say the server allows you send a parameter postId to get data of a given post of id 5. Then if your GET request code you add the parameter key (in this case postId) in OPTIONS array with its value 5. Let's look at how we can improve the code by adding a parameter:

<?php
    // including SausageHTTP
    require 'SausageHTTP.php';

    $client = new SausageHTTP();
    $client->setRequest([
        'URL' => 'https://jsonplaceholder.typicode.com/comments',
        'METHOD' => 'GET',
        'OPTIONS' => [
            'postId' => 5
        ],
    ]);

    echo $client->response;

?>

And, this is like typing https://jsonplaceholder.typicode.com/comments?postId=5 or https://jsonplaceholder.typicode.com/comments/5. Anyway, this will depend on how the REST API endpoints are set. So as a result, you get the following output in JSON format. It appears there are several data with id of 24.

sausage6

Let’s make sense of the data we have by getting the first name and email from the JSON data. We do this decoding the response from JSON:

<?php
    // including SausageHTTP
    require 'SausageHTTP.php';

    $client = new SausageHTTP();
    $client->setRequest([
        'URL' => 'https://jsonplaceholder.typicode.com/comments',
        'METHOD' => 'GET',
        'OPTIONS' => [
            'id' => 24
        ],
    ]);

    // Decoding response from JSON to array
    $result = json_decode($client->response, 1);

    // Display name    
    echo "<b>Name is </b> <br>" . $result[0]['name'];

?>

Our result should look like this:

sausage8

Congratulations you have sent your first HTTP request with SausageHTTP by using the GET method. The next article will walk you through how to send a POST request.

Thanks for reading.

Last updated 2024-01-11 UTC