Hello, please sign in or register
You are here: Home

Add an Activities to Windows Live via PHP

Windows live allows one to post messages up on their "Activity Feed" which (and i'm forfeiting articulation points here) is similar to Facebook's Wall.

It looks a bit like this...

Demo

Then checkout your Activity Feed at http://profile.live.com/

Source

wl_activities.zip

Preface

The Activity Feed is a list of users actions around the web:  I Bookmarked, I Like, I Dislike, I Posted, I added Pictures, I Just Brought, I'm attending, I love my new Specialized Endro Mountain Bike its frickin slick as sick!... etc.. etc.

If you like waffles...

However writing my own actions (and probably losing more articulation points) When such actions can be recorded at their source, relieving me from microblogging or, dare I say, neglecting too. Isn't the best way.

So as developers we can automate this: As this is what the federated social web is all about. I'm even going to coin a new phrase "Not Web 2.1. Not 2 clicks for the point of 1".... Scrap that. It'll never catch on... i'm waffling worse that the CSS3 Song.

Background knowledge

In my previous articles i've covered the basics of Authenticating using OAUTH_WRAP (for friendfeed) and Implementing a proxy to communicate with the Windows Live REST'ful server. This page introduces the concept of activitystre.ms and uses some of the activity types listed above (for a more detailed explanation http://activitystrea.ms/schema/1.0/activity-schema-01.html  and http://msdn.microsoft.com/en-us/library/ff748963.aspx). This article goes into details about a server to server REST requests using JSON format (my favourite, although there are other formats such as XML/ATOM, which might be your preference)

What is an Activity Object?

This is an example of an activitystrea.ms JSON object which is posted to the server. This is an extract from the Bookmark demo. But there's a list of other types here http://msdn.microsoft.com/en-us/library/ff750702.aspx
{
"__type" : "AddBookmarkActivity:http://schemas.microsoft.com/ado/2007/08/dataservices",
"ActivityVerb" : "http://activitystrea.ms/schema/1.0/post",
"ApplicationLink" : "http://sandbox.knarly.com",
"ActivityObjects" : [
(
"Title" : "Bookmark example in PHP",
"Description" : "Share Bookmarks to the Windows Live Activity stream using PHP",
"TargetLink" : "http://sandbox.knarly.com/winconnect/bookmark.php",
"AlternateLink" : "http://sandbox.knarly.com/winconnect/bookmark.php",
"ActivityObjectType" : "http://activitystrea.ms/schema/1.0/bookmark"
)
]
)

1, 2, 3 Steps

There are three steps described in more details below. These are...
  1. Create the activity Object which will be sent to the server
  2. Get the path of the REST'ful location to post too.
  3. Post the data to the server

Step 1: Creating the Activity Object

To create this I've created a PHP fuction which takes a few parameters. And wraps the the activitystrea.ms namespace around. You can modify this as you see fit.
/**
* STEP 1
* CREATE THE activity object
* Build the Activity Template to Post
*/
$PHP_SELF = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$activityObject = array(
"Title" => "Bookmark example in PHP",
"Description" => "Share Bookmarks to the Windows Live Activity stream using PHP",
"TargetLink" => $PHP_SELF,
"AlternateLink" => $PHP_SELF
);

// Build a new activity object using the "bookmark" template
$activity = activityTemplate("bookmark", "http://" . $_SERVER['HTTP_HOST'], array((object)$activityObject) );

Below is a helper function activityTemplate which builds the wrapper for any type of activity objects: I.e. custom, article, picture, video, etc...

/**
* Create an Activity Template
* @param string $template: Name of the template we're creating. This must be a valid template defined by activitystrea.ms
* @param string $ApplicationLink: A complete URI to the application that runs this code
* @param object $ActivityObject: An Array containing the objects being posted.
*
* @return Object
*/
function activityTemplate($template, $ApplicationLink, array $ActivityObjects, $ActivityTarget = array() ){

// Sanitize
$template = strtolower($template);

$r = (object)array();
$r->__type = ucwords($template)."Activity:http://schemas.microsoft.com/ado/2007/08/dataservices";
if($template!=='custom'){
$r->ActivityVerb = "http://activitystrea.ms/schema/1.0/post";
$r->__type = "Add" . $r->__type;
}else{
$r->CustomActivityVerb = "http://activitystrea.ms/schema/1.0/custom";
}
$r->ApplicationLink = $ApplicationLink;

foreach($ActivityObjects as $k => $o){
$r->ActivityObjects[$k] = (object)$o;
$r->ActivityObjects[$k]->ActivityObjectType = "http://activitystrea.ms/schema/1.0/" . $template;
}

if(!empty($ActivityTarget)){
$r->ActivityTarget = (object)$ActivityTarget;
}

return $r;
}

Step 2: Finding the path to Post the Activity Object

Two requests are needed to find the path to MyActivties uri. The first uses the path we do know, and returns a JSON object containing the ActivitiesLink as one of properties, we query the Activities uri to find the MyActivities URI.  You can see this in action in the aforementioned demo.
/*
* STEP 2
* GET THE PATH TO POST THE ACTIVITY
*/

$json = request("http://apis.live.net/v4.0/");
$json = request($json->ActivitiesLink);

$activityLink = $json->BaseUri . $json->MyActivitiesLink;

The helper function request is shown below.
/**
* Make an HTTP request for JSON data
* Prerequisites: the $_SESSION['wrap_access_token'] must be defined at point of authentication.
* - See token.php where the acess token is captured.
*
 * @param string $path (Required) A URI
* @param object $post (optional) A Data Object that will be JSON encoded.
*
* @return Object
*/

function request($path, $post = false){

$post = (!empty($post)?json_encode($post):false);

$headers = array_filter(array(
'Content-Type: application/json',
'Authorization: WRAP access_token=' . $_SESSION['wrap_access_token'],
'Accept: application/json',
($post ? "Content-Length: ".strlen($post) : "")
));

$cURL = curl_init();
curl_setopt_array($cURL, array(
CURLOPT_URL => $path,
// CURLOPT_PROXY => "127.0.0.1:8888",
// CURLOPT_PORT => 443,
CURLOPT_HEADER => 0,
CURLOPT_VERBOSE => false,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true ));

if($post){
curl_setopt_array($cURL, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post
));
}

// Get the contents from the stream
$content = curl_exec($cURL);
curl_close($cURL);

return json_decode($content);
}

Step 3: Posting the activity Object


Take the Activty object created in step one and posting it to the Activity path discovered by step 2, and recycling the request function from step 2.
/*
* STEP 3
* Post $activity from step 2 to the $activityLink from step 1
*/
$resp = request($activityLink, $activity );

The successful response is the object which was created including new managment properties such as the UID of the activity.

Thirsty for more?

Then you may like to puruse...

Comments

cmoutlet
s ralph lauren outlet Ray Ban Sunglasses,Ray Ban Outlet,Ray Ban Sale,Cheap Ray Bans,Cheap Ray Ban Sunglasses,ray ban sunglasses outlet,ray ban,rayb...
Created 21/10/15
oakleysunglasses
“I canada goose outlet think it doesn’t swarovski jewelry bode very
Created 21/01/16
linpingping
The michael--kors.org.uk next ralph lauren outlet day patriots jersey Mrs
Created 01/03/16
clibin
Tyrion 5c cases watched her ray ban sunglasses read. His timber...
Created 18/04/16
chenyan
Bond grinned. pandora-bracciali.it "We bcbgmax.in.net only chaussure...
Created 15/06/16
chenyan
Bond grinned. pandora-bracciali.it "We bcbgmax.in.net only chaussure...
Created 15/06/16
chenyan
Bond grinned. pandora-bracciali.it "We bcbgmax.in.net only chaussure...
Created 15/06/16
chenyan
Bond grinned. pandora-bracciali.it "We bcbgmax.in.net only chaussure...
Created 15/06/16
ninestab123
ninest123 One canada goose pas cher gucci outlet thing
Created 21/07/16
ZZZZ
michael kors handbags nike trainers
Created 05/10/16
cc
discount oakley sunglasses retro jordans
Created 26/10/16
wlp
The oakley sunglasses cheap wreck, cheap nfl jerseys even replic...
Created 14/11/16
LEE2017
, Nike Shoes Online This became Air Jordans Shoes prefer from
Created 07/04/17
Title*
Comment

Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Contact
Name*
Email never shown*
Home Page

Author

Andrew Dodson
Since:Feb 2007

Comment | flag

Categories

Bookmark and Share