Advertisement
MathWellan

api.twitter.com/1.1/direct_messages.json

Oct 2nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <?php
  2.  
  3. class TwitterBot
  4. {
  5.     protected $url_update = 'https://api.twitter.com/1.1/statuses/update.json';
  6.     protected $url_follow = 'https://api.twitter.com/1.1/friendships/create.json'; // Added
  7.     protected $url_dm = 'https://api.twitter.com/1.1/direct_messages.json'; // Added
  8.     protected $url_search = 'https://api.twitter.com/1.1/search/tweets.json?q=%s&result_type=recent&count=50';
  9.     protected $url_verify = 'https://api.twitter.com/1.1/account/verify_credentials.json';
  10.     protected $url_token = 'https://twitter.com/oauth/request_token';
  11.     protected $url_token_access = 'https://twitter.com/oauth/access_token';
  12.     protected $url_auth = 'http://twitter.com/oauth/authorize';
  13.     private $oauth;
  14.    
  15.     private $tweets = array();
  16.     private $screenName;
  17.    
  18.     public function __construct($key, $secret)
  19.     {
  20.         $this->oauth = new OAuth($key, $secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
  21.         $this->oauth->disableSSLChecks();
  22.     }
  23.    
  24.     public function setToken($token, $secret)
  25.     {
  26.         $this->oauth->setToken($token, $secret);
  27.     }
  28.    
  29.     public function send($str)
  30.     {
  31.         $this->oauth->fetch($this->url_update, array( 'status' => $str), OAUTH_HTTP_METHOD_POST);
  32.     }
  33.    
  34.     public function search($terms,$regex,$type)
  35.     {
  36.         $this->tweets[] = array('terms' => $terms,'regex' => $regex,'type' => $type);
  37.     }
  38.  
  39.     public function follow()
  40.     {
  41.         if ($this->verifyAccountWorks())
  42.         {
  43.             foreach ($this->tweets as $key => $t)
  44.             {
  45.                 $this->oauth->fetch(sprintf($this->url_search, urlencode($t['terms'][0])));
  46.                 $search_var = json_decode($this->oauth->getLastResponse());
  47.                 if($search_var)
  48.                 {
  49.                     $i = 0;
  50.                     foreach ($search_var->statuses as $tweet)
  51.                     {
  52.                         if($tweet->is_quote_status) // Si le tweet est un rt, on ne prend pas
  53.                         {
  54.                             continue;
  55.                         }
  56.                        
  57.                         $name = $tweet->user->screen_name;
  58.                         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 .
  59.                                 '/status/'.$tweet->id.'" target="_blank">'.$tweet->text.'</a><br /> ';
  60.                        
  61.                         $this->oauth->fetch($this->url_follow, array('user_id' => $tweet->user->id_str, 'follow' => true), OAUTH_HTTP_METHOD_POST); // Follow du compte $tweet
  62.                        
  63.                         // How to :
  64.                        
  65.                         // RT : $this->oauth->fetch($this->url_retweet . $tweet->id_str . '.json');
  66.                         // LIKE : $this->oauth->fetch($this->url_favorite, array('id' => $tweet->id_str));
  67.                        
  68.                         // En rajoutant :
  69.                        
  70.                         //protected $url_retweet = 'https://api.twitter.com/1.1/statuses/retweet/';
  71.                         //protected $url_favorite = 'https://api.twitter.com/1.1/favorites/create.json';
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.     }
  77.    
  78.     public function messages()
  79.     {
  80.         $since_id = $this->getSinceId();
  81.         $max_id = $since_id;
  82.        
  83.         if ($this->verifyAccountWorks())
  84.         {
  85.             $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
  86.             echo $req;
  87.            
  88.             $dms = json_decode($req);
  89.             foreach ($dms as $dm)
  90.             {
  91.                
  92.             }
  93.         }
  94.     }
  95.    
  96.     public function getSinceId($file='since_id')
  97.     {
  98.         $since_id = @file_get_contents($file);
  99.         if(!$since_id)
  100.         {
  101.             $since_id = 0;
  102.         }
  103.         return $since_id;
  104.     }
  105.  
  106.     public function setSinceId($max_id=null,$file='since_id')
  107.     {
  108.         file_put_contents($file, $max_id);
  109.     }
  110.    
  111.     private function verifyAccountWorks()
  112.     {
  113.         try
  114.         {
  115.             $this->oauth->fetch($this->url_verify, array(), OAUTH_HTTP_METHOD_GET);
  116.             $response = json_decode($this->oauth->getLastResponse());
  117.             $this->screenName = $response->screen_name;
  118.             return true;
  119.         }
  120.         catch(Exception $ex)
  121.         {
  122.             return false;
  123.         }
  124.     }
  125. }
  126.  
  127. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement