PHP ini file_get_contents外部URL

ari*_*rik 39 php url ini external file-get-contents

我使用以下PHP函数:

file_get_contents('http://example.com');

每当我在某个服务器上执行此操作时,结果为空.当我在其他任何地方进行此操作时,结果就是页面的内容可能是什么.但是,当我在结果为空的服务器上,在本地使用该函数 - 无需访问外部URL(file_get_contents('../simple/internal/path.html');),它确实有效.

现在,我很确定它与某个php.ini配置有关.我不确定的是一个.请帮忙.

Ail*_*lyn 41

您正在寻找的设置是allow_url_fopen.

您可以通过两种方式绕过它,而无需更改php.ini,其中一种是使用fsockopen(),另一种是使用cURL.

我推荐使用cURL file_get_contents(),因为它是为此而构建的.

  • 如果使用`--with-curlwrappers'编译curl扩展,只要你执行`file_get_contents("http://example.com/stuff")`,就会使用curl发出HTTP请求.我的观点是curl和`file_get_contents`不是正交的,它不是"使用一个或另一个". (13认同)

Pab*_*blo 31

作为Aillyn答案的补充,您可以使用类似下面的函数来模拟file_get_contents的行为:

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');
Run Code Online (Sandbox Code Playgroud)


小智 5

$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);
Run Code Online (Sandbox Code Playgroud)

最适合 http url,但是如何打开 https url 帮助我