无法激活CURLOPT_FOLLOWLOCATION

2022-08-31 00:44:46

所以我一直在多个服务器上得到这个烦人的错误(这是一个警告,所以我会忽略它,但我需要这个函数)

警告:curl_setopt() [function.curl-setopt]:当启用了safe_mode或在 /home/xxx/public_html/xxx.php 第 56 行设置open_basedir时,无法激活CURLOPT_FOLLOWLOCATION

我该如何通过SSH解决这个问题?


答案 1

设置在你的php.ini文件中(它通常在服务器上的/etc/中)。如果这已经关闭,那么在php.ini文件中寻找东西,并相应地进行更改。safe_mode = Offopen_basedir

基本上,作为安全措施,follow位置选项已被禁用,但PHP的内置安全功能通常比安全更烦人。实际上,在 PHP 5.3 中已弃用safe_mode


答案 2

试试这个,如果需要重定向并且启用了safemode,它将根据标题跟踪链接(如果您的抓取图像虽然这不起作用,因为它将标头添加到返回中),这是您的特定问题的解决方法,当客户安装我的脚本之一时,我遇到了同样的问题,所以不得不想出这个。它还会将错误记录到:..有用的嗯curl.error.log

<?php 
function geturl($url) {
    (function_exists('curl_init')) ? '' : die('cURL Must be installed for geturl function to work. Ask your host to enable it or uncomment extension=php_curl.dll in php.ini');

    $curl = curl_init();
    $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: ";

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_REFERER, $url);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //CURLOPT_FOLLOWLOCATION Disabled...
    curl_setopt($curl, CURLOPT_TIMEOUT, 60);

    $html = curl_exec($curl);

    $status = curl_getinfo($curl);
    curl_close($curl);

    if ($status['http_code'] != 200) {
        if ($status['http_code'] == 301 || $status['http_code'] == 302) {
            list($header) = explode("\r\n\r\n", $html, 2);
            $matches = array();
            preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
            $url = trim(str_replace($matches[1],"",$matches[0]));
            $url_parsed = parse_url($url);
            return isset($url_parsed) ? geturl($url) : '';
        }

        $oline='';
        foreach ($status as $key => $eline) {
            $oline .= '['.$key.']'.$eline.' ';
        }
        $line = $oline." \r\n ".$url."\r\n-----------------\r\n";

        $handle = @fopen('./curl.error.log', 'a');
        fwrite($handle, $line);
        return false;
    }
    return $html;
}

推荐