Monday 15 June 2015

php - Filter Stream, ‘track’ works but ‘locations’ doesnt -



php - Filter Stream, ‘track’ works but ‘locations’ doesnt -

new guy, i've been hunting hours reply on no luck far here am.

i'm next mike pultz's streaming illustration here in effort 'locations' stream working. theoretically, should able alter 'track' 'locations' on line 64 or when he's building $data variable inserted in request right? on front end end need insert 2 lng/lat pairs instead of track keywords.

i can stream work 'track' filter on switch 'locations' 401 error. ideas?

i'll paste code here (minus keys)

ctwitter_stream.php

<?php class ctwitter_stream { private $m_oauth_consumer_key; private $m_oauth_consumer_secret; private $m_oauth_token; private $m_oauth_token_secret; private $m_oauth_nonce; private $m_oauth_signature; private $m_oauth_signature_method = 'hmac-sha1'; private $m_oauth_timestamp; private $m_oauth_version = '1.0'; public function __construct() { // // set time limit unlimited // set_time_limit(0); } // // set login details // public function login($_consumer_key, $_consumer_secret, $_token, $_token_secret) { $this->m_oauth_consumer_key = $_consumer_key; $this->m_oauth_consumer_secret = $_consumer_secret; $this->m_oauth_token = $_token; $this->m_oauth_token_secret = $_token_secret; // // generate nonce; we're using random md5() hash here. // $this->m_oauth_nonce = md5(mt_rand()); homecoming true; } // // process tweet object stream // private function process_tweet($_data) { print_r($_data['text']); print("<br /><br />"); homecoming true; } // // main stream manager // public function start($_keywords) { while(1) { $fp = fsockopen("ssl://stream.twitter.com", 443, $errno, $errstr, 30); if (!$fp) { echo "error: twitter stream error: failed open socket"; } else { // // build info , store can length // $data = 'track=' . rawurlencode(implode($_keywords, ',')); // // store current timestamp // $this->m_oauth_timestamp = time(); // // generate base of operations string based on info // $base_string = 'post&' . rawurlencode('https://stream.twitter.com/1.1/statuses/filter.json') . '&' . rawurlencode('oauth_consumer_key=' . $this->m_oauth_consumer_key . '&' . 'oauth_nonce=' . $this->m_oauth_nonce . '&' . 'oauth_signature_method=' . $this->m_oauth_signature_method . '&' . 'oauth_timestamp=' . $this->m_oauth_timestamp . '&' . 'oauth_token=' . $this->m_oauth_token . '&' . 'oauth_version=' . $this->m_oauth_version . '&' . $data); // // generate secret key utilize hash // $secret = rawurlencode($this->m_oauth_consumer_secret) . '&' . rawurlencode($this->m_oauth_token_secret); // // generate signature using hmac-sha1 // // hash_hmac() requires php >= 5.1.2 or pecl hash >= 1.1 // $raw_hash = hash_hmac('sha1', $base_string, $secret, true); // // base64 urlencode raw hash // $this->m_oauth_signature = rawurlencode(base64_encode($raw_hash)); // // build oauth authorization header // $oauth = 'oauth oauth_consumer_key="' . $this->m_oauth_consumer_key . '", ' . 'oauth_nonce="' . $this->m_oauth_nonce . '", ' . 'oauth_signature="' . $this->m_oauth_signature . '", ' . 'oauth_signature_method="' . $this->m_oauth_signature_method . '", ' . 'oauth_timestamp="' . $this->m_oauth_timestamp . '", ' . 'oauth_token="' . $this->m_oauth_token . '", ' . 'oauth_version="' . $this->m_oauth_version . '"'; // // build request // $request = "post /1.1/statuses/filter.json http/1.1\r\n"; $request .= "host: stream.twitter.com\r\n"; $request .= "authorization: " . $oauth . "\r\n"; $request .= "content-length: " . strlen($data) . "\r\n"; $request .= "content-type: application/x-www-form-urlencoded\r\n\r\n"; $request .= $data; print $request; print '<br />'; // // write request // fwrite($fp, $request); // // set non-blocking // stream_set_blocking($fp, 0); while(!feof($fp)) { $read = array($fp); $write = null; $except = null; // // select, waiting 10 minutes tweet; if don't one, // reconnect, because it's possible went wrong. // $res = stream_select($read, $write, $except, 600, 0); if ( ($res == false) || ($res == 0) ) { break; } // // read json object socket // $json = fgets($fp); // // http response code // if (strncmp($json, 'http/1.1', 8) == 0) { $json = trim($json); if ($json != 'http/1.1 200 ok') { echo 'error: ' . $json . "\n"; homecoming false; } } // // if there data, process // if ( ($json !== false) && (strlen($json) > 0) ) { // // decode socket php array // $data = json_decode($json, true); if ($data) { // // process // $this->process_tweet($data); } } } } fclose($fp); sleep(10); } return; } }; ?>

twitter_front.php

<!doctype html> <html> <head> <meta charset="utf-8"> <title>untitled document</title> <?php require 'ctwitter_stream.php'; ?> </head> <body> test <?php $t = new ctwitter_stream(); $t->login("key", "secret", "token", ""); $t->start(array('albuquerque','abq','burque','land of enchantment')); // $t->start(array('-106.77','35.00','-106.43','35.25')); ?> </body> </html>

edited: tag

php twitter twitter-oauth

No comments:

Post a Comment