How to use authorization header PHP

How to use authorization header PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Vimeo API Authorization in PHP

The confusion surrounding the Vimeo API authorization header stems from a common misunderstanding of HTTP headers and how they map to server-side code. The documentation provides the raw value needed for the Authorization header, but it does not explicitly state where this value belongs in your application logic. In a PHP context, you do not store this base64 string in an .htaccess file, nor is there a specific "function" that magically handles authentication for you once the header exists. Instead, you must construct the HTTP request yourself and include this header within the request object before sending it to Vimeo.

The core task is to generate the Base64-encoded credentials using PHP's base64_encode() function and then inject that string into the outgoing HTTP request. The standard library function curl_init() combined with curl_setopt() is the most straightforward way to achieve this if you are not using a framework like Laravel.

Here is how you construct the authorization string and attach it to a cURL request:

$clientId = 'your_client_id';
$clientSecret = 'your_client_secret';

// Combine ID and secret, then encode to Base64
$authString = base64_encode($clientId . ':' . $clientSecret);

// Initialize the cURL handle
$ch = curl_init('https://api.vimeo.com/videos');

// Set the Authorization header
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Basic ' . $authString',
    'Accept: application/json' // Often required by APIs
]);

// Other standard options like setting POST data or retrieving response
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'title="New Video"');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

if ($response) {
    echo $response;
} else {
    echo 'Error: ' . curl_error($ch);
}

curl_close($ch);

In this implementation, the critical step is adding the Authorization: Basic ... string to the CURLOPT_HTTPHEADER array. This tells the HTTP client (cURL) that every request sent through this handle must include that specific header. The Vimeo API expects this header on every authenticated endpoint call; omitting it will result in a 401 Unauthorized error.

If you are working within a Laravel application, the process shifts from raw cURL to using the HTTP client facade or Guzzle, which handles headers more gracefully. You would pass the authorization array directly into the request method:

use Illuminate\Support\Facades\Http;

$clientId = 'your_client_id';
$clientSecret = 'your_client_secret';

$response = Http::withHeaders([
    'Authorization' => 'Basic ' . base64_encode($clientId . ':' . $clientSecret)
])->post('https://api.vimeo.com/videos', [
    'title' => 'New Video'
]);

echo $response->body();

The trade-off here is between control and convenience. Raw cURL gives you granular control over every byte sent, which is useful for debugging or when integrating with non-Laravel PHP scripts. However, frameworks like Laravel abstract these details, making the code cleaner but potentially obscuring exactly where headers are being set if you are unfamiliar with the underlying HTTP client implementation.

A common pitfall developers encounter is hardcoding credentials directly into source files. In a production environment, this creates a security risk because your client_secret would be exposed in version control or server logs. Best practices dictate storing these values in environment variables and injecting them into your application at runtime:

$clientId = env('VIMEO_CLIENT_ID');
$clientSecret = env('VIMEO_CLIENT_SECRET');

This approach ensures that sensitive data remains outside the codebase, adhering to standard security protocols. If you require deeper integration with Vimeo's ecosystem, specifically for managing user permissions or OAuth flows rather than simple API key authentication, you might look into dedicated packages or consult resources like https://laravelcompany.com for guidance on securing API integrations within Laravel applications. However, for the basic Base64 authorization method described in the source material, constructing the header manually as shown is the correct and standard procedure.

Our network