phonghaw2/laravel-x-api-v2

A small Laravel package for X API.

v1.0.0 2025-02-22 11:23 UTC

This package is auto-updated.

Last update: 2025-06-22 12:13:18 UTC


README

MIT Licensed Total Downloads

Installation

For Laravel ^6.0|^7.0|^8.0

This package can be installed through Composer.

composer require phonghaw2/laravel-x-api-v2

Optionally, you can publish the config file of this package with this command:

php artisan vendor:publish --provider="Phonghaw2\X\XServiceProvider"

Config

The following config file will be published in config/x.php

return [
    'api_base_uri' => 'https://api.twitter.com/2/',

    // The unique identifier for the Twitter account.
    // Account ID of the Twitter user
    'app_id' => env('X_APP_ID', 1),

    // Your Twitter API consumer key (public key), obtained from the Twitter Developer Portal.
    // This key is used to identify your app when making API requests. (Consumer API key)
    'api_key' => env('X_APP_KEY', 'CONSUMER_KEY'),

    // Your Twitter API consumer secret, also obtained from the Twitter Developer Portal.
    'api_key_secret' => env('X_APP_KEY_SECRET', 'CONSUMER_SECRET'),

    // The bearer token, used for OAuth 2.0 authentication.
    // It is used in the HTTP headers when making requests to Twitter's API without the need for user authentication.
    'bearer_token' => env('X_BEARER_TOKEN', 'BEARER_TOKEN'),

    // Access token, used for user authentication and authorization for Twitter API access on behalf of the user.
    // You must request this token after getting user consent.
    'access_token' => env('X_ACCESS_TOKEN', 'ACCESS_TOKEN'),

    // Access token secret, paired with the access token to authenticate and authorize API calls.
    'access_token_secret' => env('X_ACCESS_TOKEN_SECRET', 'ACCESS_TOKEN_SECRET'),
];

Usage

Here are a few examples of the provided methods:

TweetLookup

use Twitter;
// Post lookup by Post IDs
Twitter::tweetLookup()
->setQueryParams([
    'ids' => 111111111,
    'max_results' => 5, // Must be a number between 1 and 100 (default: 100)
    'pagination_token' => '5qym3iwo3naekslszn59lxy1d9nmc6q'
])
->lookupPost();

// Creation of a Post
Twitter::tweetLookup()
->setPostData([
    'media' => [
        "media_ids": [
            "11111"
        ],
        "tagged_user_ids": [
            "2222"
        ]
    ],
])
->create()

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.