SelectPHP SDK 3.8.3

These docs are for version 3.8.3 of the PHP SDK. To check out the docs for the latest version Click Here.

PubNub 4.0 for PHP is our latest and greatest! Please click here for our PubNub 4.0 for PHP docs and SDK.

PubNub 3.x for PHP will encounter End Of Life (EOL) Aug 1st, 2018. All users currently on 3.x should begin migrating to 4.x. The 3.x SDK will continue to work. We will no longer support it and if you run into an issue, we will request that you upgrade to the latest version before solving the issue.

If you have questions about the PubNub for PHP SDK, please contact us at

  1. You need only composer folder. To get it clone repo:
    $ git clone ./pubnub-php
  2. Copy composer/lib folder to your project and include autoloader.php file.
  3. Require autoloader.php:
  1. Add pubnub package to your composer.json file:
        "require": {
            "pubnub/pubnub": "3.8.3"
  2. Run composer install from command line.

Add PubNub to your project using one of the procedures defined under How to Get It.

use Pubnub\Pubnub;
PEM files can be downloaded for the domains, and using the commands:

                echo Q | openssl s_client -connect -servername -showcerts
                echo Q | openssl s_client -connect -servername -showcerts
                echo Q | openssl s_client -connect -servername -showcerts
You need to set the verify_peer to true to use the PEM files.
$pubnub = new Pubnub('demo', 'demo');

//Subscribe to a channel, this is not async.
$pubnub->subscribe('hello_world', function ($envelope) {

// Use the publish command separately from the Subscribe code shown above. 
// Subscribe is not async and will block the execution until complete.
$publish_result = $pubnub->publish('hello_world','Hello PubNub!');
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
Instantiate a new Pubnub instance. Only the subscribe_key is mandatory. Also include publish_key if you intend to publish from this instance, and the secret_key if you wish to perform PAM administrative operations from this PHP instance.

For security reasons you should only include the secret-key on a highly secured server. The secret-key is only required for granting rights using our Access Manager.

When you init with secret_key, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
$pubnub = new Pubnub("demo", "demo");
Call time() to verify the client connectivity to the origin:
$time = $pubnub->time();
print_r($time . "\n");
Subscribe (listen on) a channel (it's async!):
Subscribe call is blocking and that it will block until a message is published on the channel(s) it is subscribed to.
  • Returning false will exit the subscribe loop after the first message.
  • Returning true will continue to keep script subscribed after it receives a message and keep blocking.
  • Perhaps a mention that creating a special exit message would be advisable.
The same is applicable to $timeoutHandler callback
$pubnub->subscribe('my_channel', function ($envelope) {
    $msg = $envelope['message'];

    if (strcmp($msg, "exit") == 0) {
        print_r('<<< So long, and thanks for all the messages! >>>');
        return false;
    else {
        print_r('>>> May I have some more message, please? <<<');
        return true;
Publish a message to a channel:
use Pubnub\Pubnub;
// *subscribe to the same channel below
// from another script/terminal or debug console
$publish_result = $pubnub->publish('hello_world','Hello PubNub!');
Get occupancy of who's here now on the channel by UUID:
Requires you to enable the Presence add-on for your key. Refer to the How do I enable add-on features for my keys? knowledge base article for details on enabling features.
 $hereNow = $pubnub->hereNow('my_channel');
Subscribe to realtime Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:
Requires you to enable the Presence add-on for your key. Refer to the How do I enable add-on features for my keys? knowledge base article for details on enabling features.
 # subscribe directly to presence channel
 $pubnub->subscribe('my_channel-pnpres', function ($envelope) {
Retrieve published messages from archival storage:
Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see
$history = $pubnub->history('history_channel');
Stop subscribing (listening) to a channel.
$pubnub->subscribe('my_channel', function ($envelope) {

     return false; // to stop the loop