【问题标题】:yammer message json formatyammer 消息 json 格式
【发布时间】:2013-02-21 20:06:12
【问题描述】:

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

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

上面写着{"body":["Please include a message"]}

“正文”与“消息”不同吗?

【问题讨论】:

    标签: php message yammer


    【解决方案1】:

    您收到错误是因为您没有指定有效负载是 JSON。 您需要添加以下标题: 内容类型:application/json

    【讨论】:

      【解决方案2】:

      以下函数是一个示例,展示了如何将组 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_ ;
        }
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

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

          【讨论】:

          • 是的,正如您所说,这很简单,但是如果您想发布带有缩略图的视频怎么办。你能显示“body”属性吗?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多