CURLOPT_FOLLOWLOCATION无法激活

Sau*_*ius 17 php curl

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

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

我将如何通过SSH解决此问题?

Fla*_*ino 13

safe_mode = Off在php.ini文件中设置(通常在服务器上的/ etc /中).如果已经关闭了,那么请查看open_basedirphp.ini文件中的内容,并相应地进行更改.

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

  • @Flambine:Depricated意味着它仍然存在并按预期工作,但它非常气馁.它可以并且通常在较旧的共享主机平台上启用.我同意safe_mode充其量是烦人的. (2认同)
  • btw:根据http://www.php.net/releases/NEWS_5_4_0_alpha1.txt在PHP 5.4中删除safe_mode (2认同)

Law*_*one 11

试试这个,如果需要重定向并且启用了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;
}
Run Code Online (Sandbox Code Playgroud)