PHP file_get_contents with HTTP Basic Auth

Today I needed to figure out how to read some data from a URL that required HTTP Basic Auth. The solution was pretty simple, use file_get_contents and pass in a configured stream context. I found the following code on the stream_context_create documentation:

$cred = sprintf( 'Authorization: Basic %s',
  base64_encode( 'username:password' )
);
$opts = array(
  'http' => array(
    'method' => 'GET',
    'header' => $cred
  )
);
$ctx = stream_context_create( $opts );
$data = file_get_contents( $url, false, $ctx );

Easy-peasy!