Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 84 additions & 3 deletions src/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,55 @@ public function sendDirectMessage(string $username, string $message): stdClass
}


/**
* Unfollows a user on Twitter.
* https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-destroy
* @throws Exception
*/
public function unfriend(string $username = null, int $userId = null): stdClass
{
if (!$username) {
$data = ['screen_name' => $username];
}
if (!$userId) {
$data = ['screen_name' => $userId];
}
if (!$userId && !$username) {
throw new \InvalidArgumentException('Username OR userId is required to destroy a friendship!');
}
return $this->request('friendships/destroy', 'POST', $data);
}

/**
* Create a new list
*
* @param $listName
* @param string $mode
* @param null $description
* @return mixed
* @throws Exception
*/
public function createList($listName, $mode = 'private', $description = null)
{
return $this->request('lists/create', 'POST', ['name' => $listName, 'mode' => $mode]);
}

/**
* Add members (comma separtated screen names) to a pre-existing list
*
* @param $listId
* @param array $members
* @return mixed
* @throws Exception
*/
public function addMembersToList($listId, array $members)
{
return $this->request('lists/members/create_all', 'POST', ['list_id' => $listId, 'screen_name' => implode(',', $members)]);
}

/**
* Follows a user on Twitter.
* https://dev.twitter.com/rest/reference/post/friendships/create
* https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-create
* @throws Exception
*/
public function follow(string $username): stdClass
Expand Down Expand Up @@ -191,6 +237,28 @@ public function loadUserInfoById(string $id): stdClass
}


/**
* Returns IDs of friends of a given user.
* https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-list
* @throws Exception
*/
public function loadUserFriends(string $username, int $count = 5000, int $cursor = -1, $cacheExpiry = null): stdClass
{
return $this->cachedRequest('friends/ids', [
'screen_name' => $username,
'count' => $count,
'cursor' => $cursor,
], $cacheExpiry);
}

public function loadUserLists(string $username, $reverse = false)
{
return $this->cachedRequest('lists/list', [
'screen_name' => $username,
'reverse' => $reverse
]);
}

/**
* Returns IDs of followers of a given user.
* https://dev.twitter.com/rest/reference/get/followers/ids
Expand All @@ -206,6 +274,20 @@ public function loadUserFollowers(string $username, int $count = 5000, int $curs
}


/**
* Returns list of friends of a given user.
* https://dev.twitter.com/rest/reference/get/friends/list
* @throws Exception
*/
public function loadUserFriendsList(string $username, int $count = 200, int $cursor = -1, $cacheExpiry = null): stdClass
{
return $this->cachedRequest('friends/list', [
'screen_name' => $username,
'count' => $count,
'cursor' => $cursor,
], $cacheExpiry);
}

/**
* Returns list of followers of a given user.
* https://dev.twitter.com/rest/reference/get/followers/list
Expand Down Expand Up @@ -303,8 +385,7 @@ public function request(string $resource, string $method, array $data = [], arra
$method = 'POST';
$data = json_encode($data);
$headers[] = 'Content-Type: application/json';

} elseif (($method === 'GET' || $method === 'DELETE') && $data) {
} elseif (($method === 'GET' || $method === 'POST') && $data) {
$resource .= '?' . http_build_query($data, '', '&');
}

Expand Down