Working with Twitter API's using Laravel

Till now we saw how we can work with on different components of Laravel framework in our previous tutorials. Let's get into some real coding part using Laravel framework. Twitter provide access to lots of data using different Twitter API's with their method signatures in their developer community website. Let's see how we can use twitter api's to post a tweet, getting a user timeline of tweets or getting tweets based on a hashtag.

Let's go deeper into laravel developer community, we don't need to write lot of code for this because there were already few packages in the community to handle dealing with Twitter API using Laravel. The one that really good was by https://twitter.com/thujohn. If we simply follow the steps provided in his documentation, we will be super fast in interacting with the Twitter API!. let's check it out.

Create a Twitter Application

First step to work with Twitter API, we need to create a twitter application at https://apps.twitter.com/. Don't worry it's really easy to do, just sign in with your twitter account and fill in the requested information. The main thing why we are creating this application in twitter that we need an API Key, API secret, Access token and Access token secret. Don't share these things with anyone. Let's see steps in creating twitter application and once application registration successful, we need to generate access token like in third screen shot below.

Step 1

Now twitter application added successfully. We have to generate access token using the button provided under Keys and Access Tokens.

Now we successfully had all the keys and access token required from twitter application. Let's go into what we have to don on Laravel framework for this.

Laravel Twitter Package Installation

Now let's create a new project using laravel artisan command with name twitter by assuming you have already installed composer. If not please check our previous tutorials about how to install laravel.

composer create laravel/laravel twitter

Now we have to get copy of thujohn's package using composer by including in composer.json file like below.

Once after we added the package under required items in composer.json file. We just have to save and need to update composer using composer update command. So that it can fetch all the dependencies for that package.

After this we have to configure our providers and array aliases in app/config/app.php file like below.

Generate a Twitter config file

Now we can easily generate a config file using the artisan command php artisan config:publish thjohn/twitter and it will generate a config file at app/config/packages/thujohn/twitter/config.php location. It looks like below one

Note that we have to list the above keys from our twitter application here to gain access to Twitter API's.

Post a Tweet

Now all of the setup work done. We can take our new laravel twitter application to access different Twitter API's to do some real actions. First let's see how we can post a tweet from here onto our twitter profile.

Author

Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

About Us

We develop custom web apps with architecture that is carefully engineered
and tested for optimum use of Memcache to balance the read/write activity for the lowest
possible CPU usage on the server. We use the best programming languages and Frameworks
for the app, such as Laravel (PHP), Angular JS (Javascript) or RoR (Ruby).