I’m using a REST API which, among other things, uses the DELETE
method like this:
DELETE /resources/whatever/items/123
To access this using PHP I’m using cURL like this:
self::$curl = curl_init();
curl_setopt_array(self::$curl, array(
CURLOPT_AUTOREFERER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
));
As you can see, my cURL instance is static and will be reused for subsequent calls. This works fine when switching between “builtin” request methods. For example, in my get()
method, I do something like this:
curl_setopt_array(self::$curl, array(
CURLOPT_HTTPGET => true,
CURLOPT_URL => self::BASE . 'whatever',
));
and then run curl_exec()
. By explicitly setting the request method via CURLOPT_HTTPGET
, a possible previous CURLOPT_POST
will be cleared.
However, setting CURLOPT_CUSTOMREQUEST
(for example to DELETE
) will override any other builtin request method. That’s fine as long as I want to DELETE
things, but calling for example curl_setopt(self::$curl, CURLOPT_HTTPGET, true)
will not reset the custom method; DELETE
will still be used.
I have tried setting CURLOPT_CUSTOMREQUEST
to null
, false
or the empty string, but this will only result in a HTTP request like
/resources/whatever/items/123
i.e. with the empty string as method, followed by a space, followed by the path.
I know that I could set CURLOPT_CUSTOMREQUEST
to GET
instead and do GET requests without any problems, but I wonder whether there is a possiblity to reset CURLOPT_CUSTOMREQUEST
.
This option can be used to specify the request: HTTP. Instead of GET or HEAD when performing HTTP based requests. This is particularly useful, for example, for performing an HTTP DELETE request.
curl setopt($ch, option, value) sets a cURL session option defined by the ch parameter. The value specifies the value for the specified option, and the option specifies which option to set. Return page contents with curl setopt($ch, CURLOPT RETURNTRANSFER, 1). If the value is zero, no output will be returned.
CURLOPT_RETURNTRANSFER. true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly. CURLOPT_SASL_IR. true to enable sending the initial response in the first packet. Added in cURL 7.31.
CURLOPT_POSTFIELDS. The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'.
This is actually a bug in PHP, since the original documentation states the following:
Restore to the internal default by setting this to NULL.
Unfortunately, as you can see from the source code, the option value gets cast to a string before it's passed to the underlying library.
I've written a pull request that addresses the issue and allows for NULL
to be passed for the CURLOPT_CUSTOMREQUEST
option value.
The above patch will take some time to get merged into the project, so until then you would have to explicitly set the method yourself once you start using this option.
The fix has been applied to 5.5.11 and 5.6.0 (beta1).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With