Monday, April 4, 2011

Yammer API Example

yammer api php example
Yammer is an enterprise social network service that was launched in September 2008.Unlike Twitter, which is used for broadcasting messages to the public, Yammer is used for private communication within organizations or between organizational members and pre-designated groups, making it an example of enterprise social software. Yammer originally launched as an enterprise micro blogging service and has evolved to become a fully-fledged enterprise social network.

Yammer Offer its API to create personalized yammer applications for mobile phones and desktop, to in this tutorial we will make and application, authorize it and access data from user profile. To access data from yammer api one passed from following steps
  1. Register your Application on Yammer and get Application Secret and Application Key.
  2. Then get Token and Token secret from above data.
  3. Then Authorize your application using the token and get the Auth Code.
  4. Then Auth Code, token and token get the access token and access token secret.
  5. Finally get Data from the user profile.
And now get all the obvious stuff out of the way and let the code talk :) i made a small application demonstrating said steps.

yammer php api demo
Click on Image for full size

and the code...
<?php
error_reporting(0);

require_once 'common.inc.php';
require_once 'Yammer.php';
$appKey="YOUR APP KEY HERE";
$appSec="YOUR APP SECRET HERE";
$token="";
$tokenSec="";
$authcode="";
$accessToken="";
$accessTokenSec="";

$b=false;
if(isset ($_POST["isSubmit"])){

    $appKey=$_POST["appkey"]==""?"YOUR APP KEY HERE":$_POST["appkey"];
    $appSec=$_POST["appsec"]==""?"YOUR APP SECRET HERE":$_POST["appsec"];
    $token=$_POST["token"];
    $tokenSec=$_POST["tokensec"];
    $authcode=$_POST["authcode"];
    $accessToken=$_POST["accessToken"];
    $accessTokenSec=$_POST["accessTokenSec"];

    $test_consumer = new OAuthConsumer($appKey, $appSec, NULL);

    if(isset ($_POST["reqtoken"])){
        
        $endpoint="https://www.yammer.com/oauth/request_token";
        
        $req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $endpoint, null);
        $req_req->sign_request($plaintext_method , $test_consumer, NULL);

        $data=getContents($req_req->to_url());

        $data=  explode("&", $data);
        $token= str_replace("oauth_token=", "", $data[0]);
        $tokenSec= str_replace("oauth_token_secret=", "", $data[1]);
        $b=true;
    }
    if(isset ($_POST["acctoken"])){
        
        $endpoint="https://www.yammer.com/oauth/access_token";
        $param=Array('oauth_verifier'=>"$authcode");

        $test_token = new OAuthConsumer($token, $tokenSec);

        $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, "GET", $endpoint, $param);
        $acc_req->sign_request($plaintext_method, $test_consumer, $test_token);

        $data=getContents($acc_req->to_url());
        $data=  explode("&", $data);
        $accessToken= str_replace("oauth_token=", "", $data[0]);
        $accessTokenSec= str_replace("oauth_token_secret=", "", $data[1]);

    }
    if(isset ($_POST["getData"])){
        $obj=new Arc90_Service_Yammer($appKey, $appSec , $accessToken, $accessTokenSec, 10);
        $json=json_decode($obj->getMessagesAll());
        $k =count($json->messages);
        echo "

Messages Retrived via API

    "; for( $i=0;$i<$k;$i++){ echo "
  1. ".$json->messages[$i]->body->plain."
  2. "; } echo "
"; } } function getContents($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); echo curl_error($ch); curl_close($ch); return $data; } ?>
and the html code ...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
input{
 font-size:24px;
 color:#666;
}
body {
font-size:20px;
font-family: Verdana,Arial,Helvetica,sans-serif;

}
.style7 {
 color: #990033;
 font-weight: bold;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Yammer OAuth & API Test -  Originative Systems</title>
</head>

<body>
    <form action="" method="post">
<table width="100%" border="1" style="border-collapse:collapse; border:solid 2px outset" cellpadding="8" cellspacing="5">
  <tr>
    <td colspan="5"><span class="style7">Yammer OAuth & API Test</span> </td>
  </tr>
  <tr>
    <td width="25%"><div align="right">Application Key : </div></td>
    <td width="75%" colspan="4"><label>
      <input name="appkey" type="text" value="<?php echo $appKey; ?>" id="appkey" size="40" />
    </label></td>
  </tr>
  <tr>
    <td><div align="right">Application Secrect : </div></td>
    <td colspan="4"><label>
      <input name="appsec" type="text" value="<?php echo $appSec; ?>" id="appsec" size="40" />
    </label></td>
  </tr>
  <tr>
    <td><div align="right">Token : </div></td>
    <td colspan="4"><label>
      <input name="token" type="text" value="<?php echo $token; ?>" id="token" size="40" />
    </label></td>
  </tr>
  <tr>
    <td><div align="right">Token Secrect : </div></td>
    <td colspan="4"><label>
      <input name="tokensec" type="text" value="<?php echo $tokenSec; ?>" id="tokensec" size="40" />
      </label>
      <?php if($b){
         echo "<INPUT type='button' value='Authorize Request' onClick=\"window.open('https://www.yammer.com/oauth/authorize?oauth_token=$token','Authorize Yammer Request','width=500,height=500')\"> ";
      }
      ?>
    </td>
  </tr>
  <tr>
    <td><div align="right">Auth Code : </div></td>
    <td colspan="3"><label>
      <input name="authcode" type="text" value="<?php echo $authcode; ?>" id="token" size="4"  />
    </label></td>
  </tr>
  <tr>
    <td><div align="right">Access Token : </div></td>
    <td colspan="4"><label>
      <input name="accessToken" type="text" value="<?php echo $accessToken; ?>" id="token" size="40" />
    </label></td>
  </tr>
  <tr>
    <td><div align="right">Access Token Secret : </div></td>
    <td colspan="4"><label>
      <input name="accessTokenSec" type="text" value="<?php echo $accessTokenSec; ?>" id="token" size="40" />
    </label></td>
  </tr>
  <tr>
    <td> </td>
    <td align="center"><label>
      <input name="reqtoken" type="submit" id="reqtoken" value="Request Token" />
    </label>
    </td>
    <td align="center"><label>
      <input name="acctoken" type="submit" id="acctoken" value="Access Token" />
    </label></td>
    <td align="center"><label>
      <input name="getData" type="submit" id="acctoken" value="Get Data" />
    </label></td>
  </tr>
</table>
        <input type="hidden" name="isSubmit" value=""/>
    </form>
</body>
</html>
just run it and you will understand it, there nothing left to explain after source code, download netbeans project with all the files used and enjoy :)

download yammer api
Download Yammer API Example

9 comments:

Romke de Haan on August 6, 2011 at 6:50 AM said...

I am getting an error when I try and request an Access Token. Looks like some XML is trying to be processed but it broken.

Help!

Anonymous said...

it is working for me... did you set values at line 6,7,17 and 18 ?

Anonymous said...

The download doesn't work any longer.

Originative on March 10, 2012 at 4:32 AM said...

it is on a free host, may when you try they are not available, it is available now...

Unknown on November 14, 2012 at 3:01 AM said...

Hi,

i have updated Lines 6,7,17 & 18 with my Client ID and Client Secret (i believe these terms have replaced Application Key and Application Secret prehaps?)

when i run the project i just get a blank on http://localhost/yammer_test/index.php

Also the HTML Example looks broken
the Application Key & Application Secret contain the following text ""

The others also contain text i.e ""

Unknown on November 14, 2012 at 3:04 AM said...

HTML removed the text i entered for the fields """"

Originative on November 14, 2012 at 6:43 AM said...

OAuth part is working fine. i am able to get Access Token and Access Token Secret but Get Data part is not working as expected because Yammer had updated its API.

Unknown on November 22, 2012 at 4:23 AM said...

I know it's crazy but... where can it be downloaded current Yammer API?
I'm getting lost everytime i div in their site looking for it...
Could someone help me, please!?

sunset on January 24, 2019 at 9:04 AM said...

html table samples for beginner webmasters
Basic XHTML table

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems