SelectPHP Configuration API Reference for Realtime Apps

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.

PHP complete API reference for building Realtime Applications on PubNub, including basic usage and sample code.

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.
This function is used for initializing the PubNub Client API context. This function must be called before attempting to utilize any API functionality in order to establish account level credentials such as publish_key and subscribe_key.
To Initialize Pubnub you can use the following method(s) in the PHP SDK:
  1. ParameterTypeRequiredDescription
    publish_keyStringOptionalSpecifies the required publish_key to be used only when publishing messages to a channel. This key need not be specified if the client will not publish data to any channel.
    subscribe_keyStringYesSpecifies the required subscribe_key to be used for subscribing to a channel.
    secret_keyString or false to ignoreOptionalSpecifies the secret key. Necessary only if granting access right with PubNub Access Manager.

    default: None
    cipher_keyString or false to ignoreOptionalSpecifies the cipher key.
    Necessary only for AES-256 message encryption/decryption.

    default: None
    sslBooleanOptionalSetting a value of true enables transport layer encryption with TLS (formerly SSL). Default is false.
    originString or false to use defaultOptionaldefault ='ps.pndsn.com'
    Typically on used when troubleshooting. Must be configured before use by request to support@pubnub.com.
    pem_pathString or false to ignoreOptionalpem_path is a path to directory containing a pem file for SSL, the file itself should have a fixed name pubnub.com.pem. The argument should be false if not using SSL.
    uuidString or false to ignoreYes

    UUID to use. You should set a unique UUID to identify the user or the device that connects to PubNub.

    Allowing the SDK to generate a random UUID can result in significant billing impacts, particularly on an MAU pricing plan.

    proxyString or false to ignoreOptionalProxy server to tunnel requests through or false if not using proxy.
    auth_keyString or false to ignoreOptionalSpecifies the auth key to use for authorization by PubNub to read and write on channels when Access Manager service is enabled.

    default: None
    gzipBooleanOptionalEnables gzip compression
    verify_peerBooleanOptionalWhen true you may need the PEM files, check this section to know more about PEM files.

    Default: true
$pubnub = new Pubnub("my_pubkey", "my_subkey", false, false, true);
It returns the Pubnub instance for invoking PubNub APIs like publish(), subscribe(), history(), hereNow(), etc.
  1. $pubnub = new Pubnub("demo", "demo");
  2. In the case where a client will only read messages and never publish to a channel, you can simply omit the publish_key when initializing the client:
     # Initialize for Read Only Client
    $pubnub = new Pubnub(false, "demo");
  3. Set a custom UUID to identify your users.
    $pubnub = new Pubnub("myPublishKey", "mySubscribeKey", false, false, false, false, false, "myUniqueUUID");
  4. This examples demonstrates how to enable PubNub Transport Layer Encryption with SSL. Just initialize the client with ssl set to true. The hard work is done, now the PubNub API takes care of the rest. Just subscribe and publish as usual and you are good to go.

    $pubnub = new Pubnub("my_pubkey", "my_subkey", false, false, true);
  5. Clients can also be initialized using named arguments:
    new Pubnub(
    	"publish_key" => "demo",
    	"subscribe_key" => "demo",
    	"secret_key" => "my_secretKey",
    	"cipher_key" => "my_cipherKey",
    	"ssl" => true,
    	"origin" => "ps.pndsn.com",
    	"pem_path" => "../../",
    	"uuid" => "user1",
    	"proxy" => false,
    	"auth_key" => "a0c47da7-cf48-4efa-8e6d-13819854d542",
    	$verify_peer => false
    );
These functions are used to set/get a user ID on the fly.
To set/get UUID you can use the following method(s) in PHP SDK
  1. ParameterTypeRequiredDescription
    uuid string Yesnew UUID
  2. This function has no parameters.

  3. This function doesn't have any parameters.

$pubnub->setUUID("myUniqueUUID");
$uuid = $pubnub->getUUID();
echo Pubnub::uuid();
  1. Get UUID Function returns current client uuid: uuid String
  2. The output below demonstrates an example generate uuid that is returned:
    
    d73cc6e9-b433-4679-b730-e4e8536315ea
    
  1. $pubnub = new Pubnub("myPublishKey", "mySubscribeKey");
    $pubnub->setUUID("myUniqueUUID");
  2. $unique_channel = Pubnub::uuid();
    
    $pubnub->subscribe($unique_channel, function ($envelope) {
           print_r($envelope['message']);
    });
  3. $pubnub = new Pubnub([
        'publish_key' => 'demo',
        'subscribe_key' => 'demo',
        'auth_key' => Pubnub::uuid()
    ]);
This function provides the capability to reset a user's auth_key.
Typically auth_key is specified during initialization for PubNub Access Manager enabled applications. In the event that auth_key has expired or a new auth_key is issued to the client from a Security Authority, the new auth_key can be sent using setAuthKey().
To Set Authentication Key you can use the following method(s) in the PHP SDK
  1. ParameterTypeRequiredDescription
    auth_keyStringYesThe auth_key to set - using an empty string as the value will effectively unset the auth key.
$pubnub->setAuthKey("my_new_authkey");
None.
cURL request timeout setter for subscribe/non-subscribe requests
  1. ParameterTypeRequiredDescription
    timeout
    Number
    Yes
    Non-subscribe timeout value in seconds.
  2. ParameterTypeRequiredDescription
    timeout
    Number
    Yes
    timeout value in seconds
$pubnub->setTimeout(5);
$pubnub->setSubscribeTimeout(5);