php curl with CURLOPT_FOLLOWLOCATION error

后端 未结 4 779
执念已碎
执念已碎 2020-12-06 11:28

i got an error

CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in

i google many

相关标签:
4条回答
  • 2020-12-06 12:16

    You can use the code given in the below link for an alternative.

    http://slopjong.de/2012/03/31/curl-follow-locations-with-safe_mode-enabled-or-open_basedir-set/

    0 讨论(0)
  • 2020-12-06 12:26

    Hi i fix adding this condition:

    $safeMode = @ini_get('safe_mode');
    $openBasedir = @ini_get('open_basedir');
    if (empty($safeMode) && empty($openBasedir)) {
        curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
    }else
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    

    and adding in my .htaccess file

    php_flag safe_mode off
    php_flag open_basedir off
    
    0 讨论(0)
  • 2020-12-06 12:28

    If you specify that only http and https protocols are allowed during redirect using CURLOPT_REDIR_PROTOCOLS, you would be able to use CURLOPT_FOLLOWLOCATION without safe_mode or open_basedir.

    0 讨论(0)
  • 2020-12-06 12:32

    The error means safe_mode or open_basedir ARE enabled (probably open_basedir) you probably can't override either if your host has any decent security settings.

    So you have a choice

    1) change host (not really desirable I imagine)

    2) use a function similar to ones found on the php curl_setopt page, i.e. http://www.php.net/manual/en/function.curl-setopt.php#95027

    The following is a fixed version of the function specified in point 2

    function curl_redirect_exec($ch, &$redirects, $curlopt_header = false) {
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        $data = curl_exec($ch);
    
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($http_code == 301 || $http_code == 302) {
            list($header) = explode("\r\n\r\n", $data, 2);
    
            $matches = array();
            preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
            $url = trim(str_replace($matches[1], "", $matches[0]));
    
            $url_parsed = parse_url($url);
            if (isset($url_parsed)) {
                curl_setopt($ch, CURLOPT_URL, $url);
                $redirects++;
                return curl_redirect_exec($ch, $redirects, $curlopt_header);
            }
        }
    
        if ($curlopt_header) {
            return $data;
        } else {
            list(, $body) = explode("\r\n\r\n", $data, 2);
            return $body;
        }
    }
    
    0 讨论(0)
提交回复
热议问题