PHP cURL is a simple, powerful and flexible library that allows us to make HTTP requests from within your PHP application to interact with web servers and web services. With PHP cURL, we can perform various types of HTTP operations such as GET, POST, PUT, DELETE, and more. It gives us full control in customize the requests with headers, cookies, and other options.

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

If you are working within wordpress installation, you can use wp_remote_get() a wordpress builtin function to fetch a remote REST API, compared to php cURL wp_remote_get() is very simple to use.

We set the required headers for the GitHub API, like User-Agent, an Accept header, GitHub API version, and an Authorization header with our GitHub access token as per the documentation.

$auth_token = "github_pat_XXXXXXXXXXXXXXXXXXXXXXXXX";
$headers = array(
    "User-Agent: php-curl",
    "Accept: application/vnd.github+json",
    "Authorization: Bearer " . $auth_token,
    "X-GitHub-Api-Version: 2022-11-28"
);

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.

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";

For this example, we will first initialize a cURL session with curl_init(). We then set the URL of the request by replacing placeholders in the GitHub API URL with our GitHub username and the name of the repo we want to fetch commits from. We then execute the request with curl_exec() and close the cURL session with curl_close().

$curl = curl_init();

$params = array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_HTTPHEADER => $headers
);

curl_setopt_array($curl, $params);

$response = curl_exec($curl);
$err = curl_error($curl);
$response_code = intval(curl_getinfo($curl, CURLINFO_HTTP_CODE));

curl_close($curl);

Finally, we parse the JSON response into a PHP array with json_decode() and loop through each commit to print the commit message.

$json_data = json_decode($response, 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;

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

Full source code

$auth_token = "github_pat_XXXXXXXXXXXXXXXXXXXXXXXXX";
$headers = array(
    "User-Agent: php-curl",
    "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";

$curl = curl_init();

$params = array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_HTTPHEADER => $headers
);

curl_setopt_array($curl, $params);

$response = curl_exec($curl);
$err = curl_error($curl);
$response_code = intval(curl_getinfo($curl, CURLINFO_HTTP_CODE));

curl_close($curl);

if (200 == $response_code & !$err) {
    $json_data = json_decode($response, 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;

} else {
    $json_data = json_decode($response, true);
    error_log('INFO: GITHUB: response_code ' . print_r($response_code, true));
    error_log('INFO: GITHUB: err ' . print_r($err, true));
    error_log('INFO: GITHUB: json_data ' . print_r($json_data, true));
}

Now you know how to make simple PHP cURL for making HTTP requests from PHP applications. It’s a great tool for interacting with web services and APIs, and it provides a lot of control over the requests and responses.

If you are working within wordpress installation, you can use wp_remote_get() a wordpress builtin function to fetch a remote REST API, compared to php cURL wp_remote_get() is very simple to use.