PHP:获取fopen()HTTP响应错误代码 [英] PHP: Get fopen() HTTP response error code
问题描述
我想通过 fopen()
函数打开远程文件时获取HTTP错误代码。
I would like to get the HTTP error code while opening a remote file via fopen()
function.
我有以下代码:
$remote = fopen ($url, "rb");
如果网址正常,则会打开该文件。否则, fopen
会触发类似的错误消息警告:fopen(url):无法打开流:HTTP请求失败!找不到HTTP / 1.1 404
。
If the URL is fine, the file would be opened. Otherwise, fopen
triggers an error message similar to Warning: fopen(url): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
.
我知道在之前添加 @
fopen()
将取消错误消息,但如何获取http错误代码呢?
I know that adding a @
before the fopen()
would suppress the error message,
but how can I get the http error code instead?
在这里,我想在一个变量中得到 HTTP / 1.1 404 Not Found
。
Here, I want to get HTTP/1.1 404 Not Found
in a variable.
谢谢。
推荐答案
$ http_response_header
将返回响应标头。
因此您可以使用 $ http_response_header获取第一行[0]
在这种情况下,将完全 HTTP / 1.1 404 Not Found
。
The $http_response_header
will return the response header.
So you can get the first line by using $http_response_header[0]
which in this case, will be exactly HTTP/1.1 404 Not Found
.
$remote = @fopen ($url, "rb");
if (!$remote) {
echo "Error: " . $http_response_header[0];
}
这篇关于PHP:获取fopen()HTTP响应错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!