Twitter: simple favorite’ing bot on PHP

Recently I found out that lot of twitter bots are favorite’ing and retweetting you statuses. I decided to create my own to play around a little.

Simple search for PHP library, showed me that there are many of these here Twitter Libraries.

I selected J7mbo/twitter-api-php and played around a little.

Here is what I got:

ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "ACCESS_TOKEN",
    'oauth_access_token_secret' => "ACCESS_TOKEN_SECRET",
    'consumer_key' => "API_KEY",
    'consumer_secret' => "API_KEY_SECRET"
);

$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=#kosher+AND+#healthy';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest();

$result = json_decode($response);
foreach ($result->statuses as $ind=>$val)
{
    $url="https://api.twitter.com/1.1/favorites/create.json";
    $postfields = array("id"=>$val->id);
    $requestMethod = 'POST';

    $twitter = new TwitterAPIExchange($settings);
    $response =  $twitter->buildOauth($url, $requestMethod)
        ->setPostfields($postfields)
        ->performRequest();

    var_dump(json_decode($response));

    
}

This bot just searches for twits with hashtags #kosher and #healthy and favorites them. As a result, returns the twit you just favorited.

Leave a Reply

%d bloggers like this: