当 url 不存在时 file_get_contents
我正在使用 file_get_contents() 访问 URL。
file_get_contents('http://somenotrealurl.com/notrealpage');
如果 URL 不真实,则返回此错误消息。如何让它优雅地出错,以便我知道该页面不存在并采取相应的操作而不显示此错误消息?
file_get_contents('http://somenotrealurl.com/notrealpage')
[function.file-get-contents]:
failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
in myphppage.php on line 3
例如在 zend 中你可以说: if ($request->isSuccessful())
$client = New Zend_Http_Client();
$client->setUri('http://someurl.com/somepage');
$request = $client->request();
if ($request->isSuccessful()) {
//do stuff with the result
}
I'm using file_get_contents() to access a URL.
file_get_contents('http://somenotrealurl.com/notrealpage');
If the URL is not real, it return this error message. How can I get it to error gracefully so that I know that the page doesn't exist and act accordingly without displaying this error message?
file_get_contents('http://somenotrealurl.com/notrealpage')
[function.file-get-contents]:
failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
in myphppage.php on line 3
for example in zend you can say: if ($request->isSuccessful())
$client = New Zend_Http_Client();
$client->setUri('http://someurl.com/somepage');
$request = $client->request();
if ($request->isSuccessful()) {
//do stuff with the result
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您需要检查 HTTP 响应代码:
You need to check the HTTP response code:
对于 PHP 中的此类命令,您可以使用
@
作为前缀来抑制此类警告。file_get_contents() 返回
FALSE
如果发生故障,因此如果您检查返回的结果,就可以处理故障With such commands in PHP, you can prefix them with an
@
to suppress such warnings.file_get_contents() returns
FALSE
if a failure occurs, so if you check the returned result against that then you can handle the failure每次使用 http 包装器调用
file_get_contents
时,都会在本地范围内创建一个变量:$http_response_header该变量包含所有 HTTP 标头。此方法比
get_headers()
函数更好,因为只执行一个请求。注意:2 个不同的请求可能会以不同的方式结束。例如,
get_headers()
将返回 503,而 file_get_contents() 将返回 200。您将获得正确的输出,但由于 get_headers() 调用中的 503 错误而不会使用它。这种方法还允许您跟踪存储在不同变量中的少数请求标头,因为如果您使用 file_get_contents() $http_response_header 在本地范围内被覆盖。
Each time you call
file_get_contents
with an http wrapper, a variable in local scope is created: $http_response_headerThis variable contains all HTTP headers. This method is better over
get_headers()
function since only one request is executed.Note: 2 different requests can end differently. For example,
get_headers()
will return 503 and file_get_contents() would return 200. And you would get proper output but would not use it due to 503 error in get_headers() call.This aproach also alows you to have track of few request headers stored in different variables since if you use file_get_contents() $http_response_header is overwritten in local scope.
虽然
file_get_contents
非常简洁和方便,但我倾向于使用 Curl 库来实现更好的控制。这是一个例子。While
file_get_contents
is very terse and convenient, I tend to favour the Curl library for better control. Here's an example.您可以添加 'ignore_errors' => true 选项:
在这种情况下,您将能够从服务器读取响应。
You may add 'ignore_errors' => true to options:
In that case you will be able to read a response from the server.
简单且实用(易于在任何地方使用):
示例:
Simple and functional (easy to use anywhere):
Example:
为了避免双重请求,如 Orbling 对 ynh 你可以结合他们的答案。如果您首先得到有效的回复,请使用它。如果没有找出问题是什么(如果需要)。
To avoid double requests as commented by Orbling on the answer of ynh you could combine their answers. If you get a valid response in the first place, use that. If not find out what the problem was (if needed).
普通
专业
Wtf级别
Normal
Pro
Wtf level