phpurlhttp-status-codeslink-checking

What is the best way to check if a URL exists in PHP?


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


Solution

  • You can use get_headers($url)

    Example 2 from Manual:

    <?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
    )
    

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

    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.

    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 also provides the variable $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.

    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.