简体   繁体   English

PHP curl 奇怪的行为 PUT 与 POST

[英]PHP curl strange behavior PUT vs. POST

One of my projects contains an interface to a web based database, which I call using curl.我的一个项目包含一个基于 Web 的数据库的接口,我使用 curl 调用它。 A curl with -X PUT will create a new record, a curl with -X POST will update an existing record.带有 -X PUT 的 curl 将创建一条新记录,带有 -X POST 的 curl 将更新现有记录。 If I perform this using Advanced REST Client, it works as it should.如果我使用 Advanced REST Client 执行此操作,它会正常工作。 If I try this using a call of curl_exec out of my PHP script, the POST works but the PUT fails.如果我使用 PHP 脚本中的curl_exec调用来尝试此操作,则 POST 可以正常工作,但 PUT 会失败。 'Fails' means, I get a http 100 as response instead a 200 or 400. “失败”意味着,我得到一个 http 100 作为响应,而不是 200 或 400。

$strXML = "<XML></XML>"; //valid XML here
$cURL = "https://www.webURL.here";
$cToken = "this_is_my_very_secret_token";
$ch = curl_init();

//POST

curl_setopt_array($ch, array(
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => "$strXML",
                CURLOPT_URL => $cURL,
                CURLOPT_HTTPHEADER => array("X-Auth-Token: $cToken","Content-Type:text/xml"),
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_VERBOSE => true
));
$strXMLEvents = curl_exec($ch);

The PUT call looks similar: PUT 调用看起来类似:

// PUT
curl_setopt_array($ch, array(
                    CURLOPT_PUT => true,
                    CURLOPT_POSTFIELDS => "$strXML",
                    CURLOPT_URL => $cURL,
                    CURLOPT_HTTPHEADER  => array("X-Auth-Token: $cToken","Content-Type:text/xml","Content-Length: ".strlen($strXML)),
                    CURLOPT_RETURNTRANSFER  => true,
                    CURLOPT_VERBOSE     => true
));
$strXMLEvents = curl_exec($ch);

Since I encounter this on my developing system (a win 10 PC), I thought, this could be the reason.由于我在我的开发系统(一台 win 10 PC)上遇到了这个问题,我想,这可能是原因。 But after I deployed the code onto the Linux webserver, the behavior stays the same...但是在我将代码部署到 Linux 网络服务器上之后,行为保持不变......

Since the transfer works "manually" with ARC, I suspect a bug in my script - but I am not able to find it.由于传输是通过 ARC“手动”进行的,我怀疑我的脚本中存在错误 - 但我无法找到它。

from the manual:从手册:

CURLOPT_PUT

TRUE to HTTP PUT a file. TRUE 到 HTTP PUT 文件。 The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE.必须使用 CURLOPT_INFILE 和 CURLOPT_INFILESIZE 设置要 PUT 的文件。

since we are not putting a file, instead of CURLOPT_PUT => true try CURLOPT_CUSTOMREQUEST => "PUT"因为我们没有放置文件,而不是CURLOPT_PUT => true尝试CURLOPT_CUSTOMREQUEST => "PUT"

// PUT
curl_setopt_array($ch, array(
                    CURLOPT_CUSTOMREQUEST => "PUT",
                    CURLOPT_POSTFIELDS => "$strXML",
                    CURLOPT_URL => $cURL,
                    CURLOPT_HTTPHEADER  => array("X-Auth-Token: $cToken","Content-Type:text/xml" /* ,"Content-Length: ".strlen($strXML) */ ),
                    CURLOPT_RETURNTRANSFER  => true,
                    CURLOPT_VERBOSE     => true
));
$strXMLEvents = curl_exec($ch);

This way we just change the HTTP verb from POST to PUT这样我们只需将 HTTP 动词从POST更改为PUT

I think, I got it!我想,我明白了!

Problem was, that I tried to send the content as a string.问题是,我试图将内容作为字符串发送。 As I send it as a file, it worked:当我将它作为文件发送时,它起作用了:

// PUT
$fXML=fopen($path2File,'r' ); // filestream with XML content
curl_setopt_array($ch, array(
                    CURLOPT_PUT => true,
                    CURLOPT_URL => $cURL,
                    CURLOPT_HTTPHEADER  => array("X-Auth-Token: $cToken","Content-Type:text/xml"),
                    CURLOPT_RETURNTRANSFER  => true,
                    CURLOPT_VERBOSE     => true,
                    CURLOPT_INFILESIZE =>strlen($strXML),
                    CURLOPT_INFILE => $fXML
));
$strXMLEvents = curl_exec($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM