anovikov1984 / php

PubNub clients for PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Contact support@pubnub.com for all questions

PubNub Real-time Data Network

Clients for PHP and Composer

How to include

PHP <= 5.2

  1. You need only legacy folder. To get it clone repo:
``` sh
$ git clone https://github.com/pubnub/php.git ./pubnub-php
```
  1. Copy 2 files to your project vendor libraries folder.
  2. Require Pubnub.php file:
``` php
require_once('legacy/Pubnub.php');
```

PHP >= 5.3 without composer

  1. You need only composer folder. To get it clone repo:
``` sh
$ git clone https://github.com/pubnub/php.git ./pubnub-php
```
  1. Copy composer/lib folder to your project and include autoloader.php file.
  2. Require autoloader.php:
```php
require_once('lib/autoloader.php');
```

PHP >= 5.3 with composer

  1. Add pubnub package to your composer.json file:
``` json
{
    "require": {
        "pubnub/pubnub": "3.5.*"
    }
}
```
  1. Run composer install from command line

PHP API

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
));

Send Message (PUBLISH)

$info = $pubnub->publish('my_channel', 'Hey World!'));

print_r($info);

Request Server Time (TIME)

$timestamp = $pubnub->time();
var_dump($timestamp);            ## Prints integer timestamp.

Receive Message (SUBSCRIBE)

$pubnub->subscribe('my_channel', function($message) {
    var_dump($message);  ## Print Message
    return true;         ## Keep listening (return false to stop)
});

Realtime Join/Leave Events (Presence)

// will subscribe to *my_channel-pnpres* channel
$pubnub->presence('my_channel', function($message) {
    print_r($message);
    echo "\r\n";
    return false;
});

On-demand Occupancy Status (here_now)

$here_now = $pubnub->hereNow('my_channel');

print_r($here_now);

History (detailedHistory())

$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 (detailedHistory()) with time tokens

$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
)

Differences with legacy/composer clients usage

  • 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;'));

How to build

For building information see README.md file in core folder.

Contact support@pubnub.com for all questions

About

PubNub clients for PHP

License:Other


Languages

Language:PHP 99.2%Language:Shell 0.8%