Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class TwitterBot
- {
- protected $url_update = 'https://api.twitter.com/1.1/statuses/update.json';
- protected $url_follow = 'https://api.twitter.com/1.1/friendships/create.json'; // Added
- protected $url_dm = 'https://api.twitter.com/1.1/direct_messages.json'; // Added
- protected $url_search = 'https://api.twitter.com/1.1/search/tweets.json?q=%s&result_type=recent&count=50';
- protected $url_verify = 'https://api.twitter.com/1.1/account/verify_credentials.json';
- protected $url_token = 'https://twitter.com/oauth/request_token';
- protected $url_token_access = 'https://twitter.com/oauth/access_token';
- protected $url_auth = 'http://twitter.com/oauth/authorize';
- private $oauth;
- private $tweets = array();
- private $screenName;
- public function __construct($key, $secret)
- {
- $this->oauth = new OAuth($key, $secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
- $this->oauth->disableSSLChecks();
- }
- public function setToken($token, $secret)
- {
- $this->oauth->setToken($token, $secret);
- }
- public function send($str)
- {
- $this->oauth->fetch($this->url_update, array( 'status' => $str), OAUTH_HTTP_METHOD_POST);
- }
- public function search($terms,$regex,$type)
- {
- $this->tweets[] = array('terms' => $terms,'regex' => $regex,'type' => $type);
- }
- public function follow()
- {
- if ($this->verifyAccountWorks())
- {
- foreach ($this->tweets as $key => $t)
- {
- $this->oauth->fetch(sprintf($this->url_search, urlencode($t['terms'][0])));
- $search_var = json_decode($this->oauth->getLastResponse());
- if($search_var)
- {
- $i = 0;
- foreach ($search_var->statuses as $tweet)
- {
- if($tweet->is_quote_status) // Si le tweet est un rt, on ne prend pas
- {
- continue;
- }
- $name = $tweet->user->screen_name;
- echo '<b><a style="color: red;" href="https://twitter.com/' . $name . '" target="_blank">@' . $name . '</a> :</b> <a style="color: black; text-decoration: none;" href="https://twitter.com/' . $name .
- '/status/'.$tweet->id.'" target="_blank">'.$tweet->text.'</a><br /> ';
- $this->oauth->fetch($this->url_follow, array('user_id' => $tweet->user->id_str, 'follow' => true), OAUTH_HTTP_METHOD_POST); // Follow du compte $tweet
- // How to :
- // RT : $this->oauth->fetch($this->url_retweet . $tweet->id_str . '.json');
- // LIKE : $this->oauth->fetch($this->url_favorite, array('id' => $tweet->id_str));
- // En rajoutant :
- //protected $url_retweet = 'https://api.twitter.com/1.1/statuses/retweet/';
- //protected $url_favorite = 'https://api.twitter.com/1.1/favorites/create.json';
- }
- }
- }
- }
- }
- public function messages()
- {
- $since_id = $this->getSinceId();
- $max_id = $since_id;
- if ($this->verifyAccountWorks())
- {
- $req = $this->oauth->fetch($this->url_dm, $since_id, OAUTH_HTTP_METHOD_GET); // J'ai commencé à mettre le $since_id mais l'erreur ne vient pas de là elle était identique avant cet ajout
- echo $req;
- $dms = json_decode($req);
- foreach ($dms as $dm)
- {
- }
- }
- }
- public function getSinceId($file='since_id')
- {
- $since_id = @file_get_contents($file);
- if(!$since_id)
- {
- $since_id = 0;
- }
- return $since_id;
- }
- public function setSinceId($max_id=null,$file='since_id')
- {
- file_put_contents($file, $max_id);
- }
- private function verifyAccountWorks()
- {
- try
- {
- $this->oauth->fetch($this->url_verify, array(), OAUTH_HTTP_METHOD_GET);
- $response = json_decode($this->oauth->getLastResponse());
- $this->screenName = $response->screen_name;
- return true;
- }
- catch(Exception $ex)
- {
- return false;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement