LoginSignup
0
0

More than 3 years have passed since last update.

PHPのcurlでLocationを辿ろうとしたらエラー

Posted at

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--);
``
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0