Skip to content

Commit

Permalink
update readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
noweh committed May 7, 2023
1 parent 25b4554 commit b99b46c
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 42 deletions.
207 changes: 193 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
# Twitter API V2 for PHP

![php](https://img.shields.io/badge/PHP-v7.4-828cb7.svg?style=flat-square)
[![Badge Twitter](https://img.shields.io/endpoint?url=https%3A%2F%2Ftwbadges.glitch.me%2Fbadges%2Fv2)](https://developer.twitter.com/en/docs/twitter-api)
![PHP](https://img.shields.io/badge/PHP-v7.4+-828cb7.svg?style=flat-square&logo=php)
[![Badge Twitter](https://img.shields.io/badge/Twitter%20API-v2-828cb7.svg?style=flat-square&logo=twitter&color=1DA1F2)](https://developer.twitter.com/en/docs/twitter-api)
[![Run Tests](https://github.com/noweh/twitter-api-v2-php/actions/workflows/run-tests.yml/badge.svg)](https://github.com/noweh/twitter-api-v2-php/actions/workflows/run-tests.yml)
[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](licence.md)
[![MIT Licensed](https://img.shields.io/github/license/noweh/twitter-api-v2-php)](licence.md)
[![last version](https://img.shields.io/packagist/v/noweh/twitter-api-v2-php)](https://packagist.org/packages/noweh/twitter-api-v2-php)
[![Downloads](https://img.shields.io/packagist/dt/noweh/twitter-api-v2-php)](https://packagist.org/packages/noweh/twitter-api-v2-php)
[![twitter](https://img.shields.io/twitter/follow/Noweh95?style=social)](https://twitter.com/Noweh95)

Twitter API V2 is a PHP package which provides an easy and fast access to Twitter REST API for Version 2 endpoints.

## Documentation

* [Installation](#installation)
* [Github Actions](#github-actions)
* [How to use](#how-to-use)
- [Active your developer account](#active-your-developer-account)
- [Prepare settings](#prepare-settings)
* [Tweets endpoints](#tweet-endpoints)
- [Timeline endpoints](#timeline-endpoints)
- [Retrieve the users which you've blocked](#retrieve-the-users-which-youve-blocked)
- [User/Follows endpoints](#userfollows-endpoints)
- [Find Recent Mentioning for a User](#find-recent-mentioning-for-a-user)
- [Find Recent Tweets for a User](#find-recent-tweets-for-a-user)
- [Reverse Chronological Timeline by user ID](#reverse-chronological-timeline-by-user-id)
- [Tweet/Likes endpoints](#tweetlikes-endpoints)
- [Tweets liked by a user](#tweets-liked-by-a-user)
- [Users who liked a tweet](#users-who-liked-a-tweet)
- [Tweet/Lookup endpoints](#tweetlikes-endpoints)
- [Search specific tweets](#search-specific-tweets)
- [Find all replies from a Tweet](#find-all-replies-from-a-tweet)
- [Tweet endpoints](#tweetlikes-endpoints)
- [Fetch a tweet by Id](#fetch-a-tweet-by-id)
- [Create a new Tweet](#create-a-new-tweet)
- [Tweet/Quotes endpoints](#tweetquotes-endpoints)
- [Returns Quote Tweets for a Tweet specified by the requested Tweet ID](#returns-quote-tweets-for-a-tweet-specified-by-the-requested-tweet-id)
- [Retweet endpoints](#retweet-endpoints)
- [Retweet a Tweet](#retweet-a-tweet)
- [Tweet/Replies endpoints](#tweetreplies-endpoints)
- [Hide a reply to a Tweet](#hide-a-reply-to-a-tweet)
- [Unhide a reply to a Tweet](#unhide-a-reply-to-a-tweet)
- [Tweet/Bookmarks endpoints](#tweetbookmarks-endpoints)
- [Lookup a user's Bookmarks](#lookup-a-users-bookmarks)
* [Users endpoints](#users-endpoints)
- [User/Blocks endpoints](#userblocks-endpoints)
- [Retrieve the users which you've blocked](#retrieve-the-users-which-youve-blocked)
- [User/Follows endpoints](#userfollows-endpoints)
- [Retrieve the users which are following you](#retrieve-the-users-which-are-following-you)
- [Retrieve the users which you are following](#retrieve-the-users-which-you-are-following)
- [User/Lookup endpoints](#userlookup-endpoints)
- [Find Twitter Users](#find-twitter-users)
- [User/Mutes endpoints](#usermutes-endpoints)
- [Retrieve the users which you've muted](#retrieve-the-users-which-youve-muted)
- [Mute user by username or ID](#mute-user-by-username-or-id)
- [Unmute user by username or ID](#unmute-user-by-username-or-id)
- [User/Follows endpoints](#userfollows-endpoints)
- [Follow a user](#follow-a-user)
- [Unfollow a user](#unfollow-a-user)
* [Contributing](#contributing)
- [To run test](#to-run-tests)
- [To run code analyzer](#to-run-code-analyzer)

## Installation
First, you need to add the component to your composer.json
Expand Down Expand Up @@ -44,13 +97,41 @@ Settings are expected as below:
$settings['bearer_token']

$client = new Client($settings);
---
## Tweets endpoints

### To fetch a tweet by Id
## Timeline endpoints

### Find Recent Mentioning for a User
Example:

$return = $client->timeline()->getRecentMentions($accountId)->performRequest();

### Find Recent Tweets for a User
Example:

$return = $client->timeline()->getRecentTweets($accountId)->performRequest();

### Reverse Chronological Timeline by user ID
Example:

$return = $client->timeline()->getReverseChronological()->performRequest();

## Tweet/Likes endpoints

### Tweets liked by a user
Example:

$result = $client->tweet()->performRequest('GET', array( 'id' => $id));
$return = $client->tweetLikes()->addMaxResults($pageSize)->getLikedTweets($accountId)->performRequest();

### To search specific tweets
### Users who liked a tweet
Example:

$return = $client->tweetLikes()->addMaxResults($pageSize)->getUsersWhoLiked($tweetId)->performRequest();

## Tweet/Lookup endpoints

### Search specific tweets
Example:

$return = $client->tweetLookup()
Expand All @@ -69,7 +150,90 @@ Example:
->performRequest()
;

### To find Twitter Users
$client->tweetLookup()
->addMaxResults($pageSize)
->addFilterOnKeywordOrPhrase($keywordFilter)
->addFilterOnLocales($localeFilter)
->showUserDetails()
->showMetrics()
->performRequest()
;

### Find all replies from a Tweet

->addFilterOnConversationId($tweetId);

## Tweet endpoints

### Fetch a tweet by Id
Example:

$return = $client->tweet()->->fetch(1622477565565739010)->performRequest();

### Create a new Tweet
Example:

$return = $client->tweet()->create()->performRequest(['text' => 'Test Tweet... ']);

## Tweet/Quotes endpoints

### Returns Quote Tweets for a Tweet specified by the requested Tweet ID
Example:

$return = $client->tweetQuotes()->getQuoteTweets($tweetId)->performRequest();

## Retweet endpoints

### Retweet a Tweet
Example:

$return = $client->retweet()->performRequest(['tweetId' => $tweet_id]);

## Tweet/Replies endpoints

### Hide a reply to a Tweet
Example:

$return = $client->->tweetReplies()->hideReply($tweetId)->performRequest(['hidden' => true]);

### Unhide a reply to a Tweet
Example:

$return = $client->->tweetReplies()->hideReply($tweetId)->performRequest(['hidden' => false]);

## Tweet/Bookmarks endpoints

### Lookup a user's Bookmarks
Example:

$return = $client->tweetBookmarks()->lookup()->performRequest();

---

## Users endpoints

## User/Blocks endpoints

### Retrieve the users which you've blocked
Example:

$return = $client->userBlocks()->lookup()->performRequest();

## User/Follows endpoints

### Retrieve the users which are following you
Example:

$return = $client->userFollows()->getFollowers()->performRequest();

### Retrieve the users which you are following
Example:

$return = $client->userFollows()->getFollowing()->performRequest();

## User/Lookup endpoints

### Find Twitter Users
`findByIdOrUsername()` expects either an array, or a string.

You can specify the search mode as a second parameter (`Client::MODES['USERNAME']` OR `Client::MODES['ID']`)
Expand All @@ -81,21 +245,36 @@ Example:
->performRequest()
;

### To find Recent Mentioning for a User
## User/Mutes endpoints

### Retrieve the users which you've muted
Example:

$return = $client->timeline()->findRecentMentioningForUserId('1538300985570885636')->performRequest();
$return = $client->userMutes()->lookup()->performRequest();

### To Post a new Tweet
### Mute user by username or ID
Example:

$return = $client->tweet()->performRequest('POST', ['text' => 'This is a test....']);
$return = $client->userMutes()->mute()->performRequest(['target_user_id' => $userId]);

### To Retweet
### Unmute user by username or ID
Example:

$return = $client->userMutes()->unmute()->performRequest(['target_user_id' => $userId]);

## User/Follows endpoints

### Follow a user
Example:

$return = $client->retweet()->performRequest('POST', ['tweet_id' => $tweet->id]);

$return = $client->userFollows()->follow()->performRequest(['target_user_id' => $userId]);

### Unfollow a user
Example:

$return = $client->userFollows()->unfollow($userId)->performRequest(['target_user_id' => self::$userId]);

---

## Contributing
Fork/download the code and run
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function setAuthMode(int $value): void
*/
private function extensionLoaded(string $ext): void
{
if (! extension_loaded($ext)) {
if (!extension_loaded($ext)) {
throw new \Exception('PHP extension '.strtoupper($ext).' is not loaded.');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Timeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getReverseChronological(): Timeline
*/
protected function constructEndpoint(): string {
$endpoint = parent::constructEndpoint();
if (! is_null($this->next_page_token)) {
if (!is_null($this->next_page_token)) {
$this->query_string['pagination_token'] = $this->next_page_token;
$endpoint .= '?' . http_build_query($this->query_string);
}
Expand Down
9 changes: 5 additions & 4 deletions src/TweetBookmarks.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* @see <a href="https://developer.twitter.com/en/docs/twitter-api/tweets/bookmarks/api-reference">Bookmarks</a>
* @author Martin Zeitler
*/
class TweetBookmarks extends AbstractController {

class TweetBookmarks extends AbstractController
{
/**
* @param array<int, string> $settings
* @throws \Exception
Expand Down Expand Up @@ -58,9 +58,10 @@ public function removeBookmark(int $target_tweet_id): TweetBookmarks
* @return string the URL for the request.
* @throws \Exception
*/
protected function constructEndpoint(): string {
protected function constructEndpoint(): string
{
$endpoint = parent::constructEndpoint();
if (! is_null($this->next_page_token)) {
if (!is_null($this->next_page_token)) {
$this->query_string['pagination_token'] = $this->next_page_token;
$endpoint .= '?' . http_build_query($this->query_string);
}
Expand Down
9 changes: 5 additions & 4 deletions src/TweetLikes.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ public function unlike(int $tweet_id): TweetLikes
* @return string the URL for the request.
* @throws \Exception
*/
protected function constructEndpoint(): string {
if (! empty($this->maxResults)) {
protected function constructEndpoint(): string
{
if (!empty($this->maxResults)) {
$this->query_string['max_results'] = $this->maxResults;
}
if (! is_null($this->next_page_token)) {
if (!is_null($this->next_page_token)) {
$this->query_string['pagination_token'] = $this->next_page_token;
}
$endpoint = parent::constructEndpoint();
if (sizeof($this->query_string) > 0) {
if (count($this->query_string) > 0) {
$endpoint .= '?' . http_build_query($this->query_string);
}
return $endpoint;
Expand Down
4 changes: 2 additions & 2 deletions src/TweetQuotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @see <a href="https://developer.twitter.com/en/docs/twitter-api/tweets/quote-tweets/api-reference/get-tweets-id-quote_tweets">Quote Tweets</a>
* @author Martin Zeitler
*/
class TweetQuotes extends AbstractController {

class TweetQuotes extends AbstractController
{
/**
* @param array<int, string> $settings
* @throws \Exception
Expand Down
4 changes: 2 additions & 2 deletions src/TweetReplies.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @see <a href="https://developer.twitter.com/en/docs/twitter-api/tweets/hide-replies/api-reference/put-tweets-id-hidden">Hide replies</a>
* @author Martin Zeitler
*/
class TweetReplies extends AbstractController {

class TweetReplies extends AbstractController
{
/**
* @param array<int, string> $settings
* @throws \Exception
Expand Down
9 changes: 5 additions & 4 deletions src/UserBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @see <a href="https://developer.twitter.com/en/docs/twitter-api/users/blocks/api-reference/get-users-blocking">Blocks</a>
* @author Martin Zeitler
*/
class UserBlocks extends AbstractController {

class UserBlocks extends AbstractController
{
/**
* @param array<int, string> $settings
* @throws \Exception
Expand Down Expand Up @@ -58,9 +58,10 @@ public function unblock(int $user_id): UserBlocks
* @return string the URL for the request.
* @throws \Exception
*/
protected function constructEndpoint(): string {
protected function constructEndpoint(): string
{
$endpoint = parent::constructEndpoint();
if (! is_null($this->next_page_token)) {
if (!is_null($this->next_page_token)) {
$this->query_string['pagination_token'] = $this->next_page_token;
$endpoint .= '?' . http_build_query($this->query_string);
}
Expand Down
7 changes: 4 additions & 3 deletions src/UserFollows.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @see <a href="https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference">Follows</a>
* @author Martin Zeitler
*/
class UserFollows extends AbstractController {

class UserFollows extends AbstractController
{
/**
* @param array<int, string> $settings
* @throws \Exception
Expand Down Expand Up @@ -68,7 +68,8 @@ public function unfollow(int $target_user_id): UserFollows
* @return string the URL for the request.
* @throws \Exception
*/
protected function constructEndpoint(): string {
protected function constructEndpoint(): string
{
$endpoint = parent::constructEndpoint();
if (! is_null($this->next_page_token)) {
$this->query_string['pagination_token'] = $this->next_page_token;
Expand Down
Loading

0 comments on commit b99b46c

Please sign in to comment.