/
TwitterAPIClient.php
58 lines (44 loc) · 1.64 KB
/
TwitterAPIClient.php
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
57
58
<?php
require_once('vendor/j7mbo/twitter-api-php/TwitterAPIExchange.php');
class TwitterAPIClient {
private $creds = array();
public function __construct($oauth, $userName) {
$this->creds['oauth'] = $oauth;
$this->creds['user'] = $userName;
}
public function getFollowingIds() {
$url = 'https://api.twitter.com/1.1/friends/ids.json';
$responseObj = $this->getRequest($url, array('screen_name=' => $this->creds['user']));
return $responseObj->ids;
}
public function addListMembers($listId, $userIds) {
$url = 'https://api.twitter.com/1.1/lists/members/create_all.json';
$payload = array(
'list_id' => $listId,
'screen_name' => implode(',', $userIds),
);
return $this->postRequest($url, $payload);
}
private function postRequest($url, $params = array()) {
$postFields = array();
foreach($params as $key => $value) {
$postFields[$key] = $value;
}
$twitter = new TwitterAPIExchange($this->creds['oauth']);
$response = $twitter->buildOauth($url, 'POST')
->setPostfields($postFields)
->performRequest();
return $response;
}
private function getRequest($url, $params = array()) {
$getfield = '?';
foreach($params as $key => $value) {
$getfield .= '&' . $key . '=' . $value;
}
$twitter = new TwitterAPIExchange($this->creds['oauth']);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, 'GET')
->performRequest();
return json_decode($response);
}
}