Advertisement
MathWellan

TwitterBot.php

Mar 3rd, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.56 KB | None | 0 0
  1. <?php
  2.  
  3. require('OAuth.php');
  4.  
  5. class TwitterBot
  6. {
  7.     protected $url_update = 'https://api.twitter.com/1.1/statuses/update.json';
  8.     protected $url_follow = 'https://api.twitter.com/1.1/friendships/create.json';
  9.     protected $url_search = 'https://api.twitter.com/1.1/search/tweets.json?q=%s&result_type=recent&count=50';
  10.     protected $url_verify = 'https://api.twitter.com/1.1/account/verify_credentials.json';
  11.     protected $url_token = 'https://twitter.com/oauth/request_token';
  12.     protected $url_token_access = 'https://twitter.com/oauth/access_token';
  13.     protected $url_auth = 'http://twitter.com/oauth/authorize';
  14.     private $oauth;
  15.    
  16.     private $tweets = array();
  17.     private $screenName;
  18.    
  19.     public function __construct($key, $secret)
  20.     {
  21.         $this->oauth = new OAuth($key, $secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI); // Ligne de l'erreur
  22.         $this->oauth->disableSSLChecks();
  23.     }
  24.    
  25.     public function setToken($token, $secret)
  26.     {
  27.         $this->oauth->setToken($token, $secret);
  28.     }
  29.    
  30.     public function send($str)
  31.     {
  32.         $this->oauth->fetch($this->url_update, array( 'status' => $str), OAUTH_HTTP_METHOD_POST);
  33.     }
  34.    
  35.     public function search($terms,$regex,$type)
  36.     {
  37.         $this->tweets[] = array('terms' => $terms,'regex' => $regex,'type' => $type);
  38.     }
  39.  
  40.     public function follow()
  41.     {
  42.         if ($this->verifyAccountWorks())
  43.         {
  44.             foreach ($this->tweets as $key => $t)
  45.             {
  46.                 $this->oauth->fetch(sprintf($this->url_search, urlencode($t['terms'][0])));
  47.                 $search_var = json_decode($this->oauth->getLastResponse());
  48.                 if($search_var)
  49.                 {
  50.                     $i = 0;
  51.                     foreach ($search_var->statuses as $tweet)
  52.                     {
  53.                         if($tweet->is_quote_status) // Si le tweet est un rt, on ne prend pas
  54.                         {
  55.                             continue;
  56.                         }
  57.                        
  58.                         $name = $tweet->user->screen_name;
  59.                         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 .
  60.                                 '/status/'.$tweet->id.'" target="_blank">'.$tweet->text.'</a><br /> ';
  61.                        
  62.                         $this->oauth->fetch($this->url_follow, array('user_id' => $tweet->user->id_str, 'follow' => true), OAUTH_HTTP_METHOD_POST); // Follow du compte $tweet
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.     }
  68.    
  69.     private function verifyAccountWorks()
  70.     {
  71.         try
  72.         {
  73.             $this->oauth->fetch($this->url_verify, array(), OAUTH_HTTP_METHOD_GET);
  74.             $response = json_decode($this->oauth->getLastResponse());
  75.             $this->screenName = $response->screen_name;
  76.             return true;
  77.         }
  78.         catch(Exception $ex)
  79.         {
  80.             return false;
  81.         }
  82.     }
  83. }
  84.  
  85. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement