Skip to content

Commit

Permalink
Merge pull request #49 from fennb/chunked
Browse files Browse the repository at this point in the history
Chunked
  • Loading branch information
DarrenCook committed Dec 27, 2013
2 parents df8d1ee + ebf0a5f commit a9b7cac
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 606 deletions.
24 changes: 9 additions & 15 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ See:
* Encourage well-behaved streaming API clients
* Operate independently of PHP extensions (ie: shared memory, PCNTL, etc)

In short:

require_once('Phirehose.php');
class MyStream extends Phirehose
{
public function enqueueStatus($status)
{
print $status;
}
}

$stream = new MyStream('username', 'password');
$stream->consume();


## What this library does do ##
* Handles connection/authentication to the twitter streaming API
* Consumes the stream handing off each status to be enqueued by a method of your choice
Expand All @@ -38,6 +23,15 @@ In short:
* Provide any sort of inter-process communication
* Provide any non-streaming API functionality (ie: user profile info, search, etc)

## How To Use ##

See the example subdirectory for example usage. In each example file you will need to insert your own oauth token/secret, and the key/secret for the Twitter app you have created.

* filter-oauth.php shows how to follow certain keywords.
* sample.php shows how to get a small random sample of all public statuses.
* userstream-alternative.php shows how to get user streams. (All activity for one user.)
* sitestream.php shows to how to get site streams. (All activity for multiple users.)

Please see the wiki for [documentation](https://github.com/fennb/phirehose/wiki/Introduction).

If you have any additional questions, head over to the Phirehose Users group [http://groups.google.com/group/phirehose-users]
Expand Down
62 changes: 62 additions & 0 deletions example/sitestream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
require_once('../lib/OauthPhirehose.php');

/**
* Barebones example of using OauthPhirehose to do site streams.
*
* NOTE: Completely UNTESTED.
*/
class MyUserConsumer extends OauthPhirehose
{
/**
* First response looks like this:
* $data=array('friends'=>array(123,2334,9876));
*
* Each tweet of your friends looks like:
* [id] => 1011234124121
* [text] => (the tweet)
* [user] => array( the user who tweeted )
* [entities] => array ( urls, etc. )
*
* Every 30 seconds we get the keep-alive message, where $status is empty.
*
* When the user adds a friend we get one of these:
* [event] => follow
* [source] => Array( my user )
* [created_at] => Tue May 24 13:02:25 +0000 2011
* [target] => Array (the user now being followed)
*
* @param string $status
*/
public function enqueueStatus($status)
{
/*
* In this simple example, we will just display to STDOUT rather than enqueue.
* NOTE: You should NOT be processing tweets at this point in a real application, instead they
* should be being enqueued and processed asyncronously from the collection process.
*/
$data = json_decode($status, true);
echo date("Y-m-d H:i:s (").strlen($status)."):".print_r($data,true)."\n";
}

}

//These are the application key and secret
//You can create an application, and then get this info, from https://dev.twitter.com/apps
//(They are under OAuth Settings, called "Consumer key" and "Consumer secret")
define('TWITTER_CONSUMER_KEY', 'XXXXXXXXXX');
define('TWITTER_CONSUMER_SECRET', 'XXXXXXXXXX');

//These are the user's token and secret
//You can get this from https://dev.twitter.com/apps, under the "Your access token"
//section for your app.
define('OAUTH_TOKEN', 'XXXXXXXXXX');
define('OAUTH_SECRET', 'XXXXXXXXXX');

// Start streaming
$sc = new MyUserConsumer(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_SITE);
$sc->setFollow(array(
1234, 5678, 901234573 //The user IDs of the twitter accounts to follow. All of
//these users must have given your app permission.
));
$sc->consume();
60 changes: 60 additions & 0 deletions example/userstream-alternative.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
require_once('../lib/OauthPhirehose.php');

/**
* Barebones example of using OauthPhirehose to do user streams.
*
* This shows how to get user streams by just passing Phirehose::METHOD_USER
* as the 3rd parameter to the constructor, instead of using the UserStreamPhirehose
* class.
*/
class MyUserConsumer extends OauthPhirehose
{
/**
* First response looks like this:
* $data=array('friends'=>array(123,2334,9876));
*
* Each tweet of your friends looks like:
* [id] => 1011234124121
* [text] => (the tweet)
* [user] => array( the user who tweeted )
* [entities] => array ( urls, etc. )
*
* Every 30 seconds we get the keep-alive message, where $status is empty.
*
* When the user adds a friend we get one of these:
* [event] => follow
* [source] => Array( my user )
* [created_at] => Tue May 24 13:02:25 +0000 2011
* [target] => Array (the user now being followed)
*
* @param string $status
*/
public function enqueueStatus($status)
{
/*
* In this simple example, we will just display to STDOUT rather than enqueue.
* NOTE: You should NOT be processing tweets at this point in a real application, instead they
* should be being enqueued and processed asyncronously from the collection process.
*/
$data = json_decode($status, true);
echo date("Y-m-d H:i:s (").strlen($status)."):".print_r($data,true)."\n";
}

}

//These are the application key and secret
//You can create an application, and then get this info, from https://dev.twitter.com/apps
//(They are under OAuth Settings, called "Consumer key" and "Consumer secret")
define('TWITTER_CONSUMER_KEY', 'XXXXXXXXXX');
define('TWITTER_CONSUMER_SECRET', 'XXXXXXXXXX');

//These are the user's token and secret
//You can get this from https://dev.twitter.com/apps, under the "Your access token"
//section for your app.
define('OAUTH_TOKEN', 'XXXXXXXXXX');
define('OAUTH_SECRET', 'XXXXXXXXXX');

// Start streaming
$sc = new MyUserConsumer(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_USER);
$sc->consume();
1 change: 0 additions & 1 deletion example/userstream-simple.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
require_once('../lib/Phirehose.php');
require_once('../lib/UserstreamPhirehose.php');

/**
Expand Down
42 changes: 11 additions & 31 deletions lib/OauthPhirehose.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

require_once('Phirehose.php');

/**
*
*
* @internal At time of writing thise overrides getAuthorizationHeader() from the parent class;
* all other functions are helper functions for that.
*/
abstract class OauthPhirehose extends Phirehose
{

Expand Down Expand Up @@ -30,7 +37,7 @@ protected function prepareParameters($method = null, $url = null,
$oauth['oauth_nonce'] = md5(uniqid(rand(), true));
$oauth['oauth_signature_method'] = 'HMAC-SHA1';
$oauth['oauth_timestamp'] = time();
$oauth['oauth_version'] = '1.0';
$oauth['oauth_version'] = '1.0A';
$oauth['oauth_token'] = $this->username;
if (isset($params['oauth_verifier']))
{
Expand Down Expand Up @@ -134,36 +141,9 @@ protected function getOAuthHeader($method, $url, $params = array())
return $oauth;
}

protected function getAuthorizationHeader()
/** Overrides base class function */
protected function getAuthorizationHeader($url,$requestParams)
{
$url = self::URL_BASE . $this->method . '.' . $this->format;
$urlParts = parse_url($url);

// Setup params appropriately
$requestParams = array('delimited' => 'length');

// Setup the language of the stream
if($this->lang) {
$requestParams['language'] = $this->lang;
}

// Filter takes additional parameters
if (count($this->trackWords) > 0)
{
$requestParams['track'] = implode(',', $this->trackWords);
}
if (count($this->followIds) > 0)
{
$requestParams['follow'] = implode(',', $this->followIds);
}
if (count($this->locationBoxes) > 0)
{
$requestParams['locations'] = implode(',', $this->locationBoxes);
}
if (count($this->count) <> 0) {
$requestParams['count'] = $this->count;
}

return $this->getOAuthHeader('POST', $url, $requestParams);
}
}
Loading

0 comments on commit a9b7cac

Please sign in to comment.