Adam Green
Twitter API Consultant
adam@140dev.com
781-879-2960
@140dev

Twitter API Tools: Installing the common files

by Adam Green on February 11, 2014

in 140dev Source Code,Twitter API Tools

The previous post listed the set of common library files needed to use the Twitter API tools. Installing this package to get ready to use the tools is pretty easy:

  • Download the zipped set of files and unzip them on your local machine.
  • Create a web accessible directory on your server. You’ll want this to be available to the web so you can test the web versions of the tools.
  • Upload the unzipped set of files into this new directory.
  • Edit config.php to include the MySQL connection options for a database. You can use an existing database, but I’d recommend creating a new one. There is no preset schema yet. Each tool will include the SQL statements needed to create the tables it will need.
  • Edit config.php to include a complete set of OAuth tokens. If you are not sure how to create a set of tokens, you should read my free ebook on the subject. You’ll want the app that provides the tokens to be set for read, write, and direct message permissions to be able to run all the tools that are built.

That’s it. You are now ready to run the tools. As of writing this post there are no tools, but I’ll be cranking them out pretty fast. Hopefully, others will contribute tools as well, once they are familiar with the coding style I’m trying to achieve.

To make sure you have everything configured correctly, the set of common files includes two simple test programs. You should run them now to be sure. If you have any problems, this Google Group is ready for tech support questions.

test_db.php

<?php

// Connect to database using db_lib.php
require('db_lib.php');
$db = new db();

// Test connection with a simple query
$result = $db->select("SHOW DATABASES");
while($row = mysqli_fetch_assoc($result)) {
	print $row['Database'] . "\n";
}

?>

Test_db.php’s purpose is to make sure you can make a connection to MySQL. You can run it at the command line of your telnet or SSH client with:

php test_db.php

It will output a list of databases available to your MySQL user login. If it fails, you’ll see an error message from db_lib.php, and a copy of the MySQL error should be added to error_log.txt in the same directory as the code.

test_oauth.php

<?php
	
// Connect to API with OAuth
require('oauth_lib.php');
$connection = get_connection();

// Get the account profile for this user
$connection->request('GET', $connection->url('1.1/users/show'), 
	array('screen_name' => '140dev'));

print "Response code: " . $connection->response['code'] . "\n";

print "Response data: " . $connection->response['response'];

?>

Test_oauth.php is a basic proof that you can connect to the API through the OAuth tokens you placed in config.php. It uses the /users/show request, which will return the details of my @140dev account profile. If it fails, you’ll get the API’s error code and message. Running this test can again be done with your telnet or SSH client with:

php test_oauth.php

The most likely causes for an OAuth failure are: incorrect OAuth tokens, server clock that is more than 5 minutes incorrect, and incomplete installation of OAuth files. It is odd how often this happens, but if you get an error code of 0, it almost always means that cacert.pem was not uploaded with the rest of the files. Make sure you copied the complete set of files to your server.

If OAuth is still failing, use the Google Group for support. OAuth can be a pain to get started, but once it is configured correctly, it is reliable.

Leave a Comment

Previous post:

Next post: