Integrating Buffer- The Social Networking App

What is Buffer?

Buffer makes your life easier with a smarter way to schedule the great content you find. Fill up your Buffer at one time in the day and Buffer automatically posts them for you through the day. Simply keep that Buffer topped up to have a consistent social media presence all day round, all week long.

Buffer is a social networking app that lets you automatically (well sort of) post updates to your social profiles. The reason that I am saying that its not totally automatic is because you can only have updates posted for the current day and not lets say a week (or month) from now. This article will help you to post updates to whatever to day you want using Buffer’s API

Getting started

Buffer sign up page

Create an app

Once your signed up, you will need to create an app by going to http://bufferapp.com/developers/apps/create. Enter in all the required fields. Once you’re done, click “Create Application” and you should be redirected to a page with an “access token” which you will need to record for future use. You will also receive the details to your email id

Buffer create app page

Connect Social Profiles

Buffer app details received via email

Connect your social profiles like FB, twitter, LinkedIn to your buffer account, we will need these social profile’s id on order to post to these profiles. You can find out each profile’s id from your dashboard, click on each social profile tab and notice the URL in browser, it will be something like this:


https://bufferapp.com/app/profile/SOCIALPROFILEID/buffer

The string between “/profile/” & “/buffer/” is the social profile id, notedown these profile ids as we will need them in script that posts updates to these social profiles

I am using class.bufferapp.php class that I found on blog little app blog (link below), I am pasting the code snippet for this class you can copy+paste it if you wish to use this or you can create your own class once you get familiar with how Buffer API works.

We will be sharing a simple text message & link to twitter using this code snippet. First you need to do is include the class.bufferapp.php file

require_once 'class.bufferapp.php';
 $buffer = new BufferPHP('YOUR ACCESS TOKEN');

//now add the data

$data = array('profile_ids' => array());
$data['profile_ids'][] = 'TWITTER PROFILE ID';

$data['text'] = 'This is an example';

$data['media'] = array('link' => 'http://example.com/');

$data['client_id']='YOUR CLIENT ID';
$data['client_secret']= 'YOUR SECRET KEY';
$data['redirect_uri']= "REDIRECT URL";

//if you want to share this immediately set this to true else set it to false if you wish to schedule it for sharing later
$data['now']=true;        


Now that you have the information you need, your going to need to send it to Buffer's API using the following code:
$ret = $buffer->post('updates/create', $data);
?>

that’s it ! run the script and your tweet will get posted to your buffer connected twitter account !!

You can find more information about posting udpates and managing profiles on below links
http://bufferapp.com/developers/api/updates
http://bufferapp.com/developers/api/profiles

In the next blog post we will learn about oauth authentication for Buffer

Buffer – Updates

Updates An update represents a single post to a single social media account. An update can also include media attachments such as pictures and links.

via: bufferapp.com

Buffer – Profiles

Profiles A Buffer profile represents a connection to a single social media account.

via:bufferapp.com

Automatically Post Updates To The Buffer API With PHP | Little Apps Blog

find the class.bufferapp.php class here, which I have referenced for this post

via: www.little-apps.us

/**
 * BufferPHP v0.1
 *
 * See http://www.little-apps.org/blog/2012/09/automatically-post-updates-buffer-api-php/ for an example on how to use this class
 * Information on the Buffer API can be found at http://bufferapp.com/developers/api
 *
 * @package    BufferPHP
 * @author        Little Apps (http://www.little-apps.org)
 * @copyright   This is public domain so anyone can use it for any reason
 * @link        http://little-apps.org
 * @since        Version 0.1
 * @filesource
 */

if (!function_exists('curl_init')) {
  throw new Exception('BufferPHP needs the CURL PHP extension.');
}
if (!function_exists('json_decode')) {
  throw new Exception('BufferPHP needs the JSON PHP extension.');
}

class BufferPHP {
    // You can change the options below if you wish
    private $config = array(
                        'user_agent' => 'BufferPHP v0.1', // The user agent to send to the Buffer
API
                        'connect_timeout' => 30, // Maximum time to wait (in seconds) before connect fails
                        'timeout' => 30, // Maximum time to wait (in seconds) before API doesnt respond
                        'verify_ssl_cert' => false // This is false by default because some servers have trouble verifing SSL certificates through cURL
                        );
                        
    // You should only change the code below if you know what you're doing!
    
    /**
     * @var string Access token
     */
    private $access_token = '';
    
    /**
     * @var string Base URL for API
     */
    private $base_url = 'https://api.bufferapp.com/1/';
    
    /**
     * @var array HTTP headers from last request
     */
    public $http_header = array();
/**
     * @var array Info about last cURL request
     */
    public $http_info = array();
    /**
     * @var string The last URL used
     */
    public $last_url = '';

    /**
     * Class constructor
     * (Throws InvalidArgumentException if $token is not a string)
     *
     * @param string $token Access Token for app
     */
    function __construct($token) {
        if (is_string($token)) {
            $this->access_token = $token;
        } else {
            throw new InvalidArgumentException('token must be a string');
        }
    }
    
    /**
     * Sends GET request to Buffer API
*
     * @access public
     *
     * @param string $uri Endpoint to send data to (usually something like 'updates/create')
     * @param array $data Data to send to API. See API documentation for the parameters.
     *
     * @return class Returns a stdClass containing decode JSON data
     */
    public function get($uri, $data = array()) {
        if (is_array($data) && count($data) > 0) {
            $query = http_build_query($data);
        }
    
        return $this->api('get', $this->base_url . $uri, $query);
    }
    
    /**
     * Sends POST request to Buffer API
     *
     * @access public
     *
     * @param string $uri Endpoint to send data to (usually something like 'updates/create')
     * @param array $data Data to send to API. See API documentation for the parameters.
     *
     * @return class Returns a stdClass containing decode JSON data
*/
    public function post($uri, $data = array()) {
        if (is_array($data) && count($data) > 0) {
            $query = http_build_query($data);
        }
        
        return $this->api('post', $this->base_url . $uri, $query);
    }
    
    /**
     * Sends HTTP request to Buffer API
     *
     * @access private
     *
     * @param string $method Method to use to send data (post or get)
     * @param string $uri URL for where to send data
     * @param string $query_string Query string to pass to server (default is empty)
     *
     * @return class Returns a stdClass containing decode JSON data
     */
    private function api($method, $uri, $query_string = '') {
        if (preg_match('/\.json$/', $uri) == 0)
            $uri .= '.json';
            
        $this->http_info = array();
$ci = curl_init();
        /* Curl settings */
        curl_setopt($ci, CURLOPT_USERAGENT, $this->config['user_agent']);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->config['connect_timeout']);
        curl_setopt($ci, CURLOPT_TIMEOUT, $this->config['timeout']);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:', 'Authorization: Bearer ' . $this->access_token));
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->config['verify_ssl_cert']);
        curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
        curl_setopt($ci, CURLOPT_HEADER, 0);

        if ($method == 'post') {
            curl_setopt($ci, CURLOPT_POST, TRUE);
            if (!empty($query_string)) {
                curl_setopt($ci, CURLOPT_POSTFIELDS, $query_string);
            }
        } else if ($method == 'get' && !empty($query_string)) {
            $uri = $uri . '?' . $query_string;
        }

        curl_setopt($ci, CURLOPT_URL, $uri);
        $response = curl_exec($ci);

        $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
$this->last_url = $uri;
        
        curl_close ($ci);
        
        return json_decode($response);
    }
    
    /**
     * Used to save HTTP header of last request
     *
     * @access private
     *
     * @param resource $ch cURL handler
     * @param string HTTP header
     *
     * @return int Returns length of header
     */
    private function getHeader($ch, $header) {
        $i = strpos($header, ':');
        if (!empty($i)) {
            $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
            $value = trim(substr($header, $i + 2));
            $this->http_header[$key] = $value;
        }
        return strlen($header);
}
}