timh
timh

Reputation: 1590

Yammer - How to post via api to activity stream? (php)

I am able to "GET" the activity stream but not "POST" to it. It seems its a technical error. This is the code which works for getting the activity stream:

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

ok so that works fine... the code below, returns a Yammer "oops this page could not be found" message:

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;
}

One of the examples from: 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."
}

Upvotes: 2

Views: 2412

Answers (1)

Toby Beresford
Toby Beresford

Reputation: 1038

You're going to kick yourself. You have a typo in your code. :) Change:

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

to

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

Simples.

Upvotes: 3

Related Questions