Find Posts for a Forrst User:


Enter Username:
'; if(!$_POST['username']){ $u = get_forrst_posts('crassiusneo'); $_POST['username']='crassiusneo'; $thepage.='
Hi, This script will grab the public posts for a Forrst User.

It uses the API V2 that has this basic functionality available now.

Due to requests for using Curl for all of it I included API V2 usage for grabbing the user info as well which replaces Tutorial 1 which did not use the Curl functions.

Below are the Posts grabbed from my own Posts page, but type in a Username above and you can grab theirs too.
'; }else{ $u = get_forrst_posts($_POST['username']); } if(isset($u['error']) || $u==''){ if($u=='') $u['error']='Not Found.'; $thepage.='

Your request for data about username: '.$_POST['username'].' failed. Reason: '.$u['error'].'

'; }else{ if(count($u)>0){ $posts.='

Posts for '.$_POST['username'].'.

'; foreach($u as $p){ $posts.='

'.$p['posted'].'

'.$p['title'].'

'.$p['subtitle'].'
'.$p['description'].'

'; } $posts.='

You can check out the Forrst profile at:
http://forrst.me/'.$_POST['username'].'.
You can check view the Forrst Posts page at:
http://forrst.me/'.$_POST['username'].'/posts.

'; } } $thepage.=$posts.'
Click here to View the source
'; echo $thepage; //The functions below can easily be placed into a separate library file thus reducing the size of your main page. //For the purpose of the tutorial it's all kept in one file. 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?''.$post->url.'':''); $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; } } function time_ago($timestamp){ //Function by Paul Fraser on Forrst: //url: http://forrst.com/posts/PHP_time_ago_class-Hz //Slight modifications to display the time ago the way forrst.com posts do. // - Made it show the "s" only if the number is greater than 1 (just being picky) // - Added $weeks_ago // - Made it count up one week or month if the number of days was past the halfway point for the next week/month. $difference = time() - $timestamp; $difference = ($difference < 0)? 0 : $difference; $months_ago = floor($difference/60/60/24/30); $weeks_ago = floor($difference/60/60/24/7); $days_ago = floor($difference/60/60/24); $hours_ago = floor(($difference - $days_left*60*60*24)/60/60); $minutes_ago = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60); if($minutes_ago < 1 && $hours_ago < 1 && $days_ago < 1){ $time_ago = "under a minute ago"; }elseif($minutes_ago < 60 && $hours_ago < 1 && $days_ago < 1){ $time_ago = "$minutes_ago minute".($minutes_ago>1?"s":"")." ago"; }elseif($minutes_ago >= 60 && $hours_ago < 24 && $days_ago < 1){ $time_ago = "$hours_ago hour".($hours_ago>1?"s":"")." ago"; }elseif($months_ago < 1 && $weeks_ago < 1 && $hours_ago >= 24){ //$time_ago = date('D jS M Y', $timestamp); $time_ago = "$days_ago day".($days_ago>1?"s":"")." ago"; }elseif($days_ago < 30){ //$time_ago = date('D jS M Y', $timestamp); if($days_ago%7>3) $weeks_ago++; $time_ago = "$weeks_ago week".($weeks_ago>1?"s":"")." ago"; }elseif($days_ago >= 30){ //$time_ago = date('D jS M Y', $timestamp); if($days_ago%30>15) $months_ago++; $time_ago = "$months_ago month".($months_ago>1?"s":"")." ago"; } return "posted ".$time_ago; } ?>