Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require('OAuth.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';
- 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); // Ligne de l'erreur
- $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
- }
- }
- }
- }
- }
- 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