Yammer - 如何通过api发布到活动流? (PHP)

时间:2012-05-15 20:48:50

标签: php curl yammer

我可以“获取”活动流但不能“POST”到它。这似乎是一个技术错误。 这是用于获取活动流的工作的代码:

function getActivityStream()
{
    $as=$this->request('https://www.yammer.com/api/v1/streams/activities.json');
    var_dump($as);
}

function request($url, $data = array())
{
    if (empty($this->oatoken)) $this->getAccessToken();
    $headers = array();
    $headers[] = "Authorization: Bearer " . $this->oatoken['token'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($data) );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    return json_decode($output);
}

好的,这样可以正常工作...... 下面的代码返回Yammer “oops此页面无法找到”消息:

function putActivityStream()
{
    $data=array('type'=>'text', 'text'=>'hello from api test call');
    $json=json_encode($data);
    $res=$this->post('streams/activites.json',$json);
}

function post($resource, $data)
{
    if (empty($this->oatoken)) $this->getAccessToken();
    $ch = curl_init();
    $headers = array();
    $headers[] = "Authorization: Bearer " . $this->oatoken['token'];
    $headers[]='Content-Type: application/json';
    $url = 'https://www.yammer.com/api/v1/' . $resource;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    return $response;
}

其中一个例子来自: http://developer.yammer.com/api/streams.html

POST https://www.yammer.com/api/v1/streams/activities.json
Requests must be content-type: application/json.

{
  "type": "text",
  "text": "The build is broken."
}

1 个答案:

答案 0 :(得分:3)

你要踢自己。您的代码中有拼写错误。 :) 变化:

$res=$this->post('streams/activites.json',$json);

$res=$this->post('streams/activities.json',$json);

Simples。