php CURLOPT_FOLLOWLOCATION 无法激活
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6918623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CURLOPT_FOLLOWLOCATION cannot be activated
提问by Saulius Antanavicius
So I keep getting this annoying error on multiple servers(its a warning, so I'd ignore it, but I need the function)
所以我一直在多台服务器上收到这个烦人的错误(这是一个警告,所以我会忽略它,但我需要这个功能)
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/xxx/public_html/xxx.php on line 56
警告:curl_setopt() [function.curl-setopt]:启用 safe_mode 或在第 56 行的 /home/xxx/public_html/xxx.php 中设置 open_basedir 时,无法激活 CURLOPT_FOLLOWLOCATION
How would I go about fixing this via SSH?
我将如何通过 SSH 解决这个问题?
采纳答案by Flambino
Set safe_mode = Off
in your php.ini file (it's usually in /etc/ on the server). If that's already off, then look around for the open_basedir
stuff in the php.ini file, and change it accordingly.
safe_mode = Off
在你的 php.ini 文件中设置(它通常在服务器上的 /etc/ 中)。如果它已经关闭,那么查看open_basedir
php.ini 文件中的内容,并相应地更改它。
Basically, the follow location option has been disabled as a security measure, but PHP's built-in security features are usually more annoying than secure. In fact, safe_mode
is deprecated in PHP 5.3.
基本上,以下位置选项已被禁用作为安全措施,但 PHP 的内置安全功能通常比安全更烦人。事实上,safe_mode
在 PHP 5.3 中已弃用。
回答by Lawrence Cherone
Try this, If a redirect is required and safemode is enabled it will follow the link based on the header (If your grabbing images though this will not work as it adds the header to the return), this is a workaround to your specific problem, I had the same problem when a customer installed one of my script so had to come up with this.. It will also log errors to: curl.error.log
.. useful eh
试试这个,如果需要重定向并且启用了安全模式,它将跟随基于标题的链接(如果您的抓取图像在将标题添加到返回时不起作用),这是您特定问题的解决方法,当客户安装了我的一个脚本时,我遇到了同样的问题,因此不得不提出这个问题..它还会将错误记录到: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;
}
回答by Marcos Rezende
To solve this problem, simply put safe_mode = Off
and clear open_base_dir
at php.ini file.
要解决此问题,只需在 php.ini 文件中放置safe_mode = Off
并清除即可open_base_dir
。
回答by Timur
About safe mode and open_basedir option:
关于安全模式和 open_basedir 选项:
http://www.php.net/manual/en/features.safe-mode.php
http://www.php.net/manual/en/features.safe-mode.php
回答by Sajid
Simply, as long as either open_basediror safe_modeis enabled in the php.ini file, the CURLOPT_FOLLOWLOCATION configuration cannot be used. To change those settings, I can only give general notes:
简单来说,只要在php.ini 文件中开启open_basedir或safe_mode,就不能使用CURLOPT_FOLLOWLOCATION 配置。要更改这些设置,我只能给出一般性说明:
- SSH to the server
- cd to the directory (usually /etc/php5 on linux, depends on your distribution or OS) that contains php.ini
- sudo to edit (eg,
sudo nano php.ini
). - edit the lines that specify open_basedir or safe_mode and either turn them off.
- SSH 到服务器
- cd 到包含 php.ini 的目录(在 linux 上通常为 /etc/php5,取决于您的发行版或操作系统)
- sudo 进行编辑(例如,
sudo nano php.ini
)。 - 编辑指定 open_basedir 或 safe_mode 的行并将它们关闭。
Remember to restart your httpd afterwards!
记得之后重启你的httpd!