Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SquareNetMedia's avatar

Laravel and Twitter API

I'm looking at connecting to my twitter profile to post status from my laravel app.

I'm still learning but I'm struggling to be able to connect to the app using the secret keys.

I don't want to use a third party package. Can anyone help or point me to a tutorial

0 likes
5 replies
LaryAI's avatar
Level 58

Certainly! To connect to the Twitter API from your Laravel application without using a third-party package, you'll need to follow these steps:

  1. Create a Twitter Developer Account and App

    • Go to the Twitter Developer portal (https://developer.twitter.com/) and sign in with your Twitter account.
    • Create a new app and obtain your API key, API secret key, Access token, and Access token secret.
  2. Set Up Your Environment

    • Add your Twitter API credentials to your .env file in your Laravel project:
      TWITTER_API_KEY=yourapikey
      TWITTER_API_SECRET_KEY=yoursecretkey
      TWITTER_ACCESS_TOKEN=youraccesstoken
      TWITTER_ACCESS_TOKEN_SECRET=youraccesstokensecret
      
  3. Install Guzzle HTTP Client

    • Since you're not using a third-party package specifically for Twitter, you can use Guzzle to make HTTP requests to the Twitter API.
    • Install Guzzle via Composer:
      composer require guzzlehttp/guzzle
      
  4. Create a Twitter Service

    • You can create a service class in Laravel to handle the communication with the Twitter API.
    • Here's an example of a simple Twitter service class:
<?php

namespace App\Services;

use GuzzleHttp\Client;

class TwitterService
{
    protected $client;
    protected $apiKey;
    protected $apiSecret;
    protected $accessToken;
    protected $accessTokenSecret;

    public function __construct()
    {
        $this->apiKey = env('TWITTER_API_KEY');
        $this->apiSecret = env('TWITTER_API_SECRET_KEY');
        $this->accessToken = env('TWITTER_ACCESS_TOKEN');
        $this->accessTokenSecret = env('TWITTER_ACCESS_TOKEN_SECRET');

        $this->client = new Client([
            'base_uri' => 'https://api.twitter.com/1.1/',
            'auth' => [$this->apiKey, $this->apiSecret, 'oauth']
        ]);
    }

    public function postTweet($status)
    {
        $response = $this->client->post('statuses/update.json', [
            'query' => ['status' => $status]
        ]);

        return json_decode($response->getBody(), true);
    }
}
  1. Use the Twitter Service
    • You can now use this service in your controller to post a status to Twitter:
<?php

namespace App\Http\Controllers;

use App\Services\TwitterService;

class TwitterController extends Controller
{
    protected $twitter;

    public function __construct(TwitterService $twitter)
    {
        $this->twitter = $twitter;
    }

    public function postTweet()
    {
        $status = 'This is a tweet from my Laravel app!';
        $response = $this->twitter->postTweet($status);

        // Handle the response accordingly
    }
}
  1. Handle OAuth
    • The above example assumes you're using OAuth 1.0a for authentication. You'll need to handle the OAuth signing process, which involves creating a signature based on your consumer key, consumer secret, access token, and access token secret.
    • Since this can be complex, you might want to consider using a library like thephpleague/oauth1-client to help with the OAuth process.

Remember that the Twitter API has rate limits and other usage policies that you must adhere to. Also, the code provided is a simplified example and does not handle errors or exceptions that may occur when making requests to the Twitter API.

If you find the OAuth process too complex to implement from scratch, you might reconsider using a third-party package that abstracts away these details, such as atymic/twitter package for Laravel, which can greatly simplify the process.

SquareNetMedia's avatar

Laravel Socialte seems to be for logging into the application with Twitter.

I have a single user site, so the details of 'TWITTER_CLIENT_ID and TWITTER_CLIENT_SECRET belongs to one person.

I dont seem to be able to authenticate the user to be able to use https://api.twitter.com/2/tweets to post a tweet.

martinbean's avatar

Laravel Socialte seems to be for logging into the application with Twitter.

@SquareNetMedia Yes? All “logging in with Twitter is”, is obtaining an OAuth access token for a user in order to perform requests against the Twitter API as that user.

So, you need to obtain an OAuth access token for yourself and persist that token somewhere in order to post statuses—via the Twitter API—as yourself.

Please or to participate in this conversation.