Create BOT Auto Update Twitter Status With Image in PHP

Continuing my previous article Create your own twitter bot in php, now I will share about creating a bot to auto update twitter status with image in PHP. Essentially the same, using the API provided by the twitter API and using twitteroauth class.

download bot auto update twitter status with image

BOT Auto tweet with image that will be very powerfull and useful if used on your website or blog with a niche movies, wallpapers.

To update twitter status with image, the original class of abraham twitteroauth doesn`t support full of update status with image (POST statuses / update_with_media). You need to update twitteroauth class, belongs to Abraham to be usable for update twitter status with image. I found twitteroauth class that has been upgraded by tomi-heiskanen , you can immediately download the latest class twiiteroauth at the link below

 Okay, let’s start to make the bot auto update twitter status with image in PHP. 

Make sure you have twitteroath class, to be able use twitteroauth make sure you have the Twitter API too (access token and API Key), I have discussed in my previous article, please read Create your own twitter bot in php for be able to create and use it.

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_with_media($text,$media)
	{
		$con = $this->oauth();
		$image_path=$media ; 	// example my-image.jpg
		$handle = fopen($image_path,'rb');
		$image     = fread($handle,filesize($image_path));
		fclose($handle);
		$params = array(
		'media[]' => "{$image};type=image/jpeg;filename={$image_path}",
		'status'  => "Put you message here"
		);
		$post= $con->post('statuses/update_with_media',$params,true);
		return $post;
	}
	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;
	}
}
$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_with_media(‘Your status’,'your-image.jpg');
print_r($result);?>

You can also use images from the image url in the following way
• Create a temporary folder in your twitter bot folder, and give the name tmpimage
• Change the function tweet_with_media


                function tweet_with_media($text,$media)

                {

                                $con = $this->oauth();

                                $image = file_get_contents($media);

                                $file = basename($media);         

                                file_put_contents('tmpimage/'.$file, $image);

                                $image_path='tmpimage/'.$file;

                                $handle = fopen($image_path,'rb');

                                $image     = fread($handle,filesize($image_path));

                                fclose($handle);

                                $params = array(

                                'media[]' => "{$image};type=image/jpeg;filename={$image_path}",

                                'status'  => "Put you message here"

                                );

                                $post= $con->post('statuses/update_with_media',$params,true);

                                return $post;

                }

 How to use auto tweet with image from image url 

$status = “your tweet here”;
$url-image=”your image url”; // example : http://learnimage.com/myimage.jpg
$tweet->tweet_with_media($status,$url-image);
print_r($result);

Done, If you need a plugin wordpress plugin that might be useful for you, please visit my another article about wordpress plugin here wordpress plugin.

Please developed themselves bot auto update twitter status with image as you wish, next time I will share how to create a plugin, auto update twitter status with image in wordpress`s plugin.

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.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: