Twitter – Status update with API 1.1 using PHP

Disclaimer

The instructions/steps given below worked for me with several Twitter accounts. As has been a common practice for several years now, my server has a full version of Operating System (RHEL 6.4 in this case) installed. These instructions may very well work for you. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.com, nor its author is responsible for any/all damage – intellectual and/or otherwise.

Pre-requisites

Apart from having a Twitter account and a linux work station, one will need to sign in to developer website and create an application. Once the application is created, one will need to make sure that it’s Access level is set to Read and write (can be changed under Settings tab), and re-create the access token. One will also need to note down Consumer key, Consumer secret, Access token and Access token request – all found under the OAuth tool tab.

 

BASH script

For testing purposes, let us download a text — All’s Well That Ends Well by William Shakespeare — from Project Gutenberg. Let us further sanitize the file by first removing all empty lines from it and then renaming it. PHP script in the following section will randomly select a line from this file for tweeting.

#! /bin/bash
#
# Save this as download_gutenberg.sh
# Set 755 (or at least 700) permissions on it.
# Usage:
# ./download_gutenberg.sh
 
# Downloaded the file
wget http://www.gutenberg.org/cache/epub/1529/pg1529.txt
 
# Convert to UNIX format
dos2unix pg1529.txt
 
# Remove all empty lines
sed -i '/^$/d' pg1529.txt
 
# Rename the file
mv pg1529.txt all_is_well_that_ends_well.txt

 

PHP script

<!--?php 
 
#
# Save this as post2twitter.php
# Usage:
# php post2twitter.php

#
# Variables
$update_api_url   = "https://api.twitter.com/1.1/statuses/update.json";
$tweet_latitude   = "47.1195";  # Latitude of your town
$tweet_longitude  = "-88.5470"; # Longitude of your town

#
# Application speficic tokens
$consumer_key        = "XXXXXXXXXXXXXXXXXXXXXXX";
$consumer_secret     = "XXXXXXXXXXXXXXXXXXXXXXX";
$access_token        = "XXXXXXXXXXXXXXXXXXXXXXX";
$access_token_secret = "XXXXXXXXXXXXXXXXXXXXXXX";
 
# 
# Create a random string to be the tweet (&lt; 140 characters) $filename = "all_is_well_that_ends_well.txt"; $file_contents = file($filename); $random_line = $file_contents[rand(0, count($file_contents) - 1)]; $random_number = rand(); $tweet2post = "Test message # $random_number: $random_line"; $tweet2post = substr($tweet2post, 0, 138); $tweet2post = rawurlencode("$tweet2post"); # # Construct the URL to be passed to cURL $post2twitter_api_url = $update_api_url; $post2twitter_api_url .= "?lat=$tweet_latitude"; $post2twitter_api_url .= "&amp;long=$tweet_longitude"; $post2twitter_api_url .= "&amp;status=$tweet2post"; # # OAuth signature $post2twitter_oauth_hash = ''; $post2twitter_oauth_hash .= 'lat=' . $tweet_latitude; $post2twitter_oauth_hash .= '&amp;long=' . $tweet_longitude; $post2twitter_oauth_hash .= '&amp;oauth_consumer_key=' . $consumer_key; $post2twitter_oauth_hash .= '&amp;oauth_nonce=' . time(); $post2twitter_oauth_hash .= '&amp;oauth_signature_method=HMAC-SHA1'; $post2twitter_oauth_hash .= '&amp;oauth_timestamp=' . time(); $post2twitter_oauth_hash .= '&amp;oauth_token=' . $access_token; $post2twitter_oauth_hash .= '&amp;oauth_version=1.0'; $post2twitter_oauth_hash .= '&amp;status=' . $tweet2post; $post2twitter_base = ''; $post2twitter_base .= 'POST'; $post2twitter_base .= '&amp;'; $post2twitter_base .= rawurlencode("$update_api_url"); $post2twitter_base .= '&amp;'; $post2twitter_base .= rawurlencode($post2twitter_oauth_hash); $post2twitter_key = ''; $post2twitter_key .= rawurlencode("$consumer_secret"); $post2twitter_key .= '&amp;'; $post2twitter_key .= rawurlencode("$access_token_secret"); $post2twitter_signature = base64_encode(hash_hmac('sha1', $post2twitter_base, $post2twitter_key, true)); $post2twitter_signature = rawurlencode($post2twitter_signature); # # cURL Headers $post2twitter_oauth_header = ''; $post2twitter_oauth_header .= 'oauth_consumer_key="' . $consumer_key . '", '; $post2twitter_oauth_header .= 'oauth_nonce="' . time() . '", '; $post2twitter_oauth_header .= 'oauth_signature="' . $post2twitter_signature . '", '; $post2twitter_oauth_header .= 'oauth_signature_method="HMAC-SHA1", '; $post2twitter_oauth_header .= 'oauth_timestamp="' . time() . '", '; $post2twitter_oauth_header .= 'oauth_token="' . $access_token . '", '; $post2twitter_oauth_header .= 'oauth_version="1.0"'; $post2twitter_curl_header = array("Authorization: OAuth {$post2twitter_oauth_header}", 'Expect:'); # # cURL Request $post2twitter_curl_request = curl_init(); curl_setopt($post2twitter_curl_request, CURLOPT_HTTPHEADER, $post2twitter_curl_header); curl_setopt($post2twitter_curl_request, CURLOPT_HEADER, false); curl_setopt($post2twitter_curl_request, CURLOPT_URL, $post2twitter_api_url); curl_setopt($post2twitter_curl_request, CURLOPT_POST, true); curl_setopt($post2twitter_curl_request, CURLOPT_RETURNTRANSFER, true); curl_setopt($post2twitter_curl_request, CURLOPT_SSL_VERIFYPEER, false); $post2twitter_response = curl_exec($post2twitter_curl_request); curl_close($post2twitter_curl_request); ?-->

When this PHP script is executed — either manually or periodically as a cron job — the status is updated in the said user’s timeline. Potential applications are seemingly endless: random quotes from a file, server status (as done here), information extracted from a pre-populated database, and so on.


Thanks be to

Dear friend Jacob Emerick (@jpemeric) for his in-depth and insightful posts — Working with Twitter’s 1.1 API via PHP, OAuth and others — that helped me understand several things, thus paving way for this one.

One Reply to “Twitter – Status update with API 1.1 using PHP”

Comments are closed.