Since version 5.1.0, there's the max_redirects option, which makes the fopen HTTP wrapper follow the Location
redirect:
The max number of redirects to follow. Value 1 or less means that no redirects are followed.
Defaults to 20.
You might want to set it explicitly, in case your config disables this. An example modified from the docs:
<?php
$url = 'http://www.example.com/';
$opts = array(
'http' => array('method' => 'GET',
'max_redirects' => '20')
);
$context = stream_context_create($opts);
$stream = fopen($url, 'r', false, $context);
// header information as well as meta data
// about the stream
var_dump(stream_get_meta_data($stream));
// actual data at $url
var_dump(stream_get_contents($stream));
fclose($stream);
?>