Example #1
0
$history = $pubnub->history(array('channel' => $craziness, 'limit' => 1));
test(count($history), 1, 'History With 3 Items Limit 1');
test($history[0], $craziness, 'History Message is Crazy');
## ---------------------------------------------------------------------------
## Test Subscribe
## ---------------------------------------------------------------------------
$message = 1234;
## Generate String to Sign
$string_to_sign = implode('/', array($publish_key, $subscribe_key, $secret_key, $channel, $message));
$signature = $secret_key ? md5($string_to_sign) : '0';
$pubsub_url_test = "http://pubsub.pubnub.com/publish/" . $publish_key . "/" . $subscribe_key . "/" . $signature . "/" . $channel . "/0/" . $message;
echo "\nHIT THIS URL to CONTINUE -> {$pubsub_url_test}";
echo "\n\nHit CTRL+C to finish.";
echo "\nYou may continue reloading the URL to keep testing.\n\n";
$pubnub->subscribe(array('channel' => $channel, 'callback' => function ($message) {
    test($message, 1234, 'Subscribe: ' . $message);
    return true;
}));
## ---------------------------------------------------------------------------
## Unit Test Function
## ---------------------------------------------------------------------------
function test($val1, $val2, $name)
{
    if ($val1 == $val2) {
        echo 'PASS: '******'FAIL: ';
    }
    echo "{$name}\n";
}
?>
<?php

require_once 'Pubnub.php';
$publish_key = isset($argv[1]) ? $argv[1] : 'demo';
$subscribe_key = isset($argv[2]) ? $argv[2] : 'demo';
$secret_key = isset($argv[3]) ? $argv[3] : false;
$cipher_key = isset($argv[4]) ? $argv[4] : false;
$ssl_on = false;
## Create Pubnub Object
$pubnub = new Pubnub($publish_key, $subscribe_key, $secret_key, $cipher_key, $ssl_on);
## Define Messaging Channel
$channel = "hello_world";
## Subscribe Example
echo "\nWaiting for Publish message... Hit CTRL+C to finish.\n";
$pubnub->subscribe(array('channel' => $channel, 'callback' => function ($message) {
    print_r($message);
    echo "\r\n";
    return false;
}));
$publish_key = isset($argv[1]) ? $argv[1] : false;
$subscribe_key = isset($argv[2]) ? $argv[2] : false;
$channel_name = isset($argv[3]) ? $argv[3] : 'hello_world2';
# Print usage if missing info.
if (!($publish_key && $subscribe_key)) {
    echo "\n    ==============\n    EXAMPLE USAGE:\n    ==============\n    php ./subscribe-example.php PUBLISH-KEY SUBSCRIBE-KEY\n\n";
    exit;
}
## Require Pubnub API
echo "Loading Pubnub.php Class\n";
require '../Pubnub.php';
## -----------------------------------------
## Create Pubnub Client API (INITIALIZATION)
## -----------------------------------------
echo "Creating new Pubnub Client API\n";
$pubnub = new Pubnub($publish_key, $subscribe_key);
## ------------------------------
## Listen for Message (SUBSCRIBE)
## ------------------------------
echo "Listening for Messages (Press ^C to stop)\n";
$pubnub->subscribe(array('channel' => $channel_name, 'callback' => function ($message) {
    ## REQUIRED Callback With Response
    var_dump($message);
    ## Print Message
    usleep(100);
    return true;
    ## Keep listening (return false to stop)
}));
?>

<?php

require '../Pubnub.php';
$pubnub = new Pubnub('demo', 'demo');
$pubnub->subscribe(array('channel' => 'extra_cool_channel', 'callback' => function ($message) {
    var_dump($message);
    return true;
    ## continue listening
}));
Example #5
0
<?php

require_once 'Pubnub.php';
$pubnub = new Pubnub('demo', 'demo', false, false, false);
$pubnub->subscribe(array('channel' => 'testChannel', 'callback' => function ($message) {
    $fp = fopen('subscribeOut.txt', 'w');
    fwrite($fp, serialize($message));
    fclose($fp);
    exit;
}));
<?php

## Capture Publish and Subscribe Keys from Command Line
$publish_key = isset($argv[1]) ? $argv[1] : false;
$subscribe_key = isset($argv[2]) ? $argv[2] : false;
## Require Pubnub API
require '../Pubnub.php';
## -----------------------------------------
## Create Pubnub Client API (INITIALIZATION)
## -----------------------------------------
$pubnub = new Pubnub($publish_key, $subscribe_key);
## ----------------------
## Send Message (PUBLISH)
## ----------------------
$pubnub->subscribe(array('channel' => 'php_chat', 'callback' => function ($message) {
    ## REQUIRED Callback With Response
    ## Print Message
    echo "[" . date('H:i:s') . "] <" . $message['from'] . "> " . $message['text'] . "\r\n";
    ## Keep listening (return false to stop)
    return true;
}));
?>