CURLOPT_PUT 与 CURLOPT_POSTFIELDS [英] CURLOPT_PUT vs CURLOPT_POSTFIELDS

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

问题描述

当我通过 curl 将 PUT 请求 API 发送到 REST 时,我发现了奇怪的行为.如果设置参数curl_setopt($curl, CURLOPT_PUT, true),则查询,其中CURLOPT_POSTFIELDS不为空,则查询执行时间为1.5分钟(好像取决于一些超时).如果使用参数 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT") 发送相同的请求,则查询执行持续大约 1 秒,这是应该的.有人能解释一下这些参数之间的根本区别吗?

When I sent the PUT request API via curl to the REST, I found strange behavior. If you set the parameter curl_setopt($curl, CURLOPT_PUT, true), then queries, in which CURLOPT_POSTFIELDS is not empty, then the query execution lasts 1.5 minutes (as if it depends on some timeout). And if the same request is sent with the parameter curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"), then the query execution lasts about 1 second, as it should be. Can someone explain the fundamental difference between these parameters?

示例代码:

$data = http_build_query(array("enable"=> 1));

if( $curl = curl_init() ) {
    curl_setopt($curl, CURLOPT_URL, BASE_URL .'users/2');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);    
    curl_setopt($curl, CURLOPT_PUT, true); // execution time 1.5 min
    //curl_setopt ($ curl, CURLOPT_CUSTOMREQUEST, "PUT"); - execution time 1 sec
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $out = json_decode(curl_exec($curl));
    curl_close($curl);      
}

推荐答案

如果你看一下 文档,它说当您将 CURLOPT_PUT 设置为 true 时,必须将文件设置为 PUT>CURLOPT_INFILE 和 CURLOPT_INFILESIZE(在您的情况下,您没有设置文件).

If you look at the documentation, It says that when you set CURLOPT_PUT to truethen the file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE (In your case you are not setting the file).

CURLOPT_CUSTOMREQUEST 设置为 PUT 方法不期望文件,这是 CURLOPT_CUSTOMREQUESTCURLOPT_PUT 之间的主要区别.

Setting CURLOPT_CUSTOMREQUEST to PUT method is not expecting the file which is the main difference between CURLOPT_CUSTOMREQUEST and CURLOPT_PUT.

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

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