Mango API - How to Create a Post

Posted on May 28, 2010 by: Justin Scott 5 Comments

I've been using Mango Blog on and off for quite a while, but recently I've really began to dig my heels in and use it as a platform to base entire websites on.  Part of the magic of Mango Blog is its extensibility.  Laura has done a fabulous job of providing hooks into the core engine.  My goal is to write code around it that does what I want but never actually modifies the core engine.  This is easier said than done, but Mango makes that goal a lot easier to reach.  Now, one of the things that comes up often is creating new posts automatically.  They could be coming from an RSS feed, another content management system, or user submissions.  The first thing that springs to mind would be to write the data directly to the database.  That, however, would be a Very Bad Thing(tm).  Mango may change the database structure with a new release, or move to some other means of storing posts entirely.  That would also bypass the event announcements that plugins are looking for.  That would ruin your whole day!  So what are we to do?

Enter the API.  Mango provides a pretty decent set of APIs into the core functionality.  One of these is the ability to create a new post through code in a way that lets Mango handle the data storage and preserves the event announcements and all the other "work" that goes on in the background.  The API says, "Give me the data and I'll take care of the dirty work for you."  How do we use the API?  It's simple, really.  Here's an example:



<cfscript>
// Get the API
mangoAPI = createObject("component", "api.api");
// Set up the post attributes.
myPost = structNew();
myPost.username = "admin-username";
myPost.password = "admin-password";
myPost.title = "API Post Title";
myPost.content = "This is a post through the API";
myPost.publish = true;
myPost.blogID = "";
// Call the API.  Returns the post ID or an error string.
postID = mangoAPI.newPost(argumentcollection=myPost);
</cfscript>

Wasn't that easy?  The username and password would be the login credentials of the admin user you want to "own" the post.  The publish setting determines whether the post will be published on the site or put into "draft" mode.  The blogID is ignored as of this writing (perhaps it's reserved for a future release where Mango supports multiple blogs on the same install).  The API will return either a UUID which is the new post ID, or an error string letting you know what isn't right.  Hopefully this will help anyone who's trying to automate their Mango a bit.  Cheers!

5 responses to “Mango API - How to Create a Post”

  1. Thanks for this - I was unable to find much documentation for the Mango API. I'm considering using the Mango back-end for a project. Being able to make use of Mango through the API will be a great help.

    James

    James ~ Jun 17, 2010 at 11:02 AM

  2. You're welcome. I've been using Mango more extensively lately and have found that the API documentation available is pretty slim at best. As I dig deeper and continue working with the platform I plan to post some additional examples of common tasks to take some of the mystery out of it.

    Justin Scott

    Justin Scott ~ Jun 17, 2010 at 11:23 AM

  3. Thanks for the post. Mango API seems very cool. I am going to dig a little deeper into this.

    John Gag

    John Gag ~ Jul 27, 2010 at 5:02 PM

  4. Thanks for the API post. I am having this issue:-

    I have 2 mango blogs, such-
    1. applications/myBlogA/
    2. applications/myBlogB/

    From myBlogA I am trying to post to myBlogB (author and categories exist on both), using API.
    <cfscript>
                   myBlogBAPI = createObject("component", "applications.myBlogB.api.API");
                   postID = myBlogBAPI.newPost(argumentCollection=data);
                   
                </cfscript>

    But it keeps posting Blog A- even though in the code above you can clearly see I am posting to Blog B. So everytime- i enter a post in admin section to Blog A I have 2 blog post of same.

    Any pointers, thoughts, ideas- as to what I am doing wrong here.

    Varun Dixit

    Varun Dixit ~ Aug 2, 2010 at 4:59 AM

  5. Unfortunately I don't have any experience with multi-blog installations to shed light on the issue. You might try posting a question in the Mango Blog forums as more people will be available there to help.

    Justin Scott

    Justin Scott ~ Aug 4, 2010 at 11:13 AM

Leave this field empty: