code, howto

How to create a basic Twitter App in PHP

Requirements:

  • PHP (>=5.4.0) installed on your platform of choice
  • A registered Twitter account*
  • Ease with or at least knowledge of using the command-line tools for your operating system

*In order to create apps on your Twitter account, you need to verify it using a mobile phone number. For that, you can use your own phone number or, if you have a Google account, create one using Google Voice.

Naming your App

After logging into your Twitter account, proceed to apps.twitter.com. This is where you will register your app, get your API information, and control how your application works.

Twitter_NewApp

Click on the “Create New App” button to start the process of creating a new app and registering it with Twitter.

Twitter_CreateApp

For each required field, “Name”, “Description” and “Website”, make sure you fill out the information.

Note, too, that the “Name” will be how this application is known to other services. While you can using something simple like “TestingApp” for this example, it might be worthwhile to find a name you like and is not already in use for more advanced projects.

Twitter_Agree

 

Before you can finalize creating a Twitter application, you must agree to the Developer Agreement.

Take the time right now to read through it and become aware of how Twitter expects an application to act and what rights you have as a developer through this agreement.

Once done, click on “Create your Twitter application”.

Twitter_AppScreen

With your application now created, you can change the default permissions from “Read-only” (the application can only read your tweets) to “Read and Write” (the application can read and write tweets).

Changing Permissions

To do that, click on the “Permissions” tab.

Twitter_Permissions

 

From the choices, select “Read and Write” and then click on the “Update Settings” button.

Now that the permissions have been changed and your application will be allowed write tweets in the future, we need to find the API key information and create an access token.

Click on the “Keys and Access Tokens” tab.

Creating an Access Token

The “Keys and Access Tokens” tab contains your Consumer Key, Consumer Secret, and other private information. As noted in its description, none of those details should ever be public.

Twitter_CustomerScret

 

While Twitter will generate your Consumer Key and Secret pairs for you, it does not automatically create an Access Token for your application. To do that, scroll down the page to find that section.

Twitter_AccessToken

 

To generate an Access Token at your current permission levels (“Read and Write”) for your application, click on the “Create my access token” button.

Twitter_AccessTokenGranted

Once generated, you should now have a Consumer Key, Consumer Secret, Access Token, and Access Token Secret. All four will be needed to use the basic Twitter App successfully.

Starting with PHP

Before moving to the next section in this guide, I suggest you go ahead and create a new folder for testing the Twitter App. That way, you won’t potentially disrupt any other processes or PHP projects. (This is generally a best practice.)

There are three different ways to install the library.

Installing the TwitterOAuth library via Composer

Navigate to the folder you created for the project.

Once there, create (or update) your composer.json file with the following:

"require": {
    "php": ">=5.4.0",
    "abraham/twitteroauth": "0.5.0"
}

Installing the TwitterOAuth library via Git

Navigate to the folder you created for the project.

Once there, type the following and press Enter:

git clone https://github.com/abraham/twitteroauth.git

Manually Installing the TwitterOauth library

Download the library from Github (direct link).

Extract the folder into the one you created before.

Testing your configuration

In the same folder your created moments ago, create a new file called “twitterBot.php” and copy the following code into it.

[gist https://gist.github.com/videlais/3b9adce1379f8b0acce6]

Within the configuration section of the code, be sure fill in each of the four items it needs: Consumer Key, Consumer Secret, Access Token, and Access Token Secret from your application settings.

Once done, save the changes.

To test the script and your configuration, run it with the following:

php twitterBot.php

(Alternatively, if using a web server, post the file somewhere and then have the server run it.)

If everything went successfully, you should now have a new tweet in your feed. It should look something like the following, but matching your own Twitter account.

Twitter_PostedTweet

Congratulations! You have now created a very basic Twitter App capable of posting tweets from your PHP code.

Where to go from here

Now, obviously, having a single tweet does not a Twitter App make. It needs something more than the ability to post just a single string of “I’m posting a tweet!” over and over.

For that, consider looking at the TwitterAOuth documentation for details on its API.