221
224
$this->setRawNode('javaScript', $this->getSanitizer()->getParam('javaScript', ''));
224
protected function getToken()
227
$url = 'https://api.twitter.com/oauth2/token';
229
// Prepare the consumer key and secret
230
$key = base64_encode(urlencode($this->getSetting('apiKey')) . ':' . urlencode($this->getSetting('apiSecret')));
232
// Check to see if we have the bearer token already cached
233
$cache = $this->getPool()->getItem('bearer_' . $key);
235
$token = $cache->get();
237
if ($cache->isHit()) {
238
$this->getLog()->debug('Bearer Token served from cache');
242
$this->getLog()->debug('Bearer Token served from API');
244
// Shame - we will need to get it.
246
$httpOptions = array(
247
CURLOPT_TIMEOUT => 20,
248
CURLOPT_SSL_VERIFYPEER => true,
249
CURLOPT_HTTPHEADER => array(
250
'POST /oauth2/token HTTP/1.1',
251
'Authorization: Basic ' . $key,
252
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
255
CURLOPT_USERAGENT => 'Xibo Twitter Module',
256
CURLOPT_HEADER => false,
257
CURLINFO_HEADER_OUT => true,
258
CURLOPT_RETURNTRANSFER => true,
259
CURLOPT_POST => true,
260
CURLOPT_POSTFIELDS => http_build_query(array('grant_type' => 'client_credentials')),
265
if ($this->getConfig()->GetSetting('PROXY_HOST') != '' && !$this->getConfig()->isProxyException($url)) {
266
$httpOptions[CURLOPT_PROXY] = $this->getConfig()->GetSetting('PROXY_HOST');
267
$httpOptions[CURLOPT_PROXYPORT] = $this->getConfig()->GetSetting('PROXY_PORT');
269
if ($this->getConfig()->GetSetting('PROXY_AUTH') != '')
270
$httpOptions[CURLOPT_PROXYUSERPWD] = $this->getConfig()->GetSetting('PROXY_AUTH');
276
curl_setopt_array($curl, $httpOptions);
279
if (!$result = curl_exec($curl)) {
281
$this->getLog()->error('Error contacting Twitter API: ' . curl_error($curl));
285
// We want to check for a 200
286
$outHeaders = curl_getinfo($curl);
288
if ($outHeaders['http_code'] != 200) {
289
$this->getLog()->error('Twitter API returned ' . $result . ' status. Unable to proceed. Headers = ' . var_export($outHeaders, true));
291
// See if we can parse the error.
292
$body = json_decode($result);
294
$this->getLog()->error('Twitter Error: ' . ((isset($body->errors[0])) ? $body->errors[0]->message : 'Unknown Error'));
299
// See if we can parse the body as JSON.
300
$body = json_decode($result);
302
// We have a 200 - therefore we want to think about caching the bearer token
303
// First, lets check its a bearer token
304
if ($body->token_type != 'bearer') {
305
$this->getLog()->error('Twitter API returned OK, but without a bearer token. ' . var_export($body, true));
309
// It is, so lets cache it
311
$cache->set($body->access_token);
312
$cache->expiresAfter(100000);
313
$this->getPool()->saveDeferred($cache);
315
return $body->access_token;
318
protected function searchApi($token, $term, $resultType = 'mixed', $geoCode = '', $count = 15)
321
// Construct the URL to call
322
$url = 'https://api.twitter.com/1.1/search/tweets.json';
323
$queryString = '?q=' . urlencode(trim($term)) .
324
'&result_type=' . $resultType .
326
'&include_entities=true' .
327
'&tweet_mode=extended';
330
$queryString .= '&geocode=' . $geoCode;
332
$httpOptions = array(
333
CURLOPT_TIMEOUT => 20,
334
CURLOPT_SSL_VERIFYPEER => true,
335
CURLOPT_HTTPHEADER => array(
336
'GET /1.1/search/tweets.json' . $queryString . 'HTTP/1.1',
337
'Host: api.twitter.com',
338
'Authorization: Bearer ' . $token
340
CURLOPT_USERAGENT => 'Xibo Twitter Module',
341
CURLOPT_HEADER => false,
342
CURLINFO_HEADER_OUT => true,
343
CURLOPT_RETURNTRANSFER => true,
344
CURLOPT_URL => $url . $queryString,
348
if ($this->getConfig()->GetSetting('PROXY_HOST') != '' && !$this->getConfig()->isProxyException($url)) {
349
$httpOptions[CURLOPT_PROXY] = $this->getConfig()->GetSetting('PROXY_HOST');
350
$httpOptions[CURLOPT_PROXYPORT] = $this->getConfig()->GetSetting('PROXY_PORT');
352
if ($this->getConfig()->GetSetting('PROXY_AUTH') != '')
353
$httpOptions[CURLOPT_PROXYUSERPWD] = $this->getConfig()->GetSetting('PROXY_AUTH');
356
$this->getLog()->debug('Calling API with: ' . $url . $queryString);
359
curl_setopt_array($curl, $httpOptions);
360
$result = curl_exec($curl);
362
// Get the response headers
363
$outHeaders = curl_getinfo($curl);
365
if ($outHeaders['http_code'] == 0) {
367
$this->getLog()->error('Unable to reach twitter api.');
369
} else if ($outHeaders['http_code'] != 200) {
370
$this->getLog()->error('Twitter API returned ' . $outHeaders['http_code'] . ' status. Unable to proceed. Headers = ' . var_export($outHeaders, true));
372
// See if we can parse the error.
373
$body = json_decode($result);
375
$this->getLog()->error('Twitter Error: ' . ((isset($body->errors[0])) ? $body->errors[0]->message : 'Unknown Error'));
380
// Parse out header and body
381
$body = json_decode($result);
228
* @param int $displayId
229
* @param bool $isPreview
231
* @throws ConfigurationException
386
233
protected function getTwitterFeed($displayId = 0, $isPreview = true)
388
235
if (!extension_loaded('curl'))