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 support@pubnub.com.

  1. You need only composer folder. To get it clone repo:
    
    $ git clone https://github.com/pubnub/php.git ./pubnub-php
    
  2. Copy composer/lib folder to your project and include autoloader.php file.
  3. Require autoloader.php:
    
    require_once('lib/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 pubsub.pubnub.com, pubsub.pubnub.net and ps.pndsn.com using the commands:

                echo Q | openssl s_client -connect pubsub.pubnub.com:443 -servername pubsub.pubnub.com -showcerts
                echo Q | openssl s_client -connect pubsub.pubnub.net:443 -servername pubsub.pubnub.net -showcerts
                echo Q | openssl s_client -connect ps.pndsn.com:443 -servername ps.pndsn.com -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) {
             print_r($envelope['message']);
       });

// 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!');
print_r($publish_result);
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.

$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'];
    print_r($msg);

    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!');
  
print_r($publish_result);
Get occupancy of who's here now on the channel by UUID:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
 $hereNow = $pubnub->hereNow('my_channel');
 
 print_r($hereNow);
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 that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
 # subscribe directly to presence channel
 
 $pubnub->subscribe('my_channel-pnpres', function ($envelope) {
             print_r($envelope['message']);
         });
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 http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
$history = $pubnub->history('history_channel');
 
print_r($history['messages']);
Stop subscribing (listening) to a channel.
$pubnub->subscribe('my_channel', function ($envelope) {
     print_r($envelope);

     return false; // to stop the loop
});