Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.


eleitheleith New
edited April 2010 in Vanilla 2.0 - 2.8
i've got four categories in my forum. i'ld like an API where i can get the top 3-5 newest discussions in each category, and build a simple 'getsatisfaction' like feedback popup box for casual users to introduce them to the forums.

before diving into the code, wanted to see if someone's done something similar already, or if someone's got pointers of where i can start poking. i am imagining this is a pretty simple php script that will pump out json data for what i need, so shouldn't take long to put together.


  • I'm afraid I can't help at all with this, but I would certainly be interested in the result. Bookmarked.
  • here's my first stab at it:

    it's a very bare bones application, that really only meets a particular need at the moment (a get satisfaction like widget interfaced to a vanilla forum), but perhaps this could kick off something much more robust?

    in the end, i think if you need a particular API and can't wait for @todd or @mark to release a robust REST api, this application would be a good starting place. my approach was basically to look at /vanilla/controllers for the page i wanted to mimic with an API, cut out the cruft, and migrate it into the api controller that outputs json.

    hope this helps someone!
  • Being the code-free guy that I am, do you have a suggested method of using this, say in a Wordpress site/theme or something like that? Failing that, some suggested reading that would help? I know I could just google "JSON" and be done with it, but I'm guessing JSON is a pretty wide topic? (I should probably find out what it is first :D)
  • Interesting addon, I am just wondering how this can be adopted to display a specific discussion and all posts in that discussion perhaps?
  • @[-Stash-]

    install the application, and go to /api/category, and you'll see a list of all your categories, four discussion titles for each category, and even userid/key (if logged in). however, it will all be JSON encoded. this just means the information is inside an javascript object, such that an ajax call can turn this information into variables that can be used by javascript running on a webpage.

    i could have output the information in XML, text files, but since i was wiring it up to an ajax call, it made sense for me to use JSON.

    if you wanted to use it in a wordpress, you'ld need a wordpress plugin that could consume the JSON and do something with it (thus, it would have to be written by someone that knew about this application). unfortunately, that means you can't be code free.

    did you have something in mind already of how you wanted to use information like this?


    if you needed just that, i could modify the application. i could add the api to do something like this


    and that would show all the posts for that discussion. i'm happy to modify it if it was just that, but it would also be trivial to change the file controller/discussion api and add that api as well.

    let me know

    I'm not 100% certain yet, but those may be of assistance I reckon.

    Thanks for replying :)
  • oh i see. if you had a wordpress plugin that would convert json to html, then you could in theory just point it to the json API call, configure it a bit, and wire it up.

    seems like no matter what, you're going to have to get your hands dirty in code however, because you'll need somewhere in the chain to tell the templating system what the JSON looks like.

    if you knew a developer who has written even the most trivial wordpress plugins, you could just show them the API page, and tell them what you want, and i bet they could get you a working plugin in a couple of hours. that would be a lot faster than trying to find a mixmash of the right plugins to throw on top of each other and make this work.
  • I don't mind getting my hands dirty in code, I'm just very "L for learner" :D
  • in that case, i think it wouldn't take very much to get what you would want. if this API serves most of your need, just PM with some specific examples or requests, and i could write out some sample code that will help you get what you want.

    transforming the JSON this API outputs to html is pretty simple, and wouldn't need the libraries you've referenced (they're overkill, unless you have this need over and over again).
  • edited April 2010
    @eleith yeah i would greatly appreciate if you could make those modifications, would go along way in helping me with my integration.

    basically what I am looking to do is integrate vanilla2 into my website, where when I make an article there will be a discussion thread for the comments on that article. I want to integrated into the article page to display all the comments made on the article and have the option of adding a comment.
  • sure thing. want to give me more information about what you want in particular?

    for example, you want to pass in a discussion id and then get back posts on that discussion. what in particular do you need to know about the posts? text, title? username? etc etc.

    spec it out for me, and i'll post an update to the application when you do.
  • edited April 2010
    @eleith sure

    Basically what I envision is the article page will pass on the discussion id for the discussion thread which has been designated to this article. This will return all the posts so far in that discussion with username who posted the post, their avatar, time and date of post, if possible the number of post (comment #3, #4, etc), and ofcourse the text of the post.

    I would also want it to return somewhere a total number of posts in this discussion, if possible.

    At the end of the posts I envision the add comment box where if you're logged in you can add your own comment to the discussion thread, much like right here in vanilla2. Right now as I understand it, the API adds a new discussion thread, but I would want to add a post to an already existing thread that's being displayed.

    Hope this provides you some more information.
  • @GMAntonz

    updated the application. i haven't tested the comment/add API, but i tested all the others, and it should get you most of the way there. i think you'll have to figure out how to get the avatar working though, i didn't look into it, but all other data should be there just by inspection of the output.

    in order to add, you'll need the transient key of the current users session, so you'll need to call /api/session to get that. (i think you need to do this even if the users is logged out, but you might want to test this one if you want to support non logged in users)

    in general, i've modified the APIs so the output matches the internal class members used internally by vanilla to be more consistent.

    let me know if this gets you to where you need to build your comment module.
  • @eleith this is exactly what I was looking for! thank you! I haven't played around with adding the comments yet, but the displaying is working wonderfully. I can't thank you enough for this.
  • @eleith any thoughts on how I can call /api/session into php? doing file_get_contents seems to produce a "bonk" message. file_get_contents does work for comments and discussions, but not for the session.
  • api/session, returns the session of the current user. that means the requesting call must contain the session cookie for that user.

    my forum is integrated with vanilla through single sign on, so i make all server-side request to this API though a proxy i wrote, that allows me to make request while mimicking the user session for our service.

    you might be getting a bonk, because the API expects a user session, and there is none. i'll update it to return something more sane, instead of hitting a php error.

    in the end, you'll need to make a http request and pass in your services cookie, or a vanilla session cookie. i'm using python urllib/urllib2, in php you'll need a combination of file_get_contents and stream_context_create

    look at example #4:

    you'll need to pass the header of the user cookie. the http header looks something like this:

    "Cookie: key=value;; expires=GTM expire date; path=/;"

    the value needs to be urlencoded.

    this was a lot to parse, but i think once you get a few examples up and running you'll comprehend it pretty quickly.
  • @eleith, I don't suppose this could be used to feed a bunch of participating user names to the jQueryUI Autocomplete plugin, could it?

    There's already a list of users "In this Discussion" so it would be great to be able to use that list for autocompletion in the text area I reckon :)
  • Oh, and what are the chances of this JSON plugin being backported to V1? ;)
  • @[-Stash-]

    sure. the participating users of a forum are available in the api/discussion?id=x call. it just had to be parsed out and passed into the autocomplete plugin.

    as for backporting, this would depend on how much has changed for the application structure, and db related calls. i imagine the answer is no.

    i need single-sign-on, thus i'm only working with v2 currently.
  • @GMAntonz

    updated the application to push out a GUEST user transactionkey/userid instead of hitting a php error in api/session calls.
Sign In or Register to comment.