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

Twitter API Tools: Common library files

by Adam Green on February 11, 2014

in 140dev Source Code,Twitter API Tools

The collection of API tools will only need a few library files: db_lib.php, oauth_lib.php, and Matt Harris’ tmhOAuth library. These are all packaged together in a convenient zip file, ready for download. Here’s a summary of what you’ll find inside, and then we’ll look at the library code:

  • cacert.pem – SSL certificate used by tmhOAuth library.
  • config.php – Settings for database and OAuth connections.
  • db_lib.php – My standard database library of functions for MySQL.
  • license.txt – MIT license for my code, which is all open source, of course.
  • oauth_lib.php – Simple library for creating an OAuth connection with tmhOAuth.
  • test_db.php – Test for database connection code.
  • test_oauth.php – Test for OAuth connection code.
  • tmhOAuth.php – Matt Harris’ OAuth library.

config.php

<?php

// Database values for db_lib.php
$db_host = 'localhost';
$db_user = '*****';
$db_password = '*****';
$db_name = '*****';

// MySQL time zone setting to normalize dates
$time_zone = 'America/New_York';

// OAuth tokens for oauth_lib.php
$consumer_key = '*****';
$consumer_secret = '*****';
$user_token = '*****';
$user_secret = '*****';

?>

db_lib.php

<?php 
// General purpose database library for use by 140dev Twitter API tools 
// Copyright (c) 2014 Adam Green. All rights reserved.  
// Contact info: http://140dev.com, @140dev, adam@140dev.com 
// Released as open source under MIT license 
class db {   
  public $dbh;   
  public $error;   
  public $error_msg;          

  // Create a database connection for use by all functions in this class   
  function __construct() {     
    require('config.php');          
    if($this->dbh = mysqli_connect($db_host, 
      $db_user, $db_password, $db_name)) { 

      // Set every possible option to utf-8
      mysqli_query($this->dbh, 'SET NAMES "utf8"');
      mysqli_query($this->dbh, 'SET CHARACTER SET "utf8"');
      mysqli_query($this->dbh, 'SET character_set_results = "utf8",' .
        'character_set_client = "utf8", character_set_connection = "utf8",' .
        'character_set_database = "utf8", character_set_server = "utf8"');
    } else {
      // Log an error if the connection fails
      $this->error = true;
      $this->error_msg = 'Unable to connect to DB';
      $this->log_error('__construct','attempted connection to ' . $db_name);
    }

    date_default_timezone_set($time_zone);
  }

  // Call this after each DB request to test for and log errors
  // Supply the calling function name and query, so they can be logged
  private function error_test($function,$query) {

    // Record the last error state in the object, 
    // so code using objects of this class can read it
    if ($this->error_msg = mysqli_error($this->dbh)) {
        $this->log_error($function,$query);
        $this->error = true;
    } else {
        $this->error = false;
    }
    return $this->error;
  }

  // Write any errors into a text log
  // Include the date, calling script, function called, and query
  private function log_error($function,$query) {
    $fp = fopen('error_log.txt','a');
    fwrite($fp, date(DATE_RFC822) . ' | ' . 
      $_SERVER["SCRIPT_NAME"] . ' -> ' . $function . 
      ' | ' . $this->error_msg . ' | ' . $query . "\n");
    fclose($fp); 
  }

  // Create a standard data format for insertion of PHP dates into MySQL
  public function date($php_date) {
    return date('Y-m-d H:i:s', strtotime($php_date));	
  }

  // All text added to the DB should be cleaned with mysqli_real_escape_string
  // to block attempted SQL insertion exploits
  public function escape($str) {
    return mysqli_real_escape_string($this->dbh,$str);
  }

  // Test to see if a specific field value is already in the DB
  // Return false if no, true if yes
  public function in_table($table,$where) {
    $query = 'SELECT * FROM ' . $table . 
      ' WHERE ' . $where;
    $result = mysqli_query($this->dbh,$query);
    $this->error_test('in_table',$query); 
    return mysqli_num_rows($result) > 0;
  }

  // Perform a generic select and return a pointer to the result
  public function select($query) {
    $result = mysqli_query( $this->dbh, $query );
    $this->error_test("select",$query);
    return $result;
  }

  // Add a row to any table
  public function insert($table,$field_values) {
    $query = 'INSERT INTO ' . $table . ' SET ' . $field_values;
    mysqli_query($this->dbh,$query);
    $this->error_test('insert',$query);
  }

  // Update any row that matches a WHERE clause
  public function update($table,$field_values,$where) {
    $query = 'UPDATE ' . $table . ' SET ' . $field_values . 
      ' WHERE ' . $where;
    mysqli_query($this->dbh,$query);
   $this->error_test('update',$query);
  }  
}  
?>

oauth_lib.php

<?php
// General purpose OAuth library for use by 140dev Twitter API tools
// Copyright (c) 2014 Adam Green. All rights reserved. 
// Contact info: http://140dev.com, @140dev, adam@140dev.com
// Released as open source under MIT license

// Create an OAuth connection
function get_connection() {

	// Get OAuth tokens for engagement account
	require('config.php');
	require('tmhOAuth.php');
	
	// Create the connection
	// The OAuth tokens are kept in config.php
	$connection = new tmhOAuth(array(
		  'consumer_key'    => $consumer_key,
		  'consumer_secret' => $consumer_secret,
		  'user_token'      => $user_token,
		  'user_secret'     => $user_secret
	));
			
	return $connection;
}

?>

To get started with these tools, just download the package of libraries, unzip it, and copy the files to a web accesible directory on your server. The next post will cover installation and testing.

Leave a Comment

Previous post:

Next post: