Listen WebSocket with php - php

I want to get instant cryptocurrency prices on Binance with the help of Websocket. I can do this with javascript as follows. However, this is not useful for me as javascript works on the client side. I want to do this with php to run on the server. I found a few examples in my research, but they were not the solution for me.
Below is a javascript example and I want to learn how to do it in php.
var socket = new WebSocket("wss://stream.binance.com:9443/ws/btcusdt#ticker");
socket.onmessage = function (event) {
var data = JSON.parse(event.data);
console.log(data);
}

I have two classes you can use to connect to the websocket server from php
The sources are at https://github.com/napengam/phpWebSocketServer/tree/master/phpClient
<?php
include __DIR__ . "/../phpClient/websocketCore.php";
class websocketPie extends websocketCore {
public $uuid, $connected = false, $chunkSize = 6 * 1024;
//private $socketMaster;
function __construct($Address, $Port = '', $app = '/', $uu = '') {
if (parent::__construct($Address, $Port, $app, $uu) == false) {
return;
}
$respo = $this->readSocket();
echo var_dump(json_decode($respo));
}
}
$x = new websocketPie("wss://stream.binance.com","9443","/ws/btcusdt#ticker");
The output of above script below:
F:\xampp-htdocs\phpWebSocketServer\websocketExtern\websocketPie_1.php:18:
object(stdClass)[2]
public 'e' => string '24hrTicker' (length=10)
public 'E' => int 1626263895576
public 's' => string 'BTCUSDT' (length=7)
public 'p' => string '-623.48000000' (length=13)
public 'P' => string '-1.885' (length=6)
public 'w' => string '32342.75143871' (length=14)
public 'x' => string '33074.69000000' (length=14)
public 'c' => string '32451.21000000' (length=14)
public 'Q' => string '0.00356600' (length=10)
public 'b' => string '32451.21000000' (length=14)
public 'B' => string '0.00534800' (length=10)
public 'a' => string '32451.22000000' (length=14)
public 'A' => string '0.40745100' (length=10)
public 'o' => string '33074.69000000' (length=14)
public 'h' => string '33078.98000000' (length=14)
public 'l' => string '31550.00000000' (length=14)
public 'v' => string '51376.85373100' (length=14)
public 'q' => string '1661668809.92454695' (length=19)
public 'O' => int 1626177495575
public 'C' => int 1626263895575
public 'F' => int 954365122
public 'L' => int 955537825
public 'n' => int 1172704

Related

Get first array from multiples endpoints async request

I have the guzzle configured which currently uses getAsync with 4 urls promise. The return of this data comes with 2 arrays, of this format below.
My question, how can I separate into two variables, the first being just the first array? In this example, it would be the entire array of sizes 3154 and in the other variable, the entire array of sizes 10297
Guzzle
$requests = [
getenv('apiSavingNew'),
getenv('apiSavingOld'),
];
$promises = (function () use ($requests) {
$client = new Client([
'verify' => false
]);
foreach ($requests as $request) {
yield $client->getAsync($request);
}
})();
$eachPromise = new EachPromise($promises, [
'concurrency' => 2,
'fulfilled' => function (Response $response) {
if ($response->getStatusCode() == 200) {
$request = json_decode($response->getBody());
$firstRequest = // first array here
$secondRequest = // second array here
}
},
'rejected' => function (RequestException $e) {
echo $e->getMessage();
}
]);
$eachPromise->promise()->wait();
Return guzzle promise
array (size=3154)
0 =>
object(stdClass)[11532]
public 'id' => string '57a64bb0-1c6a-11ec-bfd3-173b9227de8c' (length=36)
public 'createdAt' => string '2021-09-23T12:32:40.427Z' (length=24)
public 'data' => string '2021-09-22T00:00:00.000Z' (length=24)
public 'dataFim' => string '2021-10-22T00:00:00.000Z' (length=24)
public 'valor' => string '0.30120' (length=7)
public 'serieTemporalId' => string 'a43978f1-7fd4-4550-9907-106474e64ee4' (length=36)
public 'acumuladoAno' => null
public 'acumulado12Meses' => null
1 =>
object(stdClass)[11539]
public 'id' => string '57a49e00-1c6a-11ec-bfd3-173b9227de8c' (length=36)
public 'createdAt' => string '2021-09-23T12:32:40.416Z' (length=24)
public 'data' => string '2021-09-21T00:00:00.000Z' (length=24)
public 'dataFim' => string '2021-10-21T00:00:00.000Z' (length=24)
public 'valor' => string '0.30120' (length=7)
public 'serieTemporalId' => string 'a43978f1-7fd4-4550-9907-106474e64ee4' (length=36)
public 'acumuladoAno' => null
public 'acumulado12Meses' => null
more elements...
array (size=10297)
0 =>
object(stdClass)[11545]
public 'id' => string '54f70a30-1c6a-11ec-bfd3-173b9227de8c' (length=36)
public 'createdAt' => string '2021-09-23T12:32:35.923Z' (length=24)
public 'data' => string '2021-09-22T00:00:00.000Z' (length=24)
public 'dataFim' => string '2021-10-22T00:00:00.000Z' (length=24)
public 'valor' => string '0.50000' (length=7)
public 'serieTemporalId' => string 'ec940ca2-7da8-4a75-ae7b-d90244797b65' (length=36)
public 'acumuladoAno' => null
public 'acumulado12Meses' => null
1 =>
object(stdClass)[11557]
public 'id' => string '54f3fcf0-1c6a-11ec-bfd3-173b9227de8c' (length=36)
public 'createdAt' => string '2021-09-23T12:32:35.903Z' (length=24)
public 'data' => string '2021-09-21T00:00:00.000Z' (length=24)
public 'dataFim' => string '2021-10-21T00:00:00.000Z' (length=24)
public 'valor' => string '0.50000' (length=7)
public 'serieTemporalId' => string 'ec940ca2-7da8-4a75-ae7b-d90244797b65' (length=36)
public 'acumuladoAno' => null
public 'acumulado12Meses' => null
more elements...
I didnot understood completely what your requirement is? but still I hope that this might help.
You can add the second argument to function in fulfilled as $index and add it in a separate variable for $results empty array.
$results = [];
$requests = [
getenv('apiSavingNew'),
getenv('apiSavingOld'),
];
$promises = (function () use ($requests) {
$client = new Client([
'verify' => false
]);
foreach ($requests as $request) {
yield $client->getAsync($request);
}
})();
$eachPromise = new EachPromise($promises, [
'concurrency' => 2,
'fulfilled' => function (Response $response, $index) {
if ($response->getStatusCode() == 200) {
//$request = json_decode($response->getBody());
$results[$index] = json_decode($response->getBody(), true);
}
},
'rejected' => function (RequestException $e, $index) {
echo $e->getMessage();
}
]);
$eachPromise->promise()->wait();

PHP display nested Object with array of other Object as 1 nested array

I have big Object with protected properties and a property can be an array of other Objects. My goal is to print this entire Object as a single nested array. So I need to convert the object to an array.
I've tried doing:
$result = (array) $object;
But this converts only the highest lever object to an array and it messes up my protected properties names with weird question mark signs.
I've also tried something like this but this simply returns an empty array:
$result= json_decode(json_encode($object), true);
Here is what my object looks like:
object(Handling\Model\SearchBooking\Booking)[133]
protected 'jabooknr' => string '018024709' (length=9)
protected 'jitsbooknr' => string '' (length=9)
protected 'status' => string 'Y' (length=1)
protected 'platform' => int 4
protected 'agentid' => string '' (length=6)
protected 'paymentInfo' => null
protected 'transports' =>
array (size=2)
0 =>
object(Handling\Model\SearchBooking\Transport)[145]
protected 'depdate' =>
object(DateTime)[146]
public 'date' => string '2016-12-06 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
protected 'carriercode' => string 'TB' (length=2)
protected 'carriernumber' => string '2067' (length=4)
protected 'brochure' => string '' (length=6)
protected 'pax' =>
array (size=2)
0 =>
object(Handling\Model\SearchBooking\Pax)[147]
protected 'id' => int 1
protected 'title' => string 'MRS' (length=3)
protected 'firstname' => string 'MA' (length=7)
protected 'name' => string 'BEN' (length=5)
protected 'age' => int 58
protected 'luggage' => int 20
protected 'handLuggage' => null
1 =>
object(Handling\Model\SearchBooking\Pax)[148]
protected 'id' => int 2
protected 'title' => string 'MR' (length=2)
protected 'firstname' => string 'P' (length=6)
protected 'name' => string 'FT' (length=4)
protected 'age' => int 60
protected 'luggage' => int 20
protected 'handLuggage' => null
protected 'departureAirport' => string 'BRU' (length=3)
protected 'arrivalAirport' => string 'AGP' (length=3)
1 =>
object(Handling\Model\SearchBooking\Transport)[149]
protected 'depdate' =>
object(DateTime)[150]
public 'date' => string '2016-12-13 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
protected 'carriercode' => string 'TB' (length=2)
protected 'carriernumber' => string '2068' (length=4)
protected 'brochure' => string '' (length=6)
protected 'pax' =>
array (size=2)
0 =>
object(Handling\Model\SearchBooking\Pax)[151]
protected 'id' => int 1
protected 'title' => string 'MRS' (length=3)
protected 'firstname' => string 'MANE' (length=7)
protected 'name' => string 'BN' (length=5)
protected 'age' => int 58
protected 'luggage' => int 20
protected 'handLuggage' => null
1 =>
object(Handling\Model\SearchBooking\Pax)[152]
protected 'id' => int 2
protected 'title' => string 'MR' (length=2)
protected 'firstname' => string 'PIRE' (length=6)
protected 'name' => string 'FYT' (length=4)
protected 'age' => int 60
protected 'luggage' => int 20
protected 'handLuggage' => null
protected 'departureAirport' => string 'AGP' (length=3)
protected 'arrivalAirport' => string 'BRU' (length=3)
protected 'extraLuggage' => null
EDIT
I have a method in my class where I "find" the result that looks like this:
public function findBooking()
{
//here happens a bunch of logic to get the right result
var_dump($object); exit; // this is the result that is show above
return $object;
}
There are a few issues, that make this difficult.
Property visibility, (private, protected) can cause issues when trying to read them outside of the class, proper. This is expected behavior as that's the point to not use public.
Classes are different. They are well defined and we know them ahead of time, but they are too diverse to account of all property names, at least not with a lot of wasted effort. Not to mention defining them "hard coding" would bite you later as it would make it difficult to maintain. For example if one of the packages does an update and you have coded the property names in you may have issues if they change them. On top of this given that these properties are not part of the classes Public "API" but instead part of the internals, it would not be unreasonable for them to change.
Properties can contain a mix of data types, including other classes or objects. This can make it challenging to handle.
Classes are part of other packages/frameworks and editing them is not practical, this restricts us to working outside of these classes.
So given these difficulties I would recommend using reflection to access the protected properties. Reflection allows you to inspect the definition of classes (and other stuff).
function jsonSerialize($obj){
return json_encode(toArray($obj));
}
function toArray($obj){
$R = new ReflectionObject($obj);
$proerties = $R->getProperties();
$data = [];
foreach($proerties as $k => $v){
$v->setAccessible(true);
$property = $v->getName();
$value = $v->getValue($obj);
if(!is_object($value)){
$data[$property] = $value;
}else if( is_a($obj,'\\DateTime')){
//if its a descendant of Datetime, get a formatted date.
// you can add other special case classes in this way
$data[$property] = $value->format('Y-m-d H:i:s');
}else{
$data[$property] = toArray($value); //call recursively
}
}
return $data;
}
So assume we have these classes
class foo{
private $bar; //private nested object
public function __construct(){
$this->bar = new bar();
}
}
class bar{
private $something = 'hello';
}
$obj = new foo;
echo jsonSerialize($obj);
See it in a sandbox here
Outputs:
{"bar":{"something":"hello"}}
Also of note is we have a special consideration for the DateTime class. Instead of getting all the properties of this we just want the date (probably) formatted in some standard way. So by using is_a() (I'm old school) we can tell if the Object $value has a given class as an ancestor of it. Then we just do our formatting.
There are probably a few special cases like this, so I wanted to mention how to handle them.
Though it is an old query, most answers are not easy to follow. So I tried to simplify the code for this specific question.
The cleaner way to get JSON objects is by implementing the JsonSerializable interface.
class Booking implements JsonSerializable
{
protected $jabooknr;
protected $platform;
//Other attributes ....
//Array of tronsport
protected $transports;
protected $extraLuggage;
public function jsonSerialize()
{
return [
'jabooknr'=> $this->jabooknr,
'platform'=> $this->platform,
'transports' => [json_encode($this->transports)
],
'$extraLuggage' => $this->extraLuggage
];
}
public function __construct($jabooknr, $platform){
$this->jabooknr = $jabooknr;
$this->platform = $platform;
$this->transports=[new Transport()];
}
}
class Transport implements JsonSerializable{
protected $carriercode;
protected $carriernumber;
//Array of Pax
protected $pax ;
public function jsonSerialize()
{
return [
'carriercode'=> $this->carriercode,
'carriernumber'=> $this->carriernumber
];
}
}
$booking = new Booking('018024709',25);
echo json_encode($booking);

find out a class variable's defined scope (from within the class)

Given:
class myClass extends \Phalcon\Mvc\Model
{
public $a;
protected $b;
private $c;
}
How can I test that $a is public, $b is protected, and $c is private from within myClass?
You can use ReflectionProperty -
class myClass
{
public $a;
protected $b;
private $c;
}
$obj = new myClass();
$reflect_a = new ReflectionProperty(get_class($obj), 'a');
$reflect_c = new ReflectionProperty(get_class($obj), 'c');
var_dump($reflect_a->isProtected());
var_dump($reflect_c->isPrivate());
Depending on the result you can hide or show them.
For your needs you can use use Models Meta-Data. You can get the attributes of the model within the model:
<?php
/**
* Posts Model
*
*/
class Posts extends \Phalcon\Mvc\Model
{
public $id;
public $users_id;
public $categories_id;
public $title;
public $slug;
public $content;
public $number_views;
public $number_replies;
public $votes_up;
public $votes_down;
public $sticked;
public $modified_at;
public $created_at;
public $edited_at;
public $status;
public $locked;
public $deleted;
public $accepted_answer;
private $foo_bar;
}
Somewhere in the controller:
var_dump($this->modelsMetadata->getAttributes(new Posts()));die;
Output:
array (size=18)
0 => string 'id' (length=2)
1 => string 'users_id' (length=8)
2 => string 'categories_id' (length=13)
3 => string 'title' (length=5)
4 => string 'slug' (length=4)
5 => string 'content' (length=7)
6 => string 'number_views' (length=12)
7 => string 'number_replies' (length=14)
8 => string 'votes_up' (length=8)
9 => string 'votes_down' (length=10)
10 => string 'sticked' (length=7)
11 => string 'created_at' (length=10)
12 => string 'modified_at' (length=11)
13 => string 'edited_at' (length=9)
14 => string 'status' (length=6)
15 => string 'locked' (length=6)
16 => string 'deleted' (length=7)
17 => string 'accepted_answer' (length=15)
Also you can create an model's method:
public function getAttributes()
{
$metaData = $this->getModelsMetaData();
return $metaData->getAttributes($this);
}
\Phalcon\Mvc\Model\MetaData::getAttributes Returns table attributes names - fields of table.
Also PHP's get_class_vars() returns an array of all properties visible in the current scope. In your case it should return all public properties.

Ajax between two PHP scripts

I'm tring to perform an AJAX request between 2 PHP scripts, one of them just returns (echoes) some data in the form of a json string:
[{'foo':'bar'}]
And the other one just makes a simple http request to it:
$c = new http\Client;
$r = new http\Client\Request('GET', 'http://example.com/script.php');
$c->enqueue($r,
function(http\Client\Response $r) {
// Do something with the Json here.
return true;
})->send();
But I just can't figure out how to get that Json string from the Response object. PECL's documentation doesn't help at all:
<?php
$request = new http\Client\Request("GET",
"http://example.com",
["User-Agent"=>"My Client/0.1"]
);
$request->setOptions(["timeout"=>1]);
$client = new http\Client;
$client->enqueue($request)->send();
// pop the last retrieved response
$response = $client->getResponse();
printf("%s returned '%s' (%d)\n",
$response->getTransferInfo("effective_url"),
$response->getInfo(),
$response->getResponseCode()
);
?>
This just yields:
http://example.com/ returned 'HTTP/1.1 200 OK' (200)
But nothing more. How can I do this?
Update:
This is a dump of the Response object using var_dump.
object(http\Client\Response)[6]
protected 'type' => int 2
protected 'body' =>
object(http\Message\Body)[5]
protected 'requestMethod' => string '' (length=0)
protected 'requestUrl' => string '' (length=0)
protected 'responseStatus' => string 'OK' (length=2)
protected 'responseCode' => int 200
protected 'httpVersion' => string '1.1' (length=3)
protected 'headers' =>
array (size=7)
'Server' => string 'nginx/1.4.7' (length=11)
'Date' => string 'Sat, 20 Dec 2014 23:20:07 GMT' (length=29)
'Content-Type' => string 'text/html' (length=9)
'Connection' => string 'keep-alive' (length=10)
'X-Powered-By' => string 'PHP/5.5.19' (length=10)
'X-Original-Transfer-Encoding' => string 'chunked' (length=7)
'Content-Length' => int 1695
protected 'parentMessage' =>
object(http\Client\Request)[3]
protected 'type' => int 1
protected 'body' =>
object(http\Message\Body)[8]
protected 'requestMethod' => string 'GET' (length=3)
protected 'requestUrl' => string 'http://localhost/battleturnrest/work.GetTop50.php' (length=49)
protected 'responseStatus' => string '' (length=0)
protected 'responseCode' => int 0
protected 'httpVersion' => string '1.1' (length=3)
protected 'headers' =>
array (size=0)
empty
protected 'parentMessage' => null
protected 'options' => null
protected 'transferInfo' =>
object(stdClass)[7]
public 'effective_url' => string 'http://localhost/battleturnrest/work.GetTop50.php' (length=49)
public 'response_code' => int 200
public 'total_time' => float 0.01084
public 'namelookup_time' => float 0.000913
public 'connect_time' => float 0.001345
public 'pretransfer_time' => float 0.002432
public 'size_upload' => float 0
public 'size_download' => float 1695
public 'speed_download' => float 156365
public 'speed_upload' => float 0
public 'header_size' => int 180
public 'request_size' => int 135
public 'ssl_verifyresult' => int 0
public 'filetime' => int -1
public 'content_length_download' => float -1
public 'content_length_upload' => float 0
public 'starttransfer_time' => float 0.010685
public 'content_type' => string 'text/html' (length=9)
public 'redirect_time' => float 0
public 'redirect_count' => int 0
public 'connect_code' => int 0
public 'httpauth_avail' => int 0
public 'proxyauth_avail' => int 0
public 'os_errno' => int 111
public 'num_connects' => int 1
public 'ssl_engines' =>
array (size=0)
empty
public 'cookies' =>
array (size=0)
empty
public 'redirect_url' => string '' (length=0)
public 'primary_ip' => string '127.0.0.1' (length=9)
public 'appconnect_time' => float 0
public 'condition_unmet' => int 0
public 'primary_port' => int 80
public 'local_ip' => string '127.0.0.1' (length=9)
public 'local_port' => int 45915
public 'curlcode' => int 0
public 'error' => string '' (length=0)
This is exactly the same call-request scenario. The main.php calls answer.php, and it answers with json.
answer.php
<?php
// the json header
header('Content-Type: application/json');
// parameter "value" from URL http://.../answer.php?value=bar
$value = $_REQUEST["value"];
// returns exactly [{'foo':'bar'}]
echo(json_encode(array(array("foo" => $value))));
?>
main.php
<?php
...
$bar = "bar";
$url = "http://.../answer.php?value=" . $bar;
$arr = json_decode(file_get_contents($url));
...
>
The most important thing is main.php executes line by line and you can not send two or more calls to the answer.php in the same moment.
But if:
main.php
<?php
// the crucial difference: asynchronous calls to the answer.php
class Ask_For_Value extends Thread {
public function __construct($value, $func){
$this->val = $value;
$this->func = $func;
}
function start(){
$arr = json_decode(file_get_contents("http://.../answer.php?value=" . $this->val));
call_user_func($this->func, $arr);
return(0);// boolean "OK"
}
}
// function to process the result
function do_some_thihg_with_the_result(&$array){...}
// prepare the threads
$call1 = new Ask_For_Value("bar_1", "do_some_thihg_with_the_result");
$call2 = new Ask_For_Value("bar_2", "do_some_thihg_with_the_result");
$call3 = new Ask_For_Value("bar_3", "do_some_thihg_with_the_result");
// start the threads
$call1->start();
$call2->start();
$call3->start();
// there is nothing happens, because the threads
// will continue execution asynchronous and
// independent in the "function do_some_thihg_with_the_result()"
?>

return all the array between one hour via php

lets say we do some search useing search api then it retuns something like this in json.
'results' =>
array
0 =>
object(stdClass)[9]
public 'from_user_id_str' => string '57393273' (length=8)
public 'profile_image_url' => string 'http://a1.twimg.com/profile_images/1150792547/49146_1365401218_7951632_q_normal.jpg' (length=83)
public 'created_at' => string 'Mon, 06 Jun 2011 04:01:36 +0000' (length=31)
public 'from_user' => string 'primalokomotif' (length=14)
public 'id_str' => string '77585906684526592' (length=17)
public 'metadata' =>
object(stdClass)[10]
public 'result_type' => string 'recent' (length=6)
public 'to_user_id' => null
public 'text' => string 'RT #detikcom kober siap perkenalkan ajaran klub suami suami takut istri' (length=71)
public 'id' => float 7.7585906684527E+16
public 'from_user_id' => int 57393273
public 'geo' => null
public 'iso_language_code' => string 'id' (length=2)
public 'to_user_id_str' => null
public 'source' => string '<a href="http://mobile.twitter.com" rel="nofollow">Mobile Web</a>' (length=97)
1 =>
object(stdClass)[11]
public 'from_user_id_str' => string '278060132' (length=9)
public 'profile_image_url' => string 'http://a1.twimg.com/profile_images/1355377589/296218442_normal.jpg' (length=66)
public 'created_at' => string 'Mon, 06 Jun 2011 04:01:34 +0000' (length=31)
public 'from_user' => string 'kaka_kecil' (length=10)
public 'id_str' => string '77585897637412864' (length=17)
public 'metadata' =>
object(stdClass)[12]
public 'result_type' => string 'recent' (length=6)
public 'to_user_id' => null
public 'text' => string 'RT #detikcom: 'Milan Tak Pernah Inginkan Hamsik' http://de.tk/dP7Cs via #detiksport' (length=83)
public 'id' => float 7.7585897637413E+16
public 'from_user_id' => int 278060132
public 'geo' => null
public 'iso_language_code' => string 'id' (length=2)
public 'to_user_id_str' => null
public 'source' => string '<a href="http://ubersocial.com" rel="nofollow">ÃœberSocial</a>' (length=94)
2 =>
object(stdClass)[13]
public 'from_user_id_str' => string '185984810' (length=9)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/1368466468/a_normal.jpg' (length=58)
public 'created_at' => string 'Mon, 06 Jun 2011 04:01:31 +0000' (length=31)
public 'from_user' => string 'tiayeeeaah' (length=10)
public 'id_str' => string '77585887143276544' (length=17)
public 'metadata' =>
object(stdClass)[14]
public 'result_type' => string 'recent' (length=6)
public 'to_user_id' => null
public 'text' => string 'hmm..hmm.. -___-" RT #detikcom: Bandung Siap Perkenalkan Ajaran Klub Istri Taat Suami http://bit.ly/kCcOsG' (length=111)
public 'id' => float 7.7585887143277E+16
public 'from_user_id' => int 185984810
public 'geo' => null
public 'iso_language_code' => string 'id' (length=2)
public 'to_user_id_str' => null
public 'source' => string '<a href="http://seesmic.com/seesmic_desktop/sd2" rel="nofollow">Seesmic Desktop</a>' (length=115)
3 => etc
how can we return arrays that have date less then one hour ? or filter it? maybe something like do a search then returns all the array that have strtotime(created_at) < strtotime("+1 Hours")
*edit
like How can I use the Twitter Search API to return all tweets that match my search query, posted only within the last five seconds?
but there is no answer doing this.
thanks for looking in
Adam Ramadhan
So, you're looking to return only those that have created_at within the past hour? I like to use array_filter() when I can.
function filterWithinHour($obj)
{
return (strtotime($obj->created_at) > strtotime('-1 hour'));
}
$recent = array_filter($results, 'filterWithinHour');
If you ever need need to pass it a certain time (instead of assuming now), you might want to just loop through the array manually rather than mess with trying to pass a time parameter to the callback with a global variable. I hate globals used like that.
The Twitter Search API has a since_id that could help you:
Returns results with an ID greater
than (that is, more recent than) the
specified ID. There are limits to the
number of Tweets which can be accessed
through the API. If the limit of
Tweets has occured since the since_id,
the since_id will be forced to the
oldest ID available.
If you poll every hour, you effectively get the posts of the last hour. If the service is down, you still won't miss any tweets since you could go back several days.

Categories