Sending OPTIONS Request with Curl [PHP Code]

To make an OPTIONS request with Curl, you need to pass the -X OPTIONS command-line parameter to the Curl request. Browsers send OPTIONS requests when making a CORS request to another origin. The OPTIONS request does not return any data. All information is returned in the response headers. In this Curl OPTIONS Request Example, we send an OPTIONS request to the ReqBin echo URL. Click Run to execute the Curl OPTIONS request online and see the results. The PHP code was automatically generated for the Curl OPTIONS Request example.
Sending OPTIONS Request with Curl [PHP Code] Run
curl https://api.reqbin.com/api/v1/requests
   -X OPTIONS 
   -H "Access-Control-Request-Method: POST"
   -H "Access-Control-Request-Headers: content-type"
   -H "Origin: https://reqbin.com"
Updated: Viewed: 65104 times
PHP code for Curl OPTIONS Request example

PHP code for Curl OPTIONS Request Example

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

What is Curl?

Curl is a command-line tool available for Linux, Windows, and macOS and a cross-platform library (libcurl) that can be used with almost any application. Curl supports over 25+ protocols, including HTTP, HTTPS, FTP, and SFTP. Curl has built-in support for SSL, certificate validation, HTTP Cookies, and user authentication.

What is HTTP OPTIONS?

The HTTP OPTIONS method is one of the nine standard Hypertext Transfer Protocol (HTTP) request methods used to describe communication options for the target resource. The HTTP OPTIONS method represents a request for information about the communication options available on the request/response chain identified by the Request-URI. The HTTP OPTIONS method allows the client to determine the options or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval. Responses to the OPTIONS method are not cacheable. The HTTP OPTIONS method is defined as idempotent, which means that multiple identical OPTIONS requests should have the same effect as a single request. When you send data to a different domain for security reasons, browsers usually send a 'preflight' HTTP OPTIONS request to the target server before sending the data there.

What is CORS?

The OPTIONS request is a preflight request, part of the CORS (Cross-Origin Resource Sharing). CORS is an HTTP header-based mechanism that allows the server to specify any other source the browser should obtain resources or send data. These sources can differ from the current by the hostname, HTTP scheme, or port number. CORS implementation was due to single-origin policy restrictions. The same-origin policy restricts resources to interact only with resources located in the same domain. This is useful because, thanks to the same-origin policy followed by XMLHttpRequest and fetch, JavaScript can only make calls to URLs that are on the exact origin as the location where the script is running.

Sending an HTTP OPTIONS request with Curl

Although the CORS specification does not define the use of the request body, future HTTP extensions may use the OPTIONS request body to make more detailed requests to the server. If the target server does not support expanding the OPTIONS request body, it can dismiss the request body.

Curl OPTIONS Request Syntax

The general form of the Curl command for sending a OPTIONS request is as follows:

Curl OPTIONS Syntax
curl [URL] -X OPTIONS [HEADERS]

Curl OPTIONS Request Example

An example of sending an OPTIONS request to the ReqBin echo URL using the -X OPTIONS command-line option:

Curl OPTIONS Example
curl https://api.reqbin.com/api/v1/requests
   -X OPTIONS 
   -H "Access-Control-Request-Method: POST"
   -H "Origin: https://reqbin.com"

See also

Generate code snippets for PHP and other programming languages

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