【问题标题】:Why is PHP cURL PUT request not working despite getting status code 200 in return?为什么尽管获得状态码 200 作为回报,PHP cURL PUT 请求仍不起作用?
【发布时间】:2019-08-06 15:13:55
【问题描述】:

我已经被这个问题困扰了一段时间了。我正在尝试使用 REST API 更改用户的某些设置,例如清除用户并将其设备设置为非活动状态。

REST 调用是在我很陌生的 php 中进行的。大多数调用(get 和 post)都工作得很好,所以我想我理解了 php 和 curl 的基本概念,但我就是无法让 put 请求正常工作。问题是,在进行 REST 调用时,我得到一个状态码 200 作为回报,表明一切正常,但是当我检查数据库时,没有任何变化并且设备仍然处于活动状态。

我在 stackexchange(cURL PUT Request Not Working with PHPPhp Curl return 200 but not postingPHP CURL PUT function not working)上花了几个小时研究这个问题,另外还阅读了各种教程。 对我来说,我的代码看起来不错并且非常有意义,因为它与我在网上找到的许多示例相似。所以请帮我找出我的错误。

$sn = "123456789";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/api/sn/".$sn);

$data = array("cmd" => "clearUser");
$headers = array(
    'Accept: application/json',
    'Content-Type: application/json'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$username = 'XXX';
$password = 'XXX';
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

$output = curl_exec($ch);
curl_close($ch);

【问题讨论】:

  • 如果设置Content-Type: application/json 可能curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));

标签: php curl request put


【解决方案1】:

据我所知,您的代码中有两个问题。

  1. Content-Type: application/json 不正确,我会完全删除它。
  2. 您没有Content-Length 标头。

我建议尝试

$sn = "123456789";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/api/sn/".$sn);

$data = array("cmd" => "clearUser");
$httpQuery = http_build_query($data);
$headers = array(
    'Accept: application/json',
    'Content-Length: ' . strlen($httpQuery)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$username = 'XXX';
$password = 'XXX';
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,$httpQuery);

$output = curl_exec($ch);
curl_close($ch);

【讨论】:

    【解决方案2】:

    状态 200 在 PUT 请求的情况下可能不成功。在正确的语义(服务器的正确实现)中,成功的 PUT 返回“201 Created”,如果客户端发送空的或某种错误的内容,服务器返回“204 No Content”。

    懒惰的程序员可能只返回“200 Ok”而不是 204,意思是“您的请求很好,但与数据无关”。

    尝试验证您的数据并确保您发送的内容不为空且符合 API 规范。

    【讨论】:

      【解决方案3】:

      您在 Header 'Content-Type: application/json' 中定义。尝试将您的 $data 编码为 json,然后传输 jsonEncodeteData:

      $dataJson = json_encode($data);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
      

      也许这已经有所帮助了。

      【讨论】:

        猜你喜欢
        • 2023-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-26
        • 1970-01-01
        • 2015-12-05
        相关资源
        最近更新 更多