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 →