HEAD request with PHP

March 30th, 2017

Warning: This post is 7 years old. Some of this information may be out of date.

If you want to perform a HEAD request with PHP on an URL you can use the stream functionality:

    public function checkHTTPFile($url)
    {
    
        stream_context_set_default([
            'http' =>; [
                'method' => 'HEAD'
             ]
        ]);
    
        $file_headers = @get_headers($url);
    
        if ($file_headers[0] == 'HTTP/1.1 200 OK') {
            return true;
        }
    
        return false;
    }

Example:

    checkHTTPFile('https://www.google.com');

For more information on PHP's stream functions refer to the manual here: https://secure.php.net/manual/en/function.stream-context-set-default.php