HOWTO: Call Posterous from a Logic Hook

sugarcrmdevelopers —  January 10, 2011 — Leave a comment

This code will be called by a logic hook so that the note is captured to the Posterous service. Posterous is a neat way to post to multiple services such as Twitter and Facebook and it also stores posts independantly of any service.

Extending the following code to use custom Posterous tags and turning on auto-posting could yield some interesting use cases for customer-facing notes which could be sent out to various social networks, in turn…

            CURLOPT_RETURNTRANSFER => true,


        $ret = curl_exec($ch);

        $api_tok = json_decode($ret)->api_token;


        $ch = curl_init($root_url . '/users/me/sites/primary/posts');

        $post_data = array(
            'post[title]' => $subject,
            'post[body]' => $body,
            'post[tags]' => 'sugar',
            'post[autopost]' => 0,
            'api_token' => $api_tok,

        $headers = array(
            'Content_length: 0',

        $options = array(
            CURLOPT_USERPWD => $myuser.':'.$mysecret,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $post_data,
            CURLOPT_HTTPHEADER => $headers,


        $ret = curl_exec($ch);

        if (!$ret) {
            die('There was a posterous error...');


    public function doPost(&$bean, $event, $arguments)
        $this->doWork($bean->modified_by_name . ' posted ' . $bean->name . ' in ' . $bean->parent_type . '...', $bean->description);

The goal of the above code is to post every single note entered into a Sugar instance onto the service via its V2 API. It does this by being triggered on an afterSave logic hook in the Notes module. While this may not have a use case in and of itself, adding additional filtering in which certain notes get certain tags, etc, may well be useful. This is merely a proof-of-concept.

The code:

The notePoster class is referred from the logic hook, as is the doPost() function. I made doPost a separate function so that the actual “work” is done by doWork() and filters can be applied by doPost (i.e. is this note attached to a case? Should we proceed? How should we format the post?).

The doWork() function takes a subject and a body and posts them to the referenced Posterous account. $root_url is essentially a constant and is the fundamental endpoint of the Posterous service, for brevity. $myuser and $mysecret are username and password, respectively.

The “/auth/token” API function gets an API token for a given username and password (which are, in turn, used for HTTP authentication) The token is stored in the $api_tok variable.

Posterous only supports JSON returns at this time, so the resultant decoded object has a property called “api_token,” which we actually want to use. Note that I should have done better checking for authentication failures at this point.

“/users/me/sites/primary/posts” accepts, among others, GET and POST verbs. In this case we’re using POST (“CURLOPT_POST”) so as to create a new post. me is a macro for “whoever is logged in” and primary is my primary posterous site (another “macro…”).

Some other things worth mentioning are that CURLOPT_RETURNTRANSFER should always be true when we’re expecting returned data. Also post[title] and similar arguments are sort of “quasi-arrays” and are written literally as shown. Content-length does indeed need to have a value or the associated call will not work.

No Comments

Be the first to start the conversation!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s