PHP package providing easy and fast access to Twitter API V2.

Overview

Twitter API V2 for PHP

php Badge Twitter MIT Licensed

Twitter API V2 is a PHP package that 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.

How to use

Active your developer account

In first, you need to follow this tutorial.

  • Request an approved account;
  • Once you have an approved developer account, you will need to first 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 in this form:

$settings['access_token'],
$settings['access_token_secret'],
$settings['consumer_key'],
$settings['consumer_secret'],
$settings['bearer_token']

To search specific tweets

use Noweh\TwitterApi\TweetSearch;

Example:

$settings = ['...', '...']; // Previously retrieved from Twitter app

$return = (new TweetSearch($settings))
    ->showMetrics()
    ->onlyWithMedias()
    ->addFilterOnUsernamesFrom([
        'twitterdev',
        'Noweh95'
    ], TweetSearch::OPERATORS['OR'])
    ->addFilterOnKeywordOrPhrase([
        'Dune',
        'DenisVilleneuve'
    ], TweetSearch::OPERATORS['AND'])
    ->addFilterOnLocales(['fr', 'en'])
    ->showUserDetails()
    ->performRequest()
;

To find Twitter Users

use Noweh\TwitterApi\UserSearch;

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

You can specify the search mode as a second parameter (UserSearch::MODES['USERNAME'] OR UserSearch::MODES['ID'])

Example:

$settings = ['...', '...']; // Previously retrieved from Twitter app

$return = (new UserSearch($settings))
    ->findByIdOrUsername('twitterdev', UserSearch::MODES['USERNAME'])
    ->performRequest()
;

To Retweet

use Noweh\TwitterApi\Retweet;

You have to add your account ID in settings for Oauth1.0a

$settings['account_id']

Example:

$settings = ['...', '...']; // Previously retrieved from Twitter app

$retweeter = new Retweet($this->settings);
$return = $retweeter->performRequest('POST', ['tweet_id' => $tweet->id]);
Comments
  • SSL Certificate error

    SSL Certificate error

    Hello guys, I'm currently working on a student project running on symfony 5.4.11, I have to use the twitter api, I tried to use this library but I'm facing an SSL certificate error...

    cURL error 60: SSL certificate problem: unable to get local issuer certificate

    I tried to install symfony SSL certificate with > symfony server:ca:install and i search for solutions on the web but i cannot find anything, I'm really lost and my project is due soon, I don't see how to do it...

    Do you know what cause this issue but most importantly how to resolve it ?

    Thanks a lot.

    My code :

    <?php
    
    namespace App\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;
    use Noweh\TwitterApi\Client;
    
    class TwitterController extends AbstractController
    {
    
        public function __construct()
        {
            $settings = [
                'account_id' => $_ENV['TWI_APPID'],
                'consumer_key' => $_ENV['TWI_APPID'],
                'consumer_secret' => $_ENV['TWI_APPID'],
                'bearer_token' => $_ENV['TWI_BEARERTOKEN'],
                'access_token' => $_ENV['TWI_ACCESSTOKEN'],
                'access_token_secret' => $_ENV['TWI_ACCESSTOKENSECRET']
            ];
    
            $client = new Client($settings);
            $this->client = $client;
        }
    
        /** 
        * @Route("/api/twitter/getHashtag", name="app_twitter", methods={"GET", "POST"})
        */
        public function index(): Response
        {
            $return = $this->client->tweetSearch()
                ->showMetrics()
                ->addFilterOnKeywordOrPhrase([
                    'Dune'
                ], \Noweh\TwitterApi\Enum\Operators::and)
                ->showUserDetails()
                ->performRequest()
            ;
    
            return $this->json([
                'message' => 'Test controller',
                'path' => 'src/Controller/SpotifyController.php',
                'result' => "sssssssss"
            ]);
        }
    }
    
    ?>
    

    The error : image

    opened by DestroyCom 5
  • Cannot connect with OAuth 2.0

    Cannot connect with OAuth 2.0

    Hello, I've been previously using your code to use the twitter API and it was working great. But I recently had to create a new account and I cannot manage to connect using the API with it. I noticed from the twitter developper portal I could not change the read / write settings and OAuth 1.0 settings any more. Now I only have the option to enable OAuth 2.0 :

    image

    I did so and tried to use your library anyway but I keep getting a forbiden response (either 403 or 401). I also tried to replace the 'consumer key' and 'consumer secret' with the 'client ID' and 'client secret' that are provided with OAuth 2.0 but it does not work either.

    Here is the king of message I get from my script :

    Fatal error: Uncaught Exception: "{\n "title": "Unauthorized",\n "type": "about:blank",\n "status": 401,\n "detail": "Unauthorized"\n}" in /home/~/vendor/noweh/twitter-api-v2-php/src/AbstractController.php:143

    Is it something that I'm doing wrong or does the library not support the OAuth 2.0 ?

    Thanks for your help !

    opened by JPsyart 1
  • Support for fetch tweet by Id

    Support for fetch tweet by Id

    Sample and Test included.

    Use GET method and param array with id=>123456

    Also, small change in postData handling to overcome Twitter error with empty postData.

    opened by anttiryt 0
  • EOF, composer.json setting and PHPUnit fixtures

    EOF, composer.json setting and PHPUnit fixtures

    Changed log

    • Let some files have end of line.
    • Set prefer-stable to be true on composer.json file.
    • According to the official PHPUnit doc, it should be protected function setUp method.
    opened by peter279k 0
  • get access_token

    get access_token

    Is there a ready way to get both? Need to apply: client_credentials 'access_token' => 'ACCESS_TOKEN', 'access_token_secret' => 'ACCESS_TOKEN_SECRET'

    thx

    opened by 104979058 2
  • Improvement Request: Replace query string parameters concatenation with something like http_build_query for better extendability.

    Improvement Request: Replace query string parameters concatenation with something like http_build_query for better extendability.

    Hi,

    I would like to thank you for developing this library.

    I am planning to extend it to add some additional features. While exploring existing code a suggestion came to my mind that current way of constructing query string using string concatenation should be replaced with something like http_build_query so that we can easily replace a parameter value in children class if needed.

    Thanks

    opened by saqibsarwar 1
Releases(2.2.2)
Owner
Julien SCHMITT
Julien SCHMITT
A PHP class for querying the Twitter API and rendering tweets as an HTML list

TweetPHP A PHP class for querying the Twitter API and rendering tweets as an HTML list. Features Works with Twitter API v1.1 Tweets are cached to avoi

Jonathan Nicol 151 Nov 25, 2022
Twitter REST API for PHP 5.3+

README The Wid'op Twitter REST library is a modern PHP 5.3+ API allowing you to easily interact with Twitter 1.1. In order to sign your request with t

Wid'op 24 Aug 10, 2020
Twitter API for Laravel 5.5+, 6.x, 7.x & 8.x

Twitter for PHP Twitter API for Laravel 6.x, 7.x, 8.x (and new versions as they are released). Also supports other frameworks via PHP-DI (or feel free

null 879 Dec 17, 2022
The library provides convenient access to the Epoint.az API from applications written in the PHP language.

Tural/Epoint library/SDK The library provides convenient access to the Epoint.az API from applications written in the PHP language. It includes a pre-

Tural 5 Jan 20, 2022
The library provides convenient access to the Epoint.az API from applications written in the PHP language.

Tural/Epoint library/SDK The library provides convenient access to the Epoint.az API from applications written in the PHP language. It includes a pre-

Tural 5 Jan 20, 2022
Access your Slack Team's API through PHP objects.

Slack API library Access your Slack Team's API through PHP objects. Documentation Getting started - Before you use this library, you need to generate

Cas 77 Oct 7, 2021
Get the twitter user authentication keys

TwitterLogin Get the twitter user authentication keys Step 1: Go to Twitter Developer Portal and Apply to create app. Step 2: Go to app setting and en

Mohammad Zarchi 1 Nov 21, 2021
HTTP Requestor: Package for a client request that supports you to make an external service request easily and with fast usage.

HttpRequestor from Patienceman HTTP Requestor: Package for a client request that supports you to make an external service request easily and with fast

Manirabona Patience 2 Aug 26, 2022
Easy to install email tracker with gui and telegram api bot with date device & ip tracking,

mail-php-tracking-with-gui ?? Simple mail tracking program that uses php, html, telegram bot, and a gui The gui The gui lets you create specific links

null 7 Dec 20, 2022
This package is a simple API laravel wrapper for Pokemontcg with a sleek Model design for API routes and authentication.

This package is a simple API laravel wrapper for Pokemontcg with a sleek Model design for API routes and authentication.

Daniel Henze 3 Aug 29, 2022
A simple PHP API to make working with SharePoint lists easy.

PHP SharePoint Lists API The PHP SharePoint Lists API is designed to make working with SharePoint Lists in PHP a less painful developer experience. Ra

Carl Saggs 170 Dec 10, 2022
A tool for sending fast and managed messages to Telegram bot users

???? HiToAll A tool for sending fast and managed messages to Telegram bot users About In some telegram bots programmed with php language, if there are

Matin Najafi 3 Oct 3, 2021
Toxiproxy PHP Client - Toxiproxy makes it easy and trivial to test network conditions, for example low-bandwidth and high-latency situations

Toxiproxy makes it easy and trivial to test network conditions, for example low-bandwidth and high-latency situations. toxiproxy-php-client includes everything needed to get started with configuring Toxiproxy upstream connection and listen endpoints.

Adrian Parker 29 Jun 24, 2022
Nexmo REST API client for PHP. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.

Client Library for PHP Support Notice This library and it's associated packages, nexmo/client and nexmo/client-core have transitioned into a "Maintena

Nexmo 75 Sep 23, 2022
OpenAI API Client is a component-oriented, extensible client library for the OpenAI API. It's designed to be faster and more memory efficient than traditional PHP libraries.

OpenAI API Client in PHP (community-maintained) This library is a component-oriented, extensible client library for the OpenAI API. It's designed to b

Mounir R'Quiba 6 Jun 14, 2023
A simple API documentation package for Laravel using OpenAPI and Redoc

Laravel Redoc Easily publish your API documentation using your OpenAPI document in your Laravel Application. Installation You can install this package

Steve McDougall 15 Dec 27, 2022
TeleBot - Easy way to create Telegram-bots in PHP. Rich Laravel support out of the box.

TeleBot is a PHP library for telegram bots development. Rich Laravel support out of the box. Has an easy, clean, and extendable way to handle telegram Updates.

WeStacks 206 Jan 6, 2023
The Smart-ID PHP client can be used for easy integration of the Smart-ID solution to information systems or e-services

Smart-ID PHP client Introduction The Smart-ID PHP client can be used for easy integration of the Smart-ID solution to information systems or e-service

SK ID Solutions 16 Oct 23, 2022
DigitalOcean API v2 client for Symfony and API Platform

DigitalOcean Bundle for Symfony and API Platform DunglasDigitalOceanBundle allows using the DigitalOcean API from your Symfony and API Platform projec

Kévin Dunglas 25 Jul 27, 2022