Learn how to use wp_remote_get() a wordpress builtin api to fetch any REST apis. wp_remote_get() is useful if you want to integrate data from another web service into your WordPress site, or if you want to consume data from an external API for some other purpose. For explanation we will use github REST api and pull git commits and print them.

For this article we will use github REST api and get the list of commits using wp_remote_get() API

First step is to define the access token, endpoint URL and header needed to access the github REST API, for more details

wp_remote_get() only works within wordpress installation, outside of wordpress you can use PHP curl function to fetch a remote REST API

Note on github access token : There are different ways to get access token github rest api access. For this article we will use a personal access token. We will not explain how to obtain the access token, please refer the github documentation.

$auth_token = "github_pat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$args = array(
    'headers' => array(
        'Accept' => 'application/vnd.github+json',
        'Authorization' => 'Bearer ' . $auth_token,
        'X-GitHub-Api-Version' => '2022-11-28'
    ),
);

Based on the github API documentation The REST URL is as below, you need to replace {owner} with the account owner of the repository and {repo} with the name of the repository.

$url = "https://api.github.com/repos/{owner}/{repo}/commits";

We have defined the URL of the API endpoint that we want to fetch, header and access token. We then pass this URL to wp_remote_get() function to fetch the data. The wp_remote_get() function returns the response in the form of an array. We then use wp_remote_retrieve_body() to get the response body, which we can then parse using json_decode() to convert it to a PHP array.

$res = wp_remote_get($url, $args);
$res_body = wp_remote_retrieve_body($res);
$json_data = json_decode($res_body, true);

We can use is_wp_error() to make sure there was no error in the response.

We then can loop through the json data and print the commit messages in the console or list in the wordpress site. Here we will loop through json data, and display the commits sha ID, user name and date.

$html = '<h4>List Commits</h4>';

foreach ($json_data as $item) {
    $name = $item['commit']['author']['name'];
    $date = $item['commit']['author']['date'];
    $msg = '<p>' . $item['sha'] . ' by ' . $name . ' on ' . $date . '</p>';
    $html .= $msg;
    error_log('INFO: GITHUB: commits ' . print_r($msg, true));
}

$html = nl2br($html); // Removes line break after each <br />, if any.

echo $html;

Output result

Above program produces below output, you may tune it as per your need. More info can be found here

List Commits
270f29922eef51940f7968d9655176c49fa2bc09 by Devnodes on 2023-02-04T09:34:36Z
d5f7bb960c2111a463d09b8656cc5514ebf29c53 by Devnodes on 2023-02-04T06:24:23Z
cc2a71d8b2d66f138ab896e77aab17eb02387293 by Devnodes on 2023-02-04T06:23:01Z
974a1b0ec4e2c1ff0eeec5154eb97b92e0145462 by Devnodes on 2023-02-03T15:30:13Z

Finally full source code

$auth_token = "github_pat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$args = array(
    'headers' => array(
        'Accept' => 'application/vnd.github+json',
        'Authorization' => 'Bearer ' . $auth_token,
        'X-GitHub-Api-Version' => '2022-11-28'
    ),
);

$url = "https://api.github.com/repos/OWNER/REPO/commits";

$res = wp_remote_get($url, $args);
if ( ! is_wp_error( $res ) ) {
    $res_body = wp_remote_retrieve_body($res);
    $json_data = json_decode($res_body, true);

    $html = '<h4>List Commits</h4>';

    foreach ($json_data as $item) {
        $name = $item['commit']['author']['name'];
        $date = $item['commit']['author']['date'];
        $msg = '<p>' . $item['sha'] . ' by ' . $name . ' on ' . $date . '</p>';
        $html .= $msg;
        error_log('INFO: GITHUB: commits ' . print_r($msg, true));
    }

    $html = nl2br($html); // Removes line break after each <br />, if any.

    echo $html;
}

There are similar inbuilt apis like wp_remote_request(), wp_remote_get(), wp_remote_post() and more such libraries which are part of http.php

wp_remote_get() only works within wordpress installation, outside of wordpress you can use PHP curl function to fetch a remote REST API