Вопрос

I am trying to post a message to Yammer using PHP. However, I am unable to find the message JSON format required. When i use this piece of code

$data=array('body'=>'test from API','group_id'=>'xyz');
$json=json_encode($data);

it says {"body":["Please include a message"]}

is 'body' different from 'message'?

Это было полезно?

Решение 3

I was able to solve this by including the body in the curl handle itself. This worked for me

    $curl_handle2="https://www.yammer.com/api/v1/messages.json?access_token=<<accesstoken>>&body=TestingfromAPI&group_id=<<groupid>>";
    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_URL, $curl_handle2);
    curl_setopt($ch2, CURLOPT_POST, true);
    $response2 = curl_exec($ch2);

Другие советы

You are receiving the error because you did not specify that the payload is JSON. You need to add the following header: Content-Type: application/json

The following function is an example showing how to add a group id to the json payload to send to yammer.

function postMessage($token_, $message_, $group_id_) {

          $result_ = array() ;
          $result_['token'] = $token_ ;
          $result_['message'] = $message_ ;
          $associativeArray = true ;

          $payloadArray_ = array() ;
          $payloadArray_['body'] = $message_ ;
          if ( $group_id_ > 0 ) {
                  $payloadArray_['group_id'] = $group_id_ ;
          }
          try {
                  $curl_ = curl_init() ;
                  $headers = array() ;
                  $headers[] = "Authorization: Bearer " . $token_ ;
                  $headers[]='Content-Type: application/json' ;
                  $url = 'https://www.yammer.com/api/v1/messages.json' ;
                  curl_setopt($curl_, CURLOPT_URL, $url);
                  curl_setopt($curl_, CURLOPT_POST, true);
                  curl_setopt($curl_, CURLOPT_POSTFIELDS, json_encode( $payloadArray_));
                  curl_setopt($curl_, CURLOPT_RETURNTRANSFER, 1);
                  curl_setopt($curl_, CURLOPT_HTTPHEADER, $headers);
                  $response = curl_exec($curl_);
                  $result_['response'] = json_decode($response,$associativeArray) ;
          } catch ( Exception $exception ) {
                  error_log ( $exception->getMessage() ) ;
                  $result_['exception'] = $exception->getMessage() ;
          }
          return $result_ ;
  }

Just look at this sample - very easy to implement https://gist.github.com/aaronroe/3064754

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top