如何处理PHP中的file_get_contents()函数警告?

本文翻译自:How can I handle the warning of file_get_contents() function in PHP?

I wrote a PHP code like this 我写了这样的PHP代码

$site="http://www.google.com";
$content = file_get_content($site);
echo $content;

But when I remove "http://" from $site I get the following warning: 但是,当我从$site删除“ http://”时,出现以下警告:

Warning: file_get_contents(www.google.com) [function.file-get-contents]: failed to open stream: 警告:file_get_contents(www.google.com)[function.file-get-contents]:无法打开流:

I tried try and catch but it didn't work. 我想trycatch ,但没有奏效。


#1楼

参考:https://stackoom.com/question/18qv/如何处理PHP中的file-get-contents-函数警告


#2楼

My favourite way to do this is fairly simple: 我最喜欢的方法很简单:

if (!$data = file_get_contents("http://www.google.com")) {
      $error = error_get_last();
      echo "HTTP request failed. Error was: " . $error['message'];
} else {
      echo "Everything went better than expected";
}

I found this after experimenting with the try/catch from @enobrev above, but this allows for less lengthy (and IMO, more readable) code. 我在try/catch上述@enobrev的try/catch之后发现了这一点,但这可以减少冗长的代码(以及IMO,更易读)。 We simply use error_get_last to get the text of the last error, and file_get_contents returns false on failure, so a simple "if" can catch that. 我们仅使用error_get_last来获取上一个错误的文本,而file_get_contents在失败时返回false,因此可以使用简单的“ if”来捕获错误。


#3楼

Here's how I handle that: 这是我的处理方式:

$this->response_body = @file_get_contents($this->url, false, $context);
if ($this->response_body === false) {
    $error = error_get_last();
    $error = explode(': ', $error['message']);
    $error = trim($error[2]) . PHP_EOL;
    fprintf(STDERR, 'Error: '. $error);
    die();
}

#4楼

function custom_file_get_contents($url) {
    return file_get_contents(
        $url,
        false,
        stream_context_create(
            array(
                'http' => array(
                    'ignore_errors' => true
                )
            )
        )
    );
}

$content=FALSE;

if($content=custom_file_get_contents($url)) {
    //play with the result
} else {
    //handle the error
}

#5楼

Since PHP 4 use error_reporting() : 由于PHP 4使用error_reporting()

$site="http://www.google.com";
$old_error_reporting = error_reporting(E_ALL ^ E_WARNING);
$content = file_get_content($site);
error_reporting($old_error_reporting);
if ($content === FALSE) {
    echo "Error getting '$site'";
} else {
    echo $content;
}

#6楼

You can prepend an @: $content = @file_get_contents($site); 您可以在@前面加上: $content = @file_get_contents($site);

This will supress any warning - use sparingly! 这将禁止任何警告- 谨慎使用! . See Error Control Operators 请参阅错误控制运算符

Edit: When you remove the 'http://' you're no longer looking for a web page, but a file on your disk called "www.google....." 编辑:当您删除“ http://”时,您不再在寻找网页,而是在磁盘上名为“ www.google .....”的文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: file_get_contents 函数PHP 用于读取文件内容的函数,它可以读取本地文件或远程文件的内容,并将其作为字符串返回。该函数可以用于读取文本文件、HTML 文件、XML 文件、JSON 文件等。同时,该函数还支持设置 HTTP 请求头、超时时间等参数,以满足不同的需求。 ### 回答2: file_get_contents函数PHP一个用于读取文件内容的函数,其作用是从指定的文件或者URL读取数据,返回一个字符串用于处理或者输出。 它的使用十分简单,只需要传入一个文件路径或者URL即可,它会返回读取到的数据。 在读取本地文件时,需要传入文件的完整路径,并且需要确保文件有读取权限。而如果要读取远程文件,则可以直接传入远程URL。读取远程文件需要开启相关的PHP扩展(例如PHP Curl或者allow_url_fopen选项)。 file_get_contents函数可以用来读取各种各样的数据类型,包括文本、HTML、XML、JSON以及图片等二进制文件。在读取二进制文件时,需要将返回的字符串进行解码才能正常使用。除此之外,file_get_contents函数还可以设置一些可选参数,如是否使用SSL、是否缓存等等。 需要注意的是,如果读取文件失败,则file_get_contents函数会返回false。因此,在使用时需要判断返回值是否为false,以避免出现不必要的错误。同时,由于它会将整个文件内容读取到内存,因此在读取大文件时需要谨慎使用,以避免内存占用过大导致程序崩溃。 总的来说,file_get_contents函数PHP一个非常实用的函数,它可以用于读取各种数据类型,并且使用非常方便。同时,需要注意其的一些细节,以确保程序能够正常运行。 ### 回答3: file_get_contents 是一个 PHP 函数,用于读取指定 URL 或本地文件,返回该文件的内容。 使用该函数需要传入一个参数,可以是一个 URL 或者是本地文件路径。如果传入的是 URL,则该函数会尝试从该 URL 获取文件内容。如果传入的是本地文件路径,则该函数会尝试读取该路径下的文件内容。 该函数返回读取的文件内容作为一个字符串。如果读取失败,则返回 FALSE。 使用 file_get_contents 函数可以方便地读取远程服务器上的文件,从而实现一些远程数据的获取和处理。同时也可以用于读取本地文件的内容,方便地进行文件操作。 在使用 file_get_contents 函数时,需要注意一些安全性问题。由于该函数可以读取远程服务器上的任意文件,因此可能会被攻击者利用进行远程文件包含等攻击。因此,需要谨慎使用该函数,避免安全漏洞的产生。 总之,file_get_contents 可以方便地读取 URL 或本地文件的内容,是一个非常常用的 PHP 函数。但同时需要注意安全性问题,进行谨慎的使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值