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 '403' => 'Permission denied.',
46 '404' => 'Endpoint not found.',
47 '405' => 'Method not allowed.',
48 '504' => 'Gateway timeout server response timeout.',
49 '1000' => 'An unknown error occurred.',
50 '1001' => 'Access token required.',
51 '1002' => 'Not within application scope.',
52 '1003' => 'Parameter not recognized.',
53 '1004' => 'Required parameter missing.',
54 '1005' => 'Unsupported response format.',
55 '1006' => 'Parameter value not within bounds.',
56 '1010' => 'Profile could not be found.',
57 '1011' => 'No authorization to access profile.',
58 '1012' => 'Profile did not save successfully.',
59 '1013' => 'Profile schedule limit reached.',
60 '1014' => 'Profile limit for user has been reached.',
61 '1015' => 'Profile could not be destroyed.',
62 '1016' => 'Profile buffer could not be emptied.',
63 '1020' => 'Update could not be found.',
64 '1021' => 'No authorization to access update.',
65 '1022' => 'Update did not save successfully.',
66 '1023' => 'Update limit for profile has been reached.',
67 '1024' => 'Update limit for team profile has been reached.',
68 '1025' => "Update was recently posted, can't post duplicate content.",
69 '1026' => 'Update must be in error status to requeue.',
70 '1027' => 'Update must be in buffer and not custom scheduled in order to move to top.',
71 '1028' => 'Update soft limit for profile reached.',
72 '1029' => 'Event type not supported.',
73 '1030' => 'Media filetype not supported.',
74 '1031' => 'Media filesize out of acceptable range.',
75 '1032' => 'Unable to post image to LinkedIn group(s).',
76 '1033' => 'Comments can only be posted to Facebook at this time.',
77 '1034' => 'Cannot schedule updates in the past.',
78 '1042' => 'User did not save successfully.',
79 '1050' => 'Client could not be found.',
80 '1051' => 'No authorization to access client.',
83 function __construct($client_id = '', $client_secret = '', $callback_url = '', $access_token = '') {
84 if ($client_id) $this->set_client_id($client_id);
85 if ($client_secret) $this->set_client_secret($client_secret);
86 if ($callback_url) $this->set_callback_url($callback_url);
87 if ($access_token) $this->access_token = $access_token;
89 if (isset($_GET['code']) && $_GET['code']) {
90 $this->code = $_GET['code'];
91 $this->create_access_token_url();
95 $this->retrieve_access_token();
98 function go($endpoint = '', $data = '') {
99 if (in_array($endpoint, array_keys($this->endpoints))) {
100 $done_endpoint = $endpoint;
104 foreach (array_keys($this->endpoints) as $done_endpoint) {
105 if (preg_match('/' . preg_replace('/(\:\w+)/i', '(\w+)', str_replace('/', '\/', $done_endpoint)) . '/i', $endpoint, $match)) {
111 if (!$ok) return $this->error('invalid-endpoint');
114 if (!$data || !is_array($data)) $data = [];
115 $data['access_token'] = $this->access_token;
117 $method = $this->endpoints[$done_endpoint]; //get() or post()
118 return $this->$method($this->buffer_url . $endpoint . '.json', $data);
121 function store_access_token() {
122 $_SESSION['oauth']['buffer']['access_token'] = $this->access_token;
125 function retrieve_access_token() {
126 $this->access_token = $_SESSION['oauth']['buffer']['access_token'];
128 if ($this->access_token) {
133 function error($error) {
134 return (object) ['error' => $this->errors[$error]];
137 function create_access_token_url() {
139 'code' => $this->code,
140 'grant_type' => 'authorization_code',
141 'client_id' => $this->client_id,
142 'client_secret' => $this->client_secret,
143 'redirect_uri' => $this->callback_url,
146 $obj = $this->post($this->access_token_url, $data);
147 $this->access_token = $obj->access_token;
149 $this->store_access_token();
152 function req($url = '', $data = '', $post = true) {
153 if (!$url) return false;
154 if (!$data || !is_array($data)) $data = [];
156 $options = [CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false];
160 CURLOPT_POST => $post,
161 CURLOPT_POSTFIELDS => $data
164 $url .= '?' . http_build_query($data);
167 $ch = curl_init($url);
168 curl_setopt_array($ch, $options);
169 $rs = curl_exec($ch);
171 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
173 return $this->error($code);
176 return json_decode($rs);
179 function get($url = '', $data = '') {
180 return $this->req($url, $data, false);
183 function post($url = '', $data = '') {
184 return $this->req($url, $data, true);
187 function get_login_url() {
188 return $this->authorize_url . '?'
189 . 'client_id=' . $this->client_id
190 . '&redirect_uri=' . urlencode($this->callback_url)
191 . '&response_type=code';
194 function set_client_id($client_id) {
195 $this->client_id = $client_id;
198 function set_client_secret($client_secret) {
199 $this->client_secret = $client_secret;
202 function set_callback_url($callback_url) {
203 $this->callback_url = $callback_url;