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.

Resources

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

functionget_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.

// 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');

}elsereturn$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.

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.