Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP ini文件\u获取\u内容外部url_Php_Url_External_File Get Contents_Ini - Fatal编程技术网

PHP ini文件\u获取\u内容外部url

PHP ini文件\u获取\u内容外部url,php,url,external,file-get-contents,ini,Php,Url,External,File Get Contents,Ini,我使用以下PHP函数: 文件获取内容('http://example.com'); 每当我在某个服务器上执行此操作时,结果都是空的。当我在其他任何地方这样做时,结果是页面的内容可能是什么。但是,当我在结果为空的服务器上,在本地使用该函数时—不访问外部URL(文件_get_contents('../simple/internal/path.html');),它确实可以工作 现在,我非常确定它与某个php.ini配置有关。但我不确定的是,哪一个。请帮助。您要查找的设置是 在不更改php.ini的情况

我使用以下PHP函数:

文件获取内容('http://example.com');

每当我在某个服务器上执行此操作时,结果都是空的。当我在其他任何地方这样做时,结果是页面的内容可能是什么。但是,当我在结果为空的服务器上,在本地使用该函数时—不访问外部URL(
文件_get_contents('../simple/internal/path.html');
),它确实可以工作


现在,我非常确定它与某个php.ini配置有关。但我不确定的是,哪一个。请帮助。

您要查找的设置是

在不更改php.ini的情况下,有两种方法可以绕过它,一种是使用,另一种是使用


无论如何,我建议使用cURL over
file\u get\u contents()
,因为它是为此而构建的。

与ini配置设置相关

您应该意识到,启用该选项可能会使代码中的某些bug被利用

例如,无法验证输入可能会变成一个全面的远程代码执行漏洞:

copy($_GET["file"], "."); 

作为对Aillyn答案的补充,您可以使用如下函数来模拟文件内容的行为:

function get_content($URL){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL, $URL);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;
}

echo get_content('http://example.com');
加:


php.ini
文件中。如果您使用的是共享主机,请先创建一个。

这也将为外部链接提供一个绝对路径,而无需使用php.ini

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.your_external_website.com");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://www.your_external_website.com/$2$3', $result);
echo $result
?>

最适合http url,
但是如何打开https url帮助我上面提供的答案解决了问题,但没有解释OP描述的奇怪行为。这个解释应该可以帮助任何人在开发环境中测试站点之间的通信,这些站点都位于同一个主机上(并且是同一个virtualhost;我正在使用Apache2.4和php7.0)

我遇到的
file\u get\u contents()
有一个微妙之处,在这里是绝对相关的,但没有得到解决(可能是因为它要么几乎没有文档记录,要么从我能说的内容中没有文档记录,要么是在一个我找不到的模糊的php安全模型白皮书中记录的)

在所有相关上下文中(例如,
/etc/php/7.0/apache2/php.ini
/etc/php/7.0/cli/php.ini
/etc/php/7.0/fpm/php.ini
等)和
在命令行上下文中允许url/fopen
设置为
On
,调用以获取
文件内容
本地资源将被允许,并且不会记录任何警告,例如:

file_get_contents('php://input');

总之,限制
allow\u url\u fopen=Off
类似于
OUTPUT
链中的
iptables
规则,只有在尝试“退出系统”或“更改上下文”时才应用限制


注意:在命令行上下文(即
/etc/php/7.0/cli/php.ini
)中将
允许打开是我在系统上所做的,但我怀疑它与我提供的解释没有任何关系,即使它被设置为
Off
,当然,除非您通过从命令行本身运行脚本来进行测试。我没有在命令行上下文中将
allow\u url\u fopen
设置为
Off
时测试该行为。

从cPanel或PHPINI部分中的WHM启用allow\u url\u fopen这不是完全正确的。您可以使用cURL with
file\u get\u contents()
和configure选项
——with curlwrappers
@artifact我不明白。请详细说明。如果使用
--使用curlwrappers
编译curl扩展名,则每当执行
文件获取内容(“http://example.com/stuff)
。我的观点是,curl和
file\u get\u contents
不是正交的,不是“使用一个或另一个”。你也可以添加一些背景信息,让人们知道为什么,而不仅仅是做什么。这太棒了。。我刚刚将我的所有“filt_get_content($url)…函数替换为“get_content($url)”,并将您的函数放在文件的顶部。。哇。。所有的功能都运行得很顺利。。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.your_external_website.com");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
file_get_contents('php://input');
// Path outside document root that webserver user agent has permission to read. e.g. for an apache2 webserver this user agent might be www-data so a file at /etc/php/7.0/filetoaccess would be successfully read if www-data had permission to read this file
file_get_contents('<file path to file on local machine user agent can access>');
// Relative path in same document root
file_get_contents('data/filename.dat')