400 HipChat PHP Curl调用错误


400 Error for HipChat PHP Curl Call

我正在尝试创建一个到hipchat房间的curl通知,它生成的notification Token只是为该房间创建的。

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和https://github.com/rcrowe/Hippy但它们需要管理员API令牌。

谢谢,IJC

您需要像在命令行上那样发送带有curl请求的Content-Type标头,否则后端将不知道如何解析数据。

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