curlのリダイレクトに対応するために、curl_setoptで
CURLOPT_FOLLOWLOCATIONを設定しようとしたらこんなエラーがでました。
CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/***/***/test.php on line 123
原因はPHPが動いているレンタルサーバーがセーフモードで実行していたためでした。
curlでLocationをたどるために以下の方法を使いました。
$ch = curl_init();
$limit = 5;
$url = "http://example.com/has/some/redirect";
do {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$url = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
} while ($code === 301 || $code === 302 and $limit--);
``