0

我正在尝试使用 PHP 向 Yammer 发布消息。但是,我找不到所需的消息 JSON 格式。当我使用这段代码时

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

它说{"body":["Please include a message"]}

'body' 和 'message' 有什么不同?

4

4 回答 4

1

以下函数是一个示例,展示了如何将组 id 添加到 json 有效负载以发送到 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_ ;
  }
于 2014-05-24T16:57:13.397 回答
1

您收到错误是因为您没有指定有效负载是 JSON。您需要添加以下标头: Content-Type: application/json

于 2013-06-18T01:17:57.070 回答
0

看看这个示例 - 很容易实现 https://gist.github.com/aaronroe/3064754

于 2013-05-17T00:07:03.940 回答
0

我能够通过将主体包含在卷曲手柄本身中来解决这个问题。这对我有用

    $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);
于 2013-03-02T14:36:16.903 回答