Category Archives: Twitter

A Simple Way to Get User Timelines with Twitter API version 1.1 & PHP

A simple way to get user timelines in PHP is to use “The first PHP Library to support OAuth for Twitter’s REST API” by Abraham Williams. The library supports version 1.1 of the Twitter API.

Here is a sample PHP code with the library to get most recent tweets posted by @TwitterEng (the official account for Twitter Engineering).
Continue reading

Twitter Search using the Twitter API v1.1 & PHP

I’ll introduce how to run a Twitter search, customize a query and an advanced technique, search & “auto follow”, using the Twitter API and PHP. Example codes in this post work with the version 1.1 of Twitter API. As usual, I’ll use the twitteroauth library by Abraham Williams to make an implementation easy: https://github.com/abraham/twitteroauth.

How to Run a Twitter Search

Code

<?php
require_once 'lib/twitteroauth.php';

define('CONSUMER_KEY', 'your_consumer_key');
define('CONSUMER_SECRET', 'your_consumer_secret');
define('ACCESS_TOKEN', 'your_access_token');
define('ACCESS_TOKEN_SECRET', 'your_access_token_secret');

function search(array $query)
{
  $toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
  return $toa->get('search/tweets', $query);
}

$query = array(
  "q" => "happy birthday",
);
 
$results = search($query);
 
foreach ($results->statuses as $result) {
  echo $result->user->screen_name . ": " . $result->text . "\n";
}

Continue reading

Twitter API v1.1 – check if username exists

Here is a PHP code snippet to check if username (screen name) exists, by the Twitter API with twitteroauth library by Abraham Williams: https://github.com/abraham/twitteroauth.

Code Snippet – check if username exists

<?php
require_once 'lib/twitteroauth.php';

define('CONSUMER_KEY', 'your_consumer_key');
define('CONSUMER_SECRET', 'your_consumer_secret');
define('ACCESS_TOKEN', 'your_access_token');
define('ACCESS_TOKEN_SECRET', 'your_access_token_secret');

function find_users(array $users)
{
    $founds = array();

    $toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

    // Up to 100 users per request.
    $userAry = array_slice($users, 0, 100);

    // Init with "not found" for all users.
    foreach ($userAry as $user) {
        $founds[$user] = false;
    }

    // Find existing users by "screen_name".
    $userObjs= $toa->post('users/lookup', array('screen_name' => implode(',', $userAry)));

    // Set "found" for existing users.
    foreach ($userObjs as $userObj) {
        $founds[$userObj->screen_name] = true;
    }

    return $founds;
}

Continue reading

How to Auto-Follow on Twitter by Twitter API v1.1 & Ruby

The following code is a ruby version of “auto-follow” script introduced in the previous post: How to Auto Follow on Twitter by Twitter API & PHP.

Whole Code for Twitter Auto-Follow

#!/usr/bin/ruby

require 'rubygems'
require 'twitter'

USERNAME = 'your_twitter_username'
CONSUMER_KEY = 'your_consumer_key'
CONSUMER_SECRET = 'your_consumer_secret'
OAUTH_TOKEN = 'your_oauth_token'
OAUTH_TOKEN_SECRET = 'your_oauth_token'

client = Twitter::Client.new(
  :consumer_key => CONSUMER_KEY,
  :consumer_secret => CONSUMER_SECRET,
  :oauth_token => OAUTH_TOKEN,
  :oauth_token_secret => OAUTH_TOKEN_SECRET
)

follower_ids = []
client.follower_ids(USERNAME).each do |id|
  follower_ids.push(id)
end

friend_ids = []
client.friend_ids(USERNAME).each do |id|
  friend_ids.push(id)
end

client.follow(follower_ids - friend_ids)

Continue reading

How to Auto Follow on Twitter by Twitter API v1.1 & PHP

If you want to follow all of your Twitter Followers, you can automate it with Twitter API & PHP. I’ll introduce a simple implementation with the twitteroauth library by Abraham Williams:

The code in this post works with the version 1.1 of Twitter API.

The followings are sample applications, which tweet top 10 hourly Google trends, with the auto-follow function that I’ll describe in this post:

Whole Code for Twitter Auto-Follow Function

An auto-follow function can be written in simple form with the twitteroauth library as follows:
Continue reading