HEAD request with PHP


Posted by Andrew McCombe on Thu Mar 30, 2017

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