Guest User

ctwitter_stream modified

a guest
Sep 28th, 2013
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.56 KB | None | 0 0
  1. <?
  2. class ctwitter_stream
  3. {
  4.     private $m_oauth_consumer_key;
  5.     private $m_oauth_consumer_secret;
  6.     private $m_oauth_token;
  7.     private $m_oauth_token_secret;
  8.  
  9.     private $m_oauth_nonce;
  10.     private $m_oauth_signature;
  11.     private $m_oauth_signature_method = 'HMAC-SHA1';
  12.     private $m_oauth_timestamp;
  13.     private $m_oauth_version = '1.0';
  14.  
  15.     public function __construct()
  16.     {
  17.         //
  18.         // set a time limit to unlimited
  19.         //
  20.         set_time_limit(0);
  21.     }
  22.  
  23.     //
  24.     // set the login details
  25.     //
  26.     public function login($_consumer_key, $_consumer_secret, $_token, $_token_secret)
  27.     {
  28.         $this->m_oauth_consumer_key     = $_consumer_key;
  29.         $this->m_oauth_consumer_secret  = $_consumer_secret;
  30.         $this->m_oauth_token            = $_token;
  31.         $this->m_oauth_token_secret     = $_token_secret;
  32.  
  33.         //
  34.         // generate a nonce; we're just using a random md5() hash here.
  35.         //
  36.         $this->m_oauth_nonce = md5(mt_rand());
  37.  
  38.         return true;
  39.     }
  40.  
  41.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42.     // process a tweet object from the stream
  43.     //
  44.     private function process_tweet(array $_data)
  45.     {
  46.        
  47.         $txt=$_data["text"];
  48.         //$txt = json_decode('"'.$text.'"');
  49.  
  50.     //$txt=utf8_decode($txt);
  51.     //$txt=html_entity_decode($txt);
  52.         echo "$txt<br>";
  53.         flush();
  54.  
  55.         return true;
  56.     }
  57.  
  58.     //
  59.     // the main stream manager
  60.     //
  61.     public function start(array $_keywords)
  62.     {
  63.         while(1)
  64.         {
  65.             $fp = fsockopen("ssl://stream.twitter.com", 443, $errno, $errstr, 30);
  66.             if (!$fp)
  67.             {
  68.                 echo "ERROR: Twitter Stream Error: failed to open socket";
  69.             } else
  70.             {
  71.                 //
  72.                 // build the data and store it so we can get a length
  73.                 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74.                 $data = 'count=100';
  75.  
  76.                 //
  77.                 // store the current timestamp
  78.                 //
  79.                 $this->m_oauth_timestamp = time();
  80.  
  81.                 //
  82.                 // generate the base string based on all the data
  83.                 //
  84.                 $base_string = 'GET&' .
  85.                     rawurlencode('https://stream.twitter.com/1.1/statuses/firehose.json') . '&' .
  86.                     rawurlencode('oauth_consumer_key=' . $this->m_oauth_consumer_key . '&' .
  87.                         'oauth_nonce=' . $this->m_oauth_nonce . '&' .
  88.                         'oauth_signature_method=' . $this->m_oauth_signature_method . '&' .
  89.                         'oauth_timestamp=' . $this->m_oauth_timestamp . '&' .
  90.                         'oauth_token=' . $this->m_oauth_token . '&' .
  91.                         'oauth_version=' . $this->m_oauth_version //. '&' . $data
  92.                         );
  93.  
  94.                 //
  95.                 // generate the secret key to use to hash
  96.                 //
  97.                 $secret = rawurlencode($this->m_oauth_consumer_secret) . '&' .
  98.                     rawurlencode($this->m_oauth_token_secret);
  99.  
  100.                 //
  101.                 // generate the signature using HMAC-SHA1
  102.                 //
  103.                 // hash_hmac() requires PHP >= 5.1.2 or PECL hash >= 1.1
  104.                 //
  105.                 $raw_hash = hash_hmac('sha1', $base_string, $secret, true);
  106.  
  107.                 //
  108.                 // base64 then urlencode the raw hash
  109.                 //
  110.                 $this->m_oauth_signature = rawurlencode(base64_encode($raw_hash));
  111.  
  112.                 //
  113.                 // build the OAuth Authorization header
  114.                 //
  115.                 $oauth = 'OAuth oauth_consumer_key="' . $this->m_oauth_consumer_key . '", ' .
  116.                         'oauth_nonce="' . $this->m_oauth_nonce . '", ' .
  117.                         'oauth_signature="' . $this->m_oauth_signature . '", ' .
  118.                         'oauth_signature_method="' . $this->m_oauth_signature_method . '", ' .
  119.                         'oauth_timestamp="' . $this->m_oauth_timestamp . '", ' .
  120.                         'oauth_token="' . $this->m_oauth_token . '", ' .
  121.                         'oauth_version="' . $this->m_oauth_version . '"';
  122.  
  123.                 //
  124.                 // build the request
  125.                 //
  126.                 $request  = "POST /1.1/statuses/filter.json HTTP/1.1\r\n";
  127.                 $request .= "Host: stream.twitter.com\r\n";
  128.                 $request .= "Authorization: " . $oauth . "\r\n";
  129.                 $request .= "Content-Length: " . strlen($data) . "\r\n";
  130.                 $request .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
  131.                 $request .= $data;
  132.  
  133.                
  134.                 $request2  = "GET /1.1/statuses/firehose.json?count=100 HTTP/1.1\r\n";
  135.                 $request2 .= "Host: stream.twitter.com\r\n";
  136.                 $request2 .= "Authorization: " . $oauth . "\r\n";
  137.                 //$request2 .= "Content-Length: " . strlen($data) . "\r\n";
  138.                 //$request2 .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
  139.                 //$request2 .= $data;                
  140.                
  141.                 //
  142.                 // write the request
  143.                 //
  144.                 fwrite($fp, $request2);
  145.  
  146.                 //
  147.                 // set it to non-blocking
  148.                 //
  149.                 stream_set_blocking($fp, 0);
  150.  
  151.                 while(!feof($fp))
  152.                 {
  153.                     $read   = array($fp);
  154.                     $write  = null;
  155.                     $except = null;
  156.  
  157.                     //
  158.                     // select, waiting up to 10 minutes for a tweet; if we don't get one, then
  159.                     // then reconnect, because it's possible something went wrong.
  160.                     //
  161.                     $res = stream_select($read, $write, $except, 600, 0);
  162.                     if ( ($res == false) || ($res == 0) )
  163.                     {
  164.                         break;
  165.                     }
  166.  
  167.                     //
  168.                     // read the JSON object from the socket
  169.                     //
  170.                     $json = fgets($fp);
  171.  
  172.                     //
  173.                     // look for a HTTP response code
  174.                     //
  175.                     if (strncmp($json, 'HTTP/1.1', 8) == 0)
  176.                     {
  177.                         $json = trim($json);
  178.                         if ($json != 'HTTP/1.1 200 OK')
  179.                         {
  180.                             echo 'ERROR: ' . $json . "\n";
  181.                             return false;
  182.                         }
  183.                     }
  184.  
  185.                     //
  186.                     // if there is some data, then process it
  187.                     //
  188.                     if ( ($json !== false) && (strlen($json) > 0) )
  189.                     {
  190.                         //
  191.                         // decode the socket to a PHP array
  192.                         //
  193.                         $data = json_decode($json, true);
  194.                         if ($data)
  195.                         {
  196.                             //
  197.                             // process it
  198.                             //
  199.                             $this->process_tweet($data);
  200.                         }
  201.                     }
  202.                 }
  203.             }
  204.  
  205.             fclose($fp);
  206.             sleep(10);
  207.         }
  208.  
  209.         return;
  210.     }
  211. };
  212.  
  213. ?>
Add Comment
Please, Sign In to add comment