How to use authorization header PHP

Sam Alexander picture Sam Alexander · Jun 2, 2015 · Viewed 36.1k times · Source

I am trying to use an authorization header in order to use the vimeo API.

It tells me to do this 'Authorization: basic ' + base64(client_id + ':' + client_secret) , which is something I can do.

But nowhere on the internet does it tell me what I actually do with this code? It is not PHP, but does it go in a PHP file? If so then what function do I use on it after storing it? Does it go in an htaccess file?

It is really sad how terrible any and all online documentation is on this.

To summarize, basically what I'm saying is SHOW ME THE CODE

Answer

Mikkel picture Mikkel · Jun 2, 2015
$api_url = 'http://myapiurl';

$client_id = 'myclientid';
$client_secret = 'myclientsecret';

$context = stream_context_create(array(
    'http' => array(
        'header' => "Authorization: Basic " . base64_encode("$client_id:$client_secret"),
    ),
));

$result = file_get_contents($api_url, false, $context);

Documentation links:

For more complex requests, you can use cURL, but the library's PHP implementation is a mess and I prefer to avoid it when I can. Guzzle is a library that abstracts a lot of the complexities here.