phpcurlhipchat

400 Error for HipChat PHP Curl Call


I'm trying to create a curl notification to a hipchat room, the Notification Token it generates is just for that room.

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>

My Code:

<?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);

?>

I get this as a result:

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

I tried https://github.com/hipchat/hipchat-php and https://github.com/rcrowe/Hippy but they require an admin API token.

Thanks, IJC


Solution

  • You need to send the Content-Type header with your curl request just like you did on the command line, or the backend won't know how to parse your data.

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