HipChat PHP Curl 调用的 400 错误

400 Error for HipChat PHP Curl Call

我正在尝试创建一个到 hipchat 房间的 curl 通知,它生成的通知令牌仅适用于该房间。

curl -d '{"color":"green","message":"My first notification
(yey)","notify":false,"message_format":"text"}' -H 'Content-Type: 
application/json' <My URL and TOKEN GO HERE>

我的代码:

<?php

  $payload = array("message"=>"testing");
  print_r($payload);
  $json = json_encode($payload,true);
  $curl = curl_init();
  curl_setopt ($curl, CURLOPT_URL,     "my url with token"
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
  $ch = curl_exec ($curl);
  curl_close ($curl);

  print_r($ch);

?>

我得到的结果是:

Array
(
     [message] => testing
)
{
"error": {
"code": 400,
 "field": "message",
 "message": "Required field 'message' is missing",
 "type": "Bad Request",
 "validation": "optional",
 "value": null
 }

我试过 https://github.com/hipchat/hipchat-php and https://github.com/rcrowe/Hippy 但他们需要管理员 API 令牌。

谢谢, 国际联合会

您需要像在命令行中一样发送 Content-Type header 和您的 curl 请求,否则后端将不知道如何解析您的数据。

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));