CURL错误(无法激活CURLOPT_FOLLOWLOCATION)

时间:2010-06-28 14:09:31

标签: curl php

  

警告:curl_setopt()   [function.curl-SETOPT]:   CURLOPT_FOLLOWLOCATION不能   在safe_mode或。时激活   open_basedir设置为   第594行/home/path/curl.php

我无法访问php.ini。这可以在不编辑php.ini的情况下修复吗?

3 个答案:

答案 0 :(得分:3)

请参阅手册中的this comment。它提供了一个丑陋的解决方法。我相信这个限制是有效的,因为卷曲库中的一个错误会跟随重定向到本地资源,但现在应该修复,所以我认为没有理由这样做。

答案 1 :(得分:0)

safe_mode属于PHP_INI_SYSTEM - 所以如果这是问题,那么你运气不好,这些项目只能在php.ini和vhost config中设置。

open_basedir属于PHP_INI_ALL,因此您可以使用.htaccessphp_value中进行设置。

答案 2 :(得分:0)

这是为我工作的!

        $ch = curl_init();

        $header=array(
          'User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0',
          'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
          'Accept-Language: en-us,en;q=0.5',
          'Accept-Encoding: gzip,deflate',
          'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
          'Keep-Alive: 115',
          'Connection: keep-alive',
        );
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.2; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'curl_cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'curl_cookies.txt');
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    $data = curl_exec($ch);


    curl_close($ch);

     $status = curl_getinfo($curl);

 if ($status['http_code'] == 200) {
    return $data;    
} else {
    echo $url;
    return @file_get_contents($url);
}