检查 PHP 中是否存在 URL 的最佳方法是什么?

问题描述:

查看 URL 存在且响应不是 404 的最佳方法是什么?

What is the best way to see a URL exists and the response is not a 404 ?

您可以使用 get_headers($url)

手册中的示例 2:

<?php
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
    array(
        'http' => array(
            'method' => 'HEAD'
        )
    )
);
print_r(get_headers('http://example.com'));

// gives
Array
(
    [0] => HTTP/1.1 200 OK 
    [Date] => Sat, 29 May 2004 12:28:14 GMT
    [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
    [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
    [ETag] => "3f80f-1b6-3e1cb03b"
    [Accept-Ranges] => bytes
    [Content-Length] => 438
    [Connection] => close
    [Content-Type] => text/html
)

第一个数组元素将包含 HTTP 响应状态代码.你必须解析它.

The first array element will contain the HTTP Response Status code. You have to parse that.

请注意,示例中的 get_headers 函数将发出 HTTP HEAD 请求,这意味着它不会获取 URL 的正文.这比使用也会返回正文的 GET 请求更有效.

Note that the get_headers function in the example will issue an HTTP HEAD request, which means it will not fetch the body of the URL. This is more efficient than using a GET request which will also return the body.

另请注意,通过设置 default 上下文,任何使用 http 流上下文的后续调用现在都将发出 HEAD 请求.因此,请确保在完成后重置默认上下文以再次使用 GET.

Also note that by setting a default context, any subsequent calls using an http stream context, will now issue HEAD requests. So make sure to reset the default context to use GET again when done.

PHP 还提供了 变量 $http_response_header

$http_response_header 数组类似于 get_headers() 函数.当使用 HTTP 包装器时,$http_response_header 将填充 HTTP 响应标头.$http_response_header 将在本地范围内创建.

The $http_response_header array is similar to the get_headers() function. When using the HTTP wrapper, $http_response_header will be populated with the HTTP response headers. $http_response_header will be created in the local scope.

如果你想下载远程资源的内容,你不想做两个请求(一个是查看资源是否存在,一个是获取它),而只是一个.在这种情况下,使用类似 file_get_contents 的东西来获取内容,然后检查变量中的标题.

If you want to download the content of a remote resource, you don't want to do two requests (one to see if the resource exists and one to fetch it), but just one. In that case, use something like file_get_contents to fetch the content and then inspect the headers from the variable.