Sending HTTP HEAD Request [PHP Code]

The HTTP HEAD request method is used to request HTTP headers from the server. The HTTP HEAD method is similar to the HTTP GET method, except that the server does not return the content of the requested resource. The HEAD method is used to get additional information about a resource, such as a resource size, availability, and last modification date, before downloading it. The HTTP HEAD requests, like an HTTP GET, cannot contain data in the message's body; the request data must be passed to the server in the URL. In this HTTP HEAD request example, we are sending a HEAD request to the ReqBin echo URL. Click Send to run the HEAD request online and see the results. The PHP code was automatically generated for the HEAD Request example.
Sending HTTP HEAD Request [PHP Code] Send
HEAD /sample/head/json HTTP/1.1
Host: reqbin.com
Authorization: Bearer mt0dgHmLJMVQhvjpNXDyA83vA_PxH23Y

Updated: Viewed: 44643 times
PHP code for HEAD Request example

PHP code for HEAD Request Example

This PHP code snippet was generated automatically for the HEAD Request example.
<< Back to the HEAD Request example

What is HTTP?

HTTP (Hypertext Transfer Protocol) is a data transfer protocol that transfers data between an HTTP client (browser or mobile app) and a server. HTTP is built around messages called request and response. The client sends a request to the server, then the server processes this request and sends a response back to the client. Each HTTP message consists of a request/response line, HTTP headers, and a message body.

What is the HEAD request method used for?

The HTTP HEAD method is one of 9 commonly used Hypertext Transfer Protocol (HTTP) request methods. It is used to retrieve HTTP headers from the server. An HTTP HEAD request is made before loading a large resource to check resource size, validity, accessibility, and recent modification time. HTTP HEAD requests cannot have a message body. The HTTP HEAD method is defined as idempotent, which means that multiple identical HEAD requests should have the same effect as a single request.

Basic HEAD Request Example

The following is an example of sending the HEAD request to the ReqBin echo URL:

HEAD Request Example
HEAD /echo HTTP/1.1
Host: reqbin.com

How to send HTTP headers using the HEAD method?

You can send any HTTP headers along with the HEAD request. For example, you can send additional information about your browser or mobile application or send user authentication credentials in the headers. By default, browsers send the User-Agent, Accept, Accept-Encoding, and Referer HTTP headers on every HEAD and GET request.

Can I send data using the HEAD method?

HTTP HEAD requests cannot have a message body, but it is possible to send data to the server using URL parameters. However, keep in mind that the maximum URL size will be limited to about 2000 characters (depending on the browser) and that this method is not suitable for transferring sensitive data.

What is the difference between the HEAD and GET request methods?

The main difference between HTTP HEAD and GET requests is that for a HEAD request, the server only returns headers without a response body, but specifies the size of the response content using the Content-Length header. The HEAD method is faster than the HTTP GET method because less data is transferred in HEAD requests.

See also

Generate code snippets for PHP and other programming languages

Convert your HEAD Request request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.