PHP 8.2: Curl: CURLINFO_EFFECTIVE_METHOD
support in curl_getinfo
function
Curl extension provides a function named curl_getinfo()
that returns information about a specific Curl transfer. In PHP 8.2 and later, curl_getinfo
function returns the effective method of a request, which might different from the initial request method (e.g. from an HTTP POST
request to a GET
request).
If the curl_getinfo()
function is called without any parameters, it returns an array of all available information about the transfer. In PHP 8.2 and later, the returned array contains a value with key effective_method
, that contains the name of the effective method of an HTTP request.
Further, it is also possible to retrieve the effect method value specifically by passing CURLINFO_EFFECTIVE_METHOD
constant to curl_getinfo()
function call.
This feature is only available if PHP Curl is built with Curl version 7.72.0 or later.
Usage Examples
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($info);
array(38) {
["url"]=> string(20) "https://example.com/"
["content_type"]=> string(24) "text/html; charset=UTF-8"
["http_code"]=> int(200)
...
["total_time_us"]=> int(1029949)
+ ["effective_method"]=> string(3) "GET"
}
The effective_method
information is useful when the target server emits a redirect response, and Curl is configured to follow it. For example, most of the web form submissions follow Post-Redirect-Get pattern, which the server emits a redirect response to the same URL after a form is submitted. In such cases, the effective_method
field will contain the correct effective method (GET
), as opposed to the initial request method (POST
).
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_EFFECTIVE_METHOD);
var_dump($info);
string(3) "GET"
CURLINFO_EFFECTIVE_METHOD
Constant
CURLINFO_EFFECTIVE_METHOD
is a new constant added in PHP 8.2, with an integer value of 1048634
assigned to it. This constant is declared in the global namespace.
Backwards Compatibility Impact
CURLINFO_EFFECTIVE_METHOD
is a new PHP constant. While it is possible to backport the constant declaration itself to PHP versions prior to PHP 8.2, it will not be accepted by curl_getinfo
function, and returns false
.
The constant and the functionality is only available if PHP is compiled with Curl version 7.72.0 or later.