-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterGetter.php
More file actions
56 lines (47 loc) · 1.45 KB
/
TwitterGetter.php
File metadata and controls
56 lines (47 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* Task for pilulka.cz
* Author: Pavel Balajka
*
* TwitterGetter Class
*/
use Abraham\TwitterOAuth\TwitterOAuth;
class TwitterGetter
{
public const API_SEARCH_TWEETS = 'search/tweets';
public const API_QUERY_COMBINATOR = ' OR ';
public const API_RECENT_TWEETS = 'recent';
private $connection;
public function __construct()
{
$this->connection = null;
}
/**
* Establish twitter API connection with credentials
*
* @param string $consumerKey
* @param string $consumerSecret
* @param string $accessToken
* @param string $accesSecret
* @return void
*/
public function createConnection(string $consumerKey, string $consumerSecret, string $accessToken, string $accesSecret): void
{
$this->connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accesSecret);
}
public function searchTweets(array $searchTerms, int $limit = 0)
{
if ($this->connection) {
$searchParameters = [
'q' => implode(self::API_QUERY_COMBINATOR, $searchTerms),
'result_type' => self::API_RECENT_TWEETS
];
if ($limit) {
$searchParameters['count'] = $limit;
}
return $this->connection->get(self::API_SEARCH_TWEETS, $searchParameters);
} else {
return null;
}
}
}