Making Authenticated API Calls Using PHP cURL and Bearer Tokens

Making authenticated API calls using PHP cURL and Bearer tokens is now a daily task for many developers. When you work with PHP cURL with Bearer authorization, you send a secure access token in the HTTP header so the API knows who you are and what you can access.

You usually set a php curl authorization header bearer value when you connect to payment gateways, CRM systems, shipping APIs, or your own microservices. Instead of sending a username and password every time, you send a short‑lived Bearer token that keeps your requests secure and your code clean.

In this guide, you will see a practical php curl request with bearer token example, learn how to send Bearer token in PHP cURL for GET and POST requests, and build a small reusable helper that fits real‑life projects.

Basic php curl request with bearer token example

Imagine you need to fetch a list of orders from https://api.example.com/orders. The API team gives you an access token and says: Send it as a Bearer token in the Authorization header.

Here is a simple php curl request with bearer token example that does exactly that:

<?php

$apiUrl = 'https://api.example.com/orders';
$accessToken = 'YOUR_ACCESS_TOKEN_HERE';

$ch = curl_init($apiUrl);

curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $accessToken,
    'Accept: application/json'
]);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$responseBody = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

echo $httpCode . PHP_EOL;
echo $responseBody . PHP_EOL;

In this example, we will open connection with curl_init and use curl_setopt to setup authorization headers and read the response body and HTTP status code.

Reusable helper to make API call with Bearer token PHP

In real projects you rarely write raw cURL code every time. It is easier to wrap it in a small function and call it whenever you need.

Here is a helper that lets you make api call with bearer token php for GET and POST requests:

<?php

function callApiWithBearer(string $method, string $url, string $token, array $data = null): array
{
    $ch = curl_init($url);

    $headers = [
        'Authorization: Bearer ' . $token,
        'Accept: application/json'
    ];

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $method = strtoupper($method);

    if ($method === 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
        if ($data !== null) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }
    }

    $responseBody = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curlError = curl_error($ch);

    curl_close($ch);

    return [
        'status' => $httpCode,
        'body' => $responseBody,
        'error' => $curlError
    ];
}

This is where php curl setopt authorization bearer comes into play. You use curl_setopt to attach the Authorization header and configure the HTTP method. The function returns an array with status, body, and any cURL error, so you can handle issues in one place.

You can now call this helper in a real‑life scenario. For example to create a new customer in your billing system:

<?php

$token = 'YOUR_ACCESS_TOKEN_HERE';

$result = callApiWithBearer(
    'POST',
    'https://api.example.com/customers',
    $token,
    [
        'email' => 'john.doe@example.com',
        'name' => 'John Doe'
    ]
);

if ($result['error']) {
    echo 'Curl error: ' . $result['error'] . PHP_EOL;
} else {
    echo 'Status: ' . $result['status'] . PHP_EOL;
    echo 'Response: ' . $result['body'] . PHP_EOL;
}

Conclusion

Once you understand the pattern, PHP cURL with Bearer authorization feels straightforward. You build the Authorization header, attach it with curl_setopt, and send your request with the right method and body format.

You now know how to send bearer token in PHP cURL, how to build a reusable helper to make api call with bearer token php, and how to adapt the approach for JSON APIs. With these examples you can securely connect your PHP applications to almost any modern API that expects Bearer tokens.