Skip to content

Latest commit

 

History

History
101 lines (72 loc) · 3.67 KB

readme.md

File metadata and controls

101 lines (72 loc) · 3.67 KB

Twitter API V2 for PHP

PHP Badge Twitter Run Tests MIT Licensed last version Downloads

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

Installation

First, you need to add the component to your composer.json

composer require noweh/twitter-api-v2-php

Update your packages with composer update or install with composer install.

Github Actions

This repository uses Github Actions for each push/pull request with PHPStan/PHPUnit.

Therefore, for each valid push, a new Tweet is posted from my Twitter test account.

How to use

Active your developer account

Firstly, you need to follow this tutorial.

  • Request of an approved account;
  • Once you have an approved developer account, you will need to create a Project;
  • Enable read/write access for your Twitter app;
  • Generate Consumer Keys and Authentication Tokens;
  • Grab your Keys and Tokens from the twitter developer site.

Prepare settings

Settings are expected as below:

use Noweh\TwitterApi\Client;

$settings = [
    'account_id' => 1234567,
    'consumer_key' => 'CONSUMER_KEY',
    'consumer_secret' => 'CONSUMER_SECRET',
    'bearer_token' => 'BEARER_TOKEN',
    'access_token' => 'ACCESS_TOKEN',
    'acess_token_secret' => 'ACCESS_TOKEN_SECRET'
];

$client = new Client($settings);

To search specific tweets

Example:

$return = $client->tweetSearch()
    ->showMetrics()
    ->onlyWithMedias()
    ->addFilterOnUsernamesFrom([
        'twitterdev',
        'Noweh95'
    ], \Noweh\TwitterApi\Enum\Operators::or)
    ->addFilterOnKeywordOrPhrase([
        'Dune',
        'DenisVilleneuve'
    ], \Noweh\TwitterApi\Enum\Operators::and)
    ->addFilterOnLocales(['fr', 'en'])
    ->showUserDetails()
    ->performRequest()
;

To find Twitter Users

findByIdOrUsername() expects either an array, or a string.

You can specify the search mode as a second parameter:

->findByIdOrUsername('twitterdev', \Noweh\TwitterApi\Enum\Modes::username) //OR \Noweh\TwitterApi\Enum\Modes::id

To Post a new Tweet

Example:

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

To Retweet

Example:

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

Contributing

Fork/download the code and run

composer install

copy test/config/.env.example to test/config/.env and add your credentials for testing.

To run tests

./vendor/bin/phpunit

To run code analyzer

./vendor/bin/phpstan analyse .