如何重设CURLOPT_CUSTOMREQUEST [英] How to reset CURLOPT_CUSTOMREQUEST

查看:1024
本文介绍了如何重设CURLOPT_CUSTOMREQUEST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用REST API,除其他外,该API使用 Delete 方法,如下所示:

I’m using a REST API which, among other things, uses the DELETE method like this:

DELETE /resources/whatever/items/123

访问

self::$curl = curl_init();
curl_setopt_array(self::$curl, array(
    CURLOPT_AUTOREFERER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_RETURNTRANSFER => true,
));

如您所见,我的cURL实例是静态的,将被重复用于后续调用。在内置请求方法之间进行切换时,这可以很好地工作。例如,在我的 get()方法中,我这样做:

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',
));

然后运行 curl_exec()。通过 CURLOPT_HTTPGET 显式设置请求方法,将清除以前可能的 CURLOPT_POST

and then run curl_exec(). By explicitly setting the request method via CURLOPT_HTTPGET, a possible previous CURLOPT_POST will be cleared.

但是,设置 CURLOPT_CUSTOMREQUEST (例如设置为 Delete )将覆盖任何其他内置请求方法。只要我想 DELETE 事情就可以了,但是例如调用 curl_setopt(self :: $ curl,CURLOPT_HTTPGET,true)不会重置自定义方法; 删除仍将使用。

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.

我尝试设置 CURLOPT_CUSTOMREQUEST 转换为 null false 或空字符串,但这只会导致HTTP请求,如

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

ie

我知道我可以设置 CURLOPT_CUSTOMREQUEST 改为 GET 并执行GET请求而没有任何问题,但是我想知道是否可以重置 CURLOPT_CUSTOMREQUEST

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.

推荐答案

这实际上是 PHP中的错误,因为原始的 documentation 声明以下内容:

This is actually a bug in PHP, since the original documentation states the following:


通过将其设置为NULL恢复为内部默认值。

Restore to the internal default by setting this to NULL.

很遗憾,正如您从源代码,在将选项值传递给基础天秤座之前,将其转换为字符串ry。

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.

我写了拉动请求,该问题已解决,并允许 CURLOPT_CUSTOMREQUEST传递 NULL code>选项值。

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.

此修复程序已应用于5.5.11和5.6.0(beta1)。

The fix has been applied to 5.5.11 and 5.6.0 (beta1).

这篇关于如何重设CURLOPT_CUSTOMREQUEST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆