Contact support@pubnub.com for all questions
PubNub Real-time Data Network
- You need only legacy folder. To get it clone repo:
``` sh
$ git clone https://github.com/pubnub/php.git ./pubnub-php
```
- Copy 2 files to your project vendor libraries folder.
- Require Pubnub.php file:
``` php
require_once('legacy/Pubnub.php');
```
- You need only composer folder. To get it clone repo:
``` sh
$ git clone https://github.com/pubnub/php.git ./pubnub-php
```
- Copy composer/lib folder to your project and include autoloader.php file.
- Require autoloader.php:
```php
require_once('lib/autoloader.php');
```
- Add pubnub package to your composer.json file:
``` json
{
"require": {
"pubnub/pubnub": "3.5.*"
}
}
```
- Run
composer install
from command line
You can instantiate PubNub client using postional list of arguments:
$pubnub = new Pubnub(
"demo", ## PUBLISH_KEY
"demo", ## SUBSCRIBE_KEY
"", ## SECRET_KEY
false ## SSL_ON?
);
or you can use named array:
$pubnub = new Pubnub(array(
'subscribe_key' => 'demo',
'publish_key' => 'demo',
'uuid' => 'my_uu_id',
'ssl' => true
));
$info = $pubnub->publish('my_channel', 'Hey World!'));
print_r($info);
$timestamp = $pubnub->time();
var_dump($timestamp); ## Prints integer timestamp.
$pubnub->subscribe('my_channel', function($message) {
var_dump($message); ## Print Message
return true; ## Keep listening (return false to stop)
});
// will subscribe to *my_channel-pnpres* channel
$pubnub->presence('my_channel', function($message) {
print_r($message);
echo "\r\n";
return false;
});
$here_now = $pubnub->hereNow('my_channel');
print_r($here_now);
$history = $pubnub->history('demo', 3, false, false, 13466530169226760);
print_r($history);
will output:
Array
(
[messages] => Array
(
[0] => message #1
[1] => message #2
[2] => message #3
)
[date_from] => 14037149868340218
[date_to] => 14037149868888352
)
$history = $pubnub->history('demo', 3, true);
print_r($history);
will output:
Array
(
[messages] => Array
(
[0] => Array
(
[message] => message #1
[timetoken] => 14037149868340218
)
[1] => Array
(
[message] => message #2
[timetoken] => 14037149868613433
)
[2] => Array
(
[message] => message #3
[timetoken] => 14037149868888352
)
)
[date_from] => 14037149868340218
[date_to] => 14037149868888352
)
-
in composer cliend you should use namespace Pubnub to access Pubnub class:
<?php use Pubnub\Pubnub; $pubnub = new Pubnub(); ?>
-
in composer client the most convenient way for defining collbacks is to use anonymous functions:
$pubnub->subscribe('my_channel', function($message) { var_dump($message); return true; });
but anonymous functions are implemented starting only PHP 5.3 version, so for legacy client you should use create_function() function:
$pubnub->subscribe('my_channel', create_function('$message', 'var_dump($message); return true;'));
For building information see README.md file in core folder.