Php 如何避免文件“获取”内容错误:;不能';t解析主机名“;

Php 如何避免文件“获取”内容错误:;不能';t解析主机名“;,php,Php,我成功地用 file_get_contents("http://www.site.com"); 但是,如果url不存在,或者无法访问,我会 Warning: file_get_contents(http://www.site.com) [function.file-get-contents]: failed to open stream: operation failed in /home/track/public_html/site.php on line 773 是否可以回显“无法访

我成功地用

file_get_contents("http://www.site.com");
但是,如果url不存在,或者无法访问,我会

Warning: file_get_contents(http://www.site.com) [function.file-get-contents]: 
failed to open stream: operation failed in /home/track/public_html/site.php 
on line 773
是否可以
回显“无法访问站点”而不是错误?

您可以将
@
与以下内容一起使用:

@
抑制错误消息时,消息文本可在
$php\u errormsg

但请注意,
$php\u errormsg
在默认情况下是禁用的。您必须打开
跟踪错误
。因此,在代码顶部添加:

ini_set('track_errors', 1);
但是,有一种方法不依赖于跟踪误差:

if(@file_get_contents($url) === FALSE) {
    $error = error_get_last();
    if(!$error) {
        die('An unknown error has occured');
    } else {
        die($error['message']);
    }
}
这应该起作用:

@file_get_contents("http://www.site.com");

@
禁止PHP输出警告和错误。然后您必须自己处理一个空响应。

您可以在PHP中关闭警告:

见文件中的:

返回值:函数在失败时返回读取数据或FALSE

或在函数之前写入@,以避免看到错误:


@文件获取内容(…

我倾向于触发异常而不是错误消息:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    // see http://php.net/manual/en/class.errorexception.php
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}

set_error_handler("exception_error_handler");
现在您可以捕获如下错误:

try {
    $content = file_get_contents($url);
} catch (ErrorException $ex) {
    echo 'Site not reachable (' . $ex->getMessage() . ')';
}

您可以使用curl来避免显示php错误:

    $externalUrl = ** your http request **

    curl_setopt($curl, CURLOPT_URL, $externalUrl); // Set the URL
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'); // Use your user agent
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Set so curl_exec returns the result instead of outputting it.
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Bypass SSL Verifyers
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
    ));

    $result = curl_exec($curl); // send request
    $result = json_decode($result);

请尝试
@file\u获取内容(…)
:)谢谢。但是,我在某个地方读到,我不应该使用@,因为这不是一个好方法。重复问题:。@user198989使用
@
可以处理返回值。这是去Go的路你能指定“某处”吗?您不会比在代码中添加一个字符更容易;)嗯,但错误总是处理问题的好方法,只有这一个让我恶心。如果请求失败,可能会有很多问题。更好的方法来处理PHP扩展-CURL,如果您想确切地知道在请求之后发生了什么可能是DNS请求失败-错误的404501页-服务器超时。无论如何,如果请求是错误的,您将收到FALSE。不确定,是否需要重新处理异常。。无论如何,在生产服务器中禁用警告会更好。但是你会陷入麻烦,在错误处理程序中乱来,就像这样
使用@确实可以被认为是错误的
LOL:D是的,你提出了一个很好的替代方案,但在这样说的时候要小心things@migg您始终可以使用
restore\u error\u handler
还原错误处理程序,但您没有也没有提到它。而且,您的错误处理程序中没有
die()。。。这也可能被认为是错误的。是的,你是对的。使用它并没有错,只是取决于观点和使用它的环境。
    $externalUrl = ** your http request **

    curl_setopt($curl, CURLOPT_URL, $externalUrl); // Set the URL
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'); // Use your user agent
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Set so curl_exec returns the result instead of outputting it.
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Bypass SSL Verifyers
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
    ));

    $result = curl_exec($curl); // send request
    $result = json_decode($result);