2017 © Pedro Peláez
 

library buffer-api-php

image

randomshapes/buffer-api-php

  • Monday, April 24, 2017
  • by nowned
  • Repository
  • 2 Watchers
  • 0 Stars
  • 704 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 16 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

BufferApp

Simple PHP composer package for the amazing buffer at http://bufferapp.com, (*1)

Why?

There wasn't one listed on Buffer's website and a quick Google search didn't turn one up. For most use cases Buffer's plugins will work just fine, but for those of you looking to pump lots of info into buffer via PHP this may help!, (*2)

Using this library

  1. Add to composer
    • Run composer require randomshapes/buffer-api-php or manually add it to your composer.json file
  2. Create a new Buffer app
    • You'll need to register an app with buffer before you can begin
    • Initialize like this $buffer = new BufferApp($client_id, $client_secret, $callback_url); The callback_url needs to be the exact same as the app you registered
  3. Start adding buffers!
    • Once you're in you really only need to check $buffer->ok to see if you can perform actions, and then $buffer->go($endpoint, $data) to get going!
Image Attachments

The Buffer API seems to be missing documentation for the media parameter for creating an update., (*3)

Their example here includes media[link], media[title] & media[description]., (*4)

To get the desired result you will need to use media[picture] and media[thumbnail]., (*5)

Example

Set this thing up with your credentials and your callback URL. Remember: callback_url must match what you've got in Buffer exactly!, (*6)

    $client_id = '';
    $client_secret = '';
    $callback_url = 'http://127.0.0.1/callback';

Set up the new buffer client. This is a super simple action that does a few things under the hood. If $_GET['code'] is set on this page it assumes it came from Buffer and will attempt to trade that code for an access_token. If there is an access_token in the session it will be loaded in., (*7)

    use Buffer\App\BufferApp;
    $buffer = new BufferApp($client_id, $client_secret, $callback_url);

Once we've got an access_token set the $buffer->ok property will read true. It is false by default. Now that we've received access we are free to run queries against Buffer endpoints! Below we pull the list of profiles associated with the logged in buffer user and submit a test update to each one., (*8)

    if (!$buffer->ok) {
        echo '<a href="' . $buffer->get_login_url() . '">Connect to Buffer!</a>';
    } else {
        //this pulls all of the logged in user's profiles
        $profiles = $buffer->go('/profiles');

        if (is_array($profiles)) {
            foreach ($profiles as $profile) {
                //this creates a status on each one
                $buffer->go('/updates/create', array('text' => 'My first status update from BufferApp worked!', 'profile_ids[]' => $profile->id));
            }
        }
    }

Storage

Right now this baby just stores the access_token in $_SESSION['oauth']['buffer']['access_token']. If you are doing something serious with this you should probably rewrite the store_access_token() and retrieve_access_token() methods., (*9)

Realistically these methods should be replaced with some sort of abstraction -- pull requests are welcome!, (*10)

License

Apache-2.0 Do whatever you like with this. Based on some code by preilly.me and kevin.me, (*11)

The Versions

24/04 2017

dev-master

9999999-dev

  Sources   Download

Apache-2.0