Create your own twitter bot in php

Create your own twitter bot in php. Twitter is the biggest social media website after facebook. If you can utilize the facilities and infrastructure twitter for promoting your website or online sales, thousands of dollars would be easy you get.

To help us manage the twitter account, dev.twitter as the site developer has provided a twitter API that can be used easily.

In my previous article about Script auto tweet with Imacros and Tweetdeck for Beginner, which allows us to perform an automatic tweet with assistance of iMacros, this time I will discuss how to create twitter bot with php. Twitter bot with php can be custom fit to desire. We will learn is basic use twitter API to perform automatic tweet on our twitter account / make a twitter bot to auto-tweet.

 Twitteroauth class to create twitter bot 

We don’t need bother to make a PHP class to communicate with the Twitter API, because a powerful developer named Abraham Williams has provided a PHP class named twitteroauth. Please download the php class as a material to create a twitter bot ( download here ).

 Get your free API Key, API secret, access token and access token secret to create twitter bot. 

In order twitteroauth class can run well, we have to create twitter API key. How to Create a Twitter API Key

  1. You have a twitter account
  2. Please login to your twitter account.
  3. Visit https://apps.twitter.com/
  4. Click the create app
    create new app - create your own twitter bot
  5. Click API Keys Tab
    click api key tabs - create your own twitter bot
  6. Modify application permissions
    modify app permissions - create your own twitter botmodify app permission 2 - create your own twitter bot
  7. Generate Token Key
    generate token key - create your own twitter bot
  8. Refresh. You have completed the initial steps to create twitter bot
    api key and access token - create your own twitter bot

Creating Class in PHP that allows us to call Twitteroauth Class to connect the twitter’s API

class tweet_bot
{
    function oauth()
    {
        require_once("twitteroauth/twitteroauth.php");
        $con = new TwitterOAuth($this->api_key, $this->api_secret, $this->access_token, $this->access_token_secret);
        return $con;
    }
    function tweet($text)
    {
        $con = $this->oauth();
        $status = $con->post('statuses/update', array('status' => $text));
        return $status;
    }
    function setKey($api_key,$api_secret,$access_token,$access_token_secret)
    {
        $this->api_key = $api_key;
        $this->api_secret = $api_secret;
        $this->access_token = $access_token;
        $this->access_token_secret = $access_token_secret;
    }
}

How to use the twitter_bot class to do activities tweet / twitter status updates.

    $api_key=’ Your Twitter API Key’ ;
    $api_secret=’ Your Twitter API Secret’ ;
    $access_token =’ Your Twitter Access Token’ ;
    $access_token_key=’ Your Twitter Access Token Key’ ;
    $tweet= new tweet_bot;
    $tweet->setKey($api_key, $api_secret,$access_token , $access_token_key);
    $result = $tweet->tweet('Hello this is my first tweet bot');
    Print_r($result);

Another time I will explain the functions of the twitter`s API, in addition to a tweet / update status. And I will make the tutorial make a wordpress plugin to automatically update twitter status. So how to create a twitter bot with PHP, if there are any questions please ask via comment below.

* Update Mei, 1 2015

Hello guys, if you need update of twitteroauth class you can download from twitteroauth class download.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

9 Comments

 Add your comment
  1. Been trying to find something like this for ages. Unfortunately, like all the others I’ve found, it doesn’t work. There is no twitteroauth/twitteroauth.php in the package you were referring to. Is this out of date?

  2. bro how to create this app. we need hosting ? or how can u pls explain.
    there is no index file also

  3. FYI: You don’t have a “mention” class, so use the “tweet” class if you’re trying this out. Also check instructions for twitteroauth. It’s using “autoload.php” to load classes etc. now.

  4. what do we put the callback url as? and where do we put the second php script?

  5. Fatal error: Call to undefined method tweet_bot::mention()

    Why?

    • sory, i have fixed the code

      $api_key=’ Your Twitter API Key’ ;
      $api_secret=’ Your Twitter API Secret’ ;
      $access_token =’ Your Twitter Access Token’ ;
      $access_token_key=’ Your Twitter Access Token Key’ ;
      $tweet= new tweet_bot;
      $tweet->setKey($api_key, $api_secret,$access_token , $access_token_key);
      $result = $tweet->tweet('Hello this is my first tweet bot');
      Print_r($result);
      

      thanks for correcting

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: