function tracker_run() { global $tweetQueue; $tweetQueue = new TweetQueue(); $tweetQueue->setoption('replace', false); if (defined('USE_INSERT_DELAYED') && USE_INSERT_DELAYED) { $tweetQueue->setoption('delayed', true); } if (defined('DISABLE_INSERT_IGNORE') && DISABLE_INSERT_IGNORE) { $tweetQueue->setoption('ignore', false); } else { $tweetQueue->setoption('ignore', true); } if (!defined("CAPTURE")) { /* logged to no file in particular, because we don't know which one. this should not happen. */ error_log("tracker_run() called without defining CAPTURE. have you set up config.php ?"); die; } $roles = unserialize(CAPTUREROLES); if (!in_array(CAPTURE, $roles)) { /* incorrect script execution, report back error to user */ error_log("tracker_run() role " . CAPTURE . " is not configured to run"); die; } // log execution environment $phpstring = phpversion() . " in mode " . php_sapi_name() . " with extensions "; $extensions = get_loaded_extensions(); $first = true; foreach ($extensions as $ext) { if ($first) { $first = false; } else { $phpstring .= ','; } $phpstring .= "{$ext}"; } $phpstring .= " (ini file: " . php_ini_loaded_file() . ")"; logit(CAPTURE . ".error.log", "running php version {$phpstring}"); // install the signal handler if (function_exists('pcntl_signal')) { // tick use required as of PHP 4.3.0 declare (ticks=1); // See signal method discussion: // http://darrendev.blogspot.nl/2010/11/php-53-ticks-pcntlsignal.html logit(CAPTURE . ".error.log", "installing term signal handler for this script"); // setup signal handlers pcntl_signal(SIGTERM, "capture_signal_handler_term"); } else { logit(CAPTURE . ".error.log", "your php installation does not support signal handlers. graceful reload will not work"); } // sanity check for geo bins functions if (geophp_sane()) { logit(CAPTURE . ".error.log", "geoPHP library is fully functional"); } elseif (geobinsActive()) { logit(CAPTURE . ".error.log", "refusing to track until geobins are stopped or geo is functional"); exit(1); } else { logit(CAPTURE . ".error.log", "geoPHP functions are not yet available, see documentation for instructions"); } global $ratelimit, $exceeding, $ex_start, $last_insert_id; $ratelimit = 0; // rate limit counter since start of script $exceeding = 0; // are we exceeding the rate limit currently? $ex_start = 0; // time at which rate limit started being exceeded $last_insert_id = -1; global $twitter_consumer_key, $twitter_consumer_secret, $twitter_user_token, $twitter_user_secret, $lastinsert; $pid = getmypid(); logit(CAPTURE . ".error.log", "started script " . CAPTURE . " with pid {$pid}"); $lastinsert = time(); $procfilename = BASE_FILE . "proc/" . CAPTURE . ".procinfo"; if (file_put_contents($procfilename, $pid . "|" . time()) === FALSE) { logit(CAPTURE . ".error.log", "cannot register capture script start time (file \"{$procfilename}\" is not WRITABLE. make sure the proc/ directory exists in your webroot and is writable by the cron user)"); die; } $networkpath = isset($GLOBALS["HOSTROLE"][CAPTURE]) ? $GLOBALS["HOSTROLE"][CAPTURE] : 'https://stream.twitter.com/'; // prepare queries if (CAPTURE == "track") { // check for geolocation bins $locations = geobinsActive() ? getActiveLocationsImploded() : false; // assemble query $querylist = getActivePhrases(); if (empty($querylist) && !geobinsActive()) { logit(CAPTURE . ".error.log", "empty query list, aborting!"); return; } $method = $networkpath . '1.1/statuses/filter.json'; $track = implode(",", $querylist); $params = array(); if (geobinsActive()) { $params['locations'] = $locations; } if (!empty($querylist)) { $params['track'] = $track; } } elseif (CAPTURE == "follow") { $querylist = getActiveUsers(); if (empty($querylist)) { logit(CAPTURE . ".error.log", "empty query list, aborting!"); return; } $method = $networkpath . '1.1/statuses/filter.json'; $params = array("follow" => implode(",", $querylist)); } elseif (CAPTURE == "onepercent") { $method = $networkpath . '1.1/statuses/sample.json'; $params = array('stall_warnings' => 'true'); } logit(CAPTURE . ".error.log", "connecting to API socket"); $tmhOAuth = new tmhOAuth(array('consumer_key' => $twitter_consumer_key, 'consumer_secret' => $twitter_consumer_secret, 'token' => $twitter_user_token, 'secret' => $twitter_user_secret, 'host' => 'stream.twitter.com')); $tmhOAuth->request_settings['headers']['Host'] = 'stream.twitter.com'; if (CAPTURE == "track" || CAPTURE == "follow") { logit(CAPTURE . ".error.log", "connecting - query " . var_export($params, 1)); } elseif (CAPTURE == "onepercent") { logit(CAPTURE . ".error.log", "connecting to sample stream"); } $capturebucket = array(); $tmhOAuth->streaming_request('POST', $method, $params, 'tracker_streamCallback', array('Host' => 'stream.twitter.com')); // output any response we get back AFTER the Stream has stopped -- or it errors logit(CAPTURE . ".error.log", "stream stopped - error " . var_export($tmhOAuth, 1)); logit(CAPTURE . ".error.log", "processing buffer before exit"); processtweets($capturebucket); }
* secret into the place in this code marked with (YOUR_CONSUMER_KEY * and YOUR_CONSUMER_SECRET) * 3) From the application details page copy the access token and access token * secret into the place in this code marked with (A_USER_TOKEN * and A_USER_SECRET) * 4) In a terminal or server type: * php /path/to/here/streaming.php * 5) To stop the Streaming API either press CTRL-C or, in the folder the * script is running from type: * touch STOP * * @author themattharris */ function my_streaming_callback($data, $length, $metrics) { // Twitter sends keep alive's in their streaming API. // when this happens $data will appear empty. // ref: https://dev.twitter.com/docs/streaming-apis/messages#Blank_lines echo $data . PHP_EOL; return file_exists(dirname(__FILE__) . '/STOP'); } require '../tmhOAuth.php'; require '../tmhUtilities.php'; $tmhOAuth = new tmhOAuth(array('consumer_key' => 'YOUR_CONSUMER_KEY', 'consumer_secret' => 'YOUR_CONSUMER_SECRET', 'user_token' => 'A_USER_TOKEN', 'user_secret' => 'A_USER_SECRET')); $method = 'https://stream.twitter.com/1/statuses/filter.json'; // show Tweets which contan the word twitter OR have been geo-tagged within // the bounding box -122.41,37.77,-122.40,37.78 OR are by themattharris $params = array('track' => 'twitter', 'locations' => '-122.41,37.77,-122.40,37.78', 'follow' => '777925'); $tmhOAuth->streaming_request('POST', $method, $params, 'my_streaming_callback'); // output any response we get back AFTER the Stream has stopped -- or it errors tmhUtilities::pr($tmhOAuth);
<?php require dirname(__FILE__) . '/' . './lib/tmhOAuth/tmhOAuth.php'; require dirname(__FILE__) . '/' . './oauth.php'; require dirname(__FILE__) . '/' . './action.php'; $tmhOAuth = new tmhOAuth(array("consumer_key" => $consumer_key, "consumer_secret" => $consumer_secret, "user_token" => $user_token, "user_secret" => $user_secret)); //Tweet受信にfilter_callback関数を呼び出す $method = "https://userstream.twitter.com/1.1/user.json"; $params = array(); $tmhOAuth->streaming_request('POST', $method, $params, 'filter_callback', true); function filter_callback($data, $length, $metrics) { global $tmhOAuth; @($res = json_decode($data)); //各アクションを実行 必要ないものはコメントアウトしましょ /** えんくんのアレ **/ Action::Event_Watch($tmhOAuth, $res); /** スクリーンネームとつぶやきを見る**/ Action::Tweet_Watch($tmhOAuth, $res); /** らこらこらこ~w **/ Action::Lacolacolaco($tmhOAuth, $res, @$your_screen_name); } /*** * [メモ]エラー時のレスポンス * * [response][error] = connect() timed out! * [response][errno] = 28 */ /*** * 流れてくる情報は以下のURLを参照 *