Subscribe Via RSS

27895 Subscribers

Subscribe Via RSS
Subscribe Via Twitter

Followers

Subscribe Via Twitter
Forrst API Tutorial — Pt II

April 12th, 2011 in Coding Tutorials by Ramsez Stamper

Forrst API Tutorial — Pt II

1 Star2 Stars3 Stars4 Stars5 Stars1 Votes, Rating: 5.00
Loading ... Loading ...

This is a follow up tutorial to the Forrst User API Tutorial – Pt. 1 and will teach you how to grab the posts for a forrst user.
As of April 2011 this still works, but may change as their API is still being developed and in this case we are scraping the user’s profile page to generate some useful arrays that we then place into a custom template.
We will use the API V2, which returns the data in JSON format, which we then use json_decode on. In case the API fails or your server does not have json support setup for PHP, we have a backup Simple HTML DOM script by S.C. Chen to convert the user’s Posts page into a data object from which we then grab the individual posts. This DOM script only grabs public posts as it gets only displayed posts on the public profile.

You can use this to generate your own custom profile pages with Forrst data, and it’s easy to combine with the previous user data tutorial.

Instead of using file_get_contents we use Curl to grab the data in this one and that can easily be transplanted into the previous tutorial as well.



Clicking the above image will take you to a demo using the code explained below.

Resources

Active Usernames from Forrst by forsst.com

Simple HTML Dom Script by S.C. Chen

How to Grab Forrst User Posts

At this time the Forrst API does not have a search call for filtering posts, so this script will let you grab the posts for a specific user via their username to display as you see fit.

For the demo I started it with my own, but you can type in any other valid username and see their info as well.

In order to grab the posts data we are using the API V2 which returns the data in JSON format. In case of failuer we fall back to the Simple HTML Dom script which turns the html into an object set. The DOM script is not as complete as the API script but provides enough information for a basic content display.

Issues with the DOM version:
- Actual full post URLs are not links we can grab from the posts list, so we cannot reach those pages without knowing the URLs by manually grabbing them.
- It grabs less fields of data since we are stripping the posts page, so it’s limited opposed to the API V2 version.
- Does not return half the fields the API does. They are explained in the PHP code below.

function get_forrst_posts($username){
	//USE JSON DECODE (Note: Your server must have the json module installed for PHP, or another class emulating it for this to work.
	//Note: I did not use a Class for this as I wanted to embed the DOM solution too and found this was not something you would do in a Class definition.
	$curl = curl_init('http://api.forrst.com/api/v2/users/posts?username='.$username);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	$response = curl_exec($curl);
	$response = @json_decode($response); //Disable error display with "@" so the DOM method is attempted if JSON fails.

	curl_close($curl);
	if($response->stat!='ok'){
		$usedom=1; // If JSON does not return "ok" (This means, the API failed, or the API call returned fail, or json_decode does not work on this server. So we try the DOM method anyway.
	}else{
		foreach($response->resp as $post){
			//Note: do a print_r on $response to see all the variables. It does also return all the user info, but I find that should not be in the "Posts" call as it's already in the "User" call.
			//To get time ago, take created_at value, break it into pieces, use mktime to generate a unix timestamp from it, pass that to the time_ago function.
			$t1 = explode(' ',trim($post->created_at));
			$d1 = explode('-',$t1[0]);
			$t2 = explode(':',$t1[1]);
			$timestamp=mktime($t2[0],$t2[1],$t2[2],$d1[1],$d1[2],$d1[0]);
			$p['posted'] = time_ago($timestamp);

		    $p['title'] = $post->title;
		    $p['subtitle'] = ($post->url?'<a href="'.$post->url.'">'.$post->url.'</a>':'');
		    $p['description'] = $post->description;
		    $p['content'] = $post->content; // Code and Questions have the content section that essentially gets added into the full article on display.

		    $p['likes'] = $post->like_count;
		    $p['comments'] = $post->comment_count;

		    //The following fields only come from the API and are not available for the DOM solution.
		    $p['created'] = $post->created_at; //instead of days since post (like the DOM request, this returns the time of the post.
		    $p['post_url'] = $post->post_url; // URL to detailed post view on forrst. Not available in DOM result.
		    $p['type'] = $post->post_type; //Determines what var is the article description.
		    $p['published'] = $post->published; //Status var showing if the post is currently published or not. The DOM only sees published posts.
		    $p['public'] = $post->public; //Status var showing if the post is public or not. The DOM only sees public posts.
		    $p['post_url'] = $post->post_url; //Used for the subtitle.
		    $p['formatted_description'] = $post->formatted_description; //HTML version of the description
		    $p['formatted_content'] = $post->formatted_content; //HTML version of the description

		    $ret[] = $p;
		}

		if(count($ret)==0){
	    	return $ret = array('error'=>'No Posts Found');
	    }else return $ret;
	}

	if($usedom){
		// USING THE SIMPLE HTML DOM SCRIPT
		// As you can see above this does not return nearly as much information as the API and should not be used if you have json enabled.
		include_once('simple_html_dom.php');
	    $html = file_get_html("http://forrst.me/".$username."/posts"); //Generate the HTML DOM object.

	    // THIS IS COMPLETELY BASED ON THE CURRENT HTML MARKUP FOR THE POSTS PAGE.
	    // IF THE FORRST MARKUP CHANGES, THIS MUST BE UPDATED TOO.
	    if(count($html->find('div.Post')>0)){
		    foreach($html->find('div.Post') as $post) {
		        $p['posted'] = trim($post->find('div.activity-meta', 0)->plaintext);
		        $p['title'] = trim($post->find('.title', 0)->plaintext);
		        $p['subtitle'] = trim($post->find('.subtitle', 0)->plaintext);
		        $p['description'] = trim($post->find('.description', 0)->plaintext);
		        $p['likes'] = trim($post->find('.likes', 0)->plaintext);
		        $p['comments'] = trim($post->find('.comments', 0)->plaintext);
		        $ret[] = $p;
		    }
	    }else $ret='';
	    $html->clear(); // Clears memory.
	    unset($html);

	    if(count($ret)==0){
	    	return $ret = array('error'=>'No Posts Found');
	    }else return $ret;
	}
}

The function is written to generate a simple array of data. In the demo we only use the fields shared by the API call and the DOM script, but using the API you can generate some additional display data.
By looping through the array you can then generate a display with the data.

The Data Returned:

When the html data is converted into an array, the fields used for our basic display contain the following information:

  • $p['posted'] : How long ago the Post was posted
  • $p['title'] : The Username.
  • $p['subtitle'] : The Name.
  • $p['description'] : The Profile URL for the User.
  • $p['likes'] : The number of Likes for the post.
  • $p['comments'] : The number of comments for the post.

Post Display Manipulation

To display the data in the demo I used some basic html to just output the data to the screen, basic usage of some of the returned user data.
The great thing is that once you have the data array, you can essentially do anything you want with it.
Below follows a display example of the results.

<div id="wrapper">
<div id="result" class="with_bg">
		<span class="posted">posted 4 weeks ago</span>
<h1 class="sh">Freelancer Demotivational Posters</h1>
		<span class="subtitle">http://freelanceswitch.com/humour/de-motivational-posters-for-freelancers/</span>

		Enjoy these posters, a pretty funny find this morning.</div>
</div>
html, body{ width:100%; height:100%; padding:0; margin:0; font-size:12px; font-family: arial, helvetica; background:#162810;}
#wrapper{width:840px; margin:0 auto; padding:20px; background:#EBE9DB; }
.with_bg{ padding:10px; background:#F7F7F7;}
form {width:400px; margin:0 auto; }
h1{ color:#3B3227; }
#result{ width:660px; margin:20px auto; }
.posted{float:right; font-size:11px; color:#999;}
.sh{width:400px;}
.subtitle{color:#333; }

Conclusion

This script will help you grab some Forrst data and create a quick basic profile page with basic post info.

As the API develops we may eventually write a formal API class, though I’m certain the Forrst staff are already working on that.
At this time, I’m hoping this tutorial helps you get started with your Forrst apps/pages.

The full PHP/HTML/CSS source is available here: http://webnuz.com/tutorials/04-03-2011_Forrst_API_Grab_User_Posts/source.txt

You will also need the Simple HTML Dom script, which you can grab here: http://webnuz.com/tutorials/04-03-2011_Forrst_API_Grab_User_Posts/simple_html_dom.txt

About The Author

About The Author: Ramsez Stamper

Ramsez Stamper is a web developer with a focus on frontend development, but an eye on backend as well. Specializing in HTML/CSS/JS/PHP/MySQL websites and applications and doing so since 2004.

 

Ramsez Stamper has written 2 posts.

Be Part Of The Community!

Become part of the hv-designs community.

Subscribe Via RSS or Follow Us On Twitter.

Subscribe Via RSS Follow Us On Twitter

2 Responses to “Forrst API Tutorial — Pt II”

  1. Do you have an email that I could use to contact you?

  2. Aaron says:

    You can email our lead editor at [email protected]

Leave a Reply