PHP & MySQL, Tutorials, Social Media

Buffer is a great social media management tool, perfect to scheduling Facebook and Twitter updates. I recently wanted to integrate Buffer into a website using the Buffer api. Thankfully the documentation for the Buffer api is clean and easy to follow.

Buffer requires OAuth 2.0 authentication they don’t have a php library, but I found a brilliant repo on Github https://github.com/thewebguy/bufferapp-php the buffer class provides a wrapper for integrating with Buffer.

To get started a app must be created https://buffer.com/developers/apps/create the form asks for the application name, description and a few more details one of them is the callback url this is important if you plan on letting users authenticate themselves. For my need I will generate a single token to be used within my website so I entered my website address.

One the app is registered you are provided with your client id and client secret and a token. 

Include the class and create a new instance pass in your client id, client secret and callback url (in my case I don’t need to use the callback so I will omit that)

session_start();
require('buffer.php');

$client_id = 'yourid';
$client_secret = 'yoursecret';

$buffer = new BufferApp($client_id, $client_secret, null);

This class is designed to store the token in a session in my case I wanted the token to be persistent so I have hard coded the token in the class:

function retrieve_access_token() {
    $this->access_token = 'token';//$_SESSION['oauth']['buffer']['access_token'];

    if ($this->access_token) {
        $this->ok = true;
    }
}

Next set your profile id’s that you want to post to, to get these you can login to buffer and select the profile the id is part of the url or using the api to look at the profile data:

$profiles = $buffer->go('/profiles');
print_r($profiles);

These two profile id’s a sample id’s. 

$profiles = array(
    '561aa0563v24244c321316b1', 
    '561aa056563bb32c321316b1',
);

Next loop through the id’s and post to Buffer, pass an array to updates/create the only part the array that is required the profile_ids the rest are optional I’ve used a selected of the available options see the full list https://buffer.com/developers/api/updates#updatescreate

foreach($profiles as $profile){
    $buffer->go('/updates/create', array(
        'text' => 'My article title',
        'media[title]'=>  'My article title',
        'media[link]'=>  'http://domain.com',
        'media[description]'=>  'you will love this story!',
        'media[picture]'=>  'http://domain.com/images/story.jpg',
        'media[thumbnail]'=>  'http://domain.com/images/story.jpg',
        'profile_ids[]' => $profile
    ));
}

Once run this will add your updates to your schedules on Buffer unless you’ve decided to run them straight away by using the now key in the array.

Putting it all together:

session_start();
require('buffer.php');

$client_id = 'yourid';
$client_secret = 'yoursecret';

$buffer = new BufferApp($client_id, $client_secret, null);

$profiles = array(
    '561aa0563v24244c321316b1', 
    '561aa056563bb32c321316b1',
);

foreach($profiles as $profile){
    $buffer->go('/updates/create', array(
        'text' => 'My article title',
        'media[title]'=>  'My article title',
        'media[link]'=>  'http://domain.com',
        'media[description]'=>  'you will love this story!',
        'media[picture]'=>  'http://domain.com/images/story.jpg',
        'media[thumbnail]'=>  'http://domain.com/images/story.jpg',
        'profile_ids[]' => $profile
    ));
}