4 private $client_secret;
9 private $authorize_url = 'https://bufferapp.com/oauth2/authorize';
10 private $access_token_url = 'https://api.bufferapp.com/1/oauth2/token.json';
11 private $buffer_url = 'https://api.bufferapp.com/1';
15 private $endpoints = [
19 '/profiles/:id' => 'get',
20 '/profiles/:id/schedules' => 'get',
21 '/profiles/:id/schedules/update' => 'post', // Array schedules [0][days][]=mon, [0][times][]=12:00
23 '/updates/:id' => 'get',
24 '/profiles/:id/updates/pending' => 'get',
25 '/profiles/:id/updates/sent' => 'get',
26 '/updates/:id/interactions' => 'get',
28 '/profiles/:id/updates/reorder' => 'post', // Array order, int offset, bool utc
29 '/profiles/:id/updates/shuffle' => 'post',
30 '/updates/create' => 'post', // String text, Array profile_ids, Aool shorten, Bool now, Array media ['link'], ['description'], ['picture']
31 '/updates/:id/update' => 'post', // String text, Bool now, Array media ['link'], ['description'], ['picture'], Bool utc
32 '/updates/:id/share' => 'post',
33 '/updates/:id/destroy' => 'post',
34 '/updates/:id/move_to_top' => 'post',
36 '/links/shares' => 'get',
38 '/info/configuration' => 'get',
43 'invalid-endpoint' => 'The endpoint you supplied does not appear to be valid.',
45 '401' => 'Unauthorized.',
46 '403' => 'Permission denied.',
47 '404' => 'Endpoint not found.',
48 '405' => 'Method not allowed.',
49 '504' => 'Gateway timeout server response timeout.',
50 '1000' => 'An unknown error occurred.',
51 '1001' => 'Access token required.',
52 '1002' => 'Not within application scope.',
53 '1003' => 'Parameter not recognized.',
54 '1004' => 'Required parameter missing.',
55 '1005' => 'Unsupported response format.',
56 '1006' => 'Parameter value not within bounds.',
57 '1010' => 'Profile could not be found.',
58 '1011' => 'No authorization to access profile.',
59 '1012' => 'Profile did not save successfully.',
60 '1013' => 'Profile schedule limit reached.',
61 '1014' => 'Profile limit for user has been reached.',
62 '1015' => 'Profile could not be destroyed.',
63 '1016' => 'Profile buffer could not be emptied.',
64 '1020' => 'Update could not be found.',
65 '1021' => 'No authorization to access update.',
66 '1022' => 'Update did not save successfully.',
67 '1023' => 'Update limit for profile has been reached.',
68 '1024' => 'Update limit for team profile has been reached.',
69 '1025' => "Update was recently posted, can't post duplicate content.",
70 '1026' => 'Update must be in error status to requeue.',
71 '1027' => 'Update must be in buffer and not custom scheduled in order to move to top.',
72 '1028' => 'Update soft limit for profile reached.',
73 '1029' => 'Event type not supported.',
74 '1030' => 'Media filetype not supported.',
75 '1031' => 'Media filesize out of acceptable range.',
76 '1032' => 'Unable to post image to LinkedIn group(s).',
77 '1033' => 'Comments can only be posted to Facebook at this time.',
78 '1034' => 'Cannot schedule updates in the past.',
79 '1042' => 'User did not save successfully.',
80 '1050' => 'Client could not be found.',
81 '1051' => 'No authorization to access client.',
84 function __construct($client_id = '', $client_secret = '', $callback_url = '', $access_token = '') {
85 if ($client_id) $this->set_client_id($client_id);
86 if ($client_secret) $this->set_client_secret($client_secret);
87 if ($callback_url) $this->set_callback_url($callback_url);
88 if ($access_token) $this->access_token = $access_token;
90 if (isset($_GET['code']) && $_GET['code']) {
91 $this->code = $_GET['code'];
92 $this->create_access_token_url();
96 $this->retrieve_access_token();
99 function go($endpoint = '', $data = '') {
100 if (in_array($endpoint, array_keys($this->endpoints))) {
101 $done_endpoint = $endpoint;
105 foreach (array_keys($this->endpoints) as $done_endpoint) {
106 if (preg_match('/' . preg_replace('/(\:\w+)/i', '(\w+)', str_replace('/', '\/', $done_endpoint)) . '/i', $endpoint, $match)) {
112 if (!$ok) return $this->error('invalid-endpoint');
115 if (!$data || !is_array($data)) $data = [];
116 $data['access_token'] = $this->access_token;
118 $method = $this->endpoints[$done_endpoint]; //get() or post()
119 return $this->$method($this->buffer_url . $endpoint . '.json', $data);
122 function store_access_token() {
123 $_SESSION['oauth']['buffer']['access_token'] = $this->access_token;
126 function retrieve_access_token() {
127 $this->access_token = $_SESSION['oauth']['buffer']['access_token'];
129 if ($this->access_token) {
134 function error($error) {
135 return (object) ['error' => $this->errors[$error]];
138 function create_access_token_url() {
140 'code' => $this->code,
141 'grant_type' => 'authorization_code',
142 'client_id' => $this->client_id,
143 'client_secret' => $this->client_secret,
144 'redirect_uri' => $this->callback_url,
147 $obj = $this->post($this->access_token_url, $data);
148 $this->access_token = $obj->access_token;
150 $this->store_access_token();
153 function req($url = '', $data = '', $post = true) {
154 if (!$url) return false;
155 if (!$data || !is_array($data)) $data = [];
157 $options = [CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false];
161 CURLOPT_POST => $post,
162 CURLOPT_POSTFIELDS => $data
165 $url .= '?' . http_build_query($data);
168 $ch = curl_init($url);
169 curl_setopt_array($ch, $options);
170 $rs = curl_exec($ch);
172 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
174 return $this->error($code);
177 return json_decode($rs);
180 function get($url = '', $data = '') {
181 return $this->req($url, $data, false);
184 function post($url = '', $data = '') {
185 return $this->req($url, $data, true);
188 function get_login_url() {
189 return $this->authorize_url . '?'
190 . 'client_id=' . $this->client_id
191 . '&redirect_uri=' . urlencode($this->callback_url)
192 . '&response_type=code';
195 function set_client_id($client_id) {
196 $this->client_id = $client_id;
199 function set_client_secret($client_secret) {
200 $this->client_secret = $client_secret;
203 function set_callback_url($callback_url) {
204 $this->callback_url = $callback_url;