PHP file_get_contents() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12294124/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:16:51  来源:igfitidea点击:

PHP file_get_contents() not working

phpcodepad

提问by Kristy Saulsbury

Can anyone explain why the following code returns a warning:

谁能解释为什么以下代码会返回警告:

<?php
  echo file_get_contents("http://google.com");
?>

I get a Warning:

我收到警告:

Warning: file_get_contents(http://google.com): 
failed to open stream: No such file or directory on line 2

See codepad

查看键盘

回答by Sudhir Bastakoti

As an alternative, you can use cURL, like:

作为替代方案,您可以使用 cURL,例如:

$url = "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

See: cURL

请参阅:卷曲

回答by Adelmar

Try this function in place of file_get_contents():

试试这个函数来代替 file_get_contents():

<?php

function curl_get_contents($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

It can be used just like file_get_contents(), but uses cURL.

它可以像 file_get_contents() 一样使用,但使用 cURL。

Install cURL on Ubuntu (or other unix-like operating system with aptitude):

在 Ubuntu(或其他具有 aptitude 的类 Unix 操作系统)上安装 cURL:

sudo apt-get install php5-curl
sudo /etc/init.d/apache2 restart

See also cURL

另见卷曲

回答by Spudley

This is almost certainly caused by the config setting that allows PHP to disable the ability to open URLs using the file handling functions.

这几乎肯定是由允许 PHP 禁用使用文件处理功能打开 URL 的能力的配置设置引起的。

If you can change you PHP.ini, try switching on the allow_url_fopensetting. See also the man page for fopenfor more in (the same restictions affect all file handling functions)

如果您可以更改 PHP.ini,请尝试打开allow_url_fopen设置。另请参阅fopen手册页了解更多信息(相同的限制影响所有文件处理功能)

If you can't switch on the flag, you'll need to use a different method, such as Curl, to read your URL.

如果您无法打开标志,则需要使用不同的方法(例如 Curl)来读取您的 URL。

回答by Conrad Lotz

If you run this code:

如果您运行此代码:

<?php
    print_r(stream_get_wrappers());
?>

in http://codepad.org/NHMjzO5pyou see the following array:

http://codepad.org/NHMjzO5p 中,您会看到以下数组:

Array
(
    [0] => php
    [1] => file
    [2] => data
)

Run the same code in on Codepad.Viper - http://codepad.viper-7.com/lYKihIyou will see that the http stream has been enabled thus file_get_contentsnot working in codepad.org.

在 Codepad.Viper - http://codepad.viper-7.com/lYKihI上运行相同的代码,您将看到 http 流已启用,因此file_get_contents在 codepad.org 中不起作用。

Array 
( 
    [0] => https 
    [1] => ftps 
    [2] => compress.zlib 
    [3] => php 
    [4] => file 
    [5] => glob 
    [6] => data 
    [7] => http 
    [8] => ftp 
    [9] => phar 
)

If you run your question code above in Codepad.Viper then it open the google page. The difference thus is the httpstream that is disable in your CodePad.org and enabled in CodePad.Viper.

如果您在 Codepad.Viper 中运行上面的问题代码,则会打开谷歌页面。因此,不同之处http在于在 CodePad.org 中禁用和在 CodePad.Viper 中启用的流。

To enable it read the following post How to enable HTTPS stream wrappers. Alternatively use cURL.

要启用它,请阅读以下文章如何启用 HTTPS 流包装器。或者使用cURL.

回答by John Carlson

Try a trailing slash after the hostname.

尝试在主机名后添加斜杠。

<?php
    echo file_get_contents("http://google.com/");
?>

回答by Tony07

you can try using single quotes like this:

您可以尝试使用这样的单引号:

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