PHPPHP V4 Configuration API Reference for Realtime Apps

 

These docs are for PubNub 4.x for PHP which is our latest and greatest! For the docs of the older versions of the SDK, please check PubNub 3.x for PHP.

If you have questions about the PubNub for PHP SDK, please contact us at support@pubnub.com.

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

PNConfiguration instance is storage for user-provided information which describe further PubNub client behaviour. Configuration instance contain additional set of properties which allow to perform precise PubNub client configuration.
To create configuration instance you can use the following function in the PHP V4 SDK:
  1. ParameterTypeRequiredDefaultsDescription
    subscribeKeyStringYessubscribeKey from admin panel
    publishKeyStringOptionalnullpublishKey from admin panel (only required if publishing)
    secretKeyStringOptionalnullsecretKey (only required for modifying/revealing access permissions)
    cipherKeyStringOptionalnullIf cipherKey is passed, all communications to/from PubNub will be encrypted.
    uuidStringOptionalSDK generated uuiduuid to be used as a device identifier, a default uuid is generated if not passed.
    authKeyStringOptionalnullIf Access Manager is utilized, client will use this authKey in all restricted requests.
    sslBooleanOptionaltrueUse ssl
    connectTimeoutIntegerOptional10

    How long to wait before giving up connection to client.


    The value is in seconds.

    subscribeTimeoutIntegerOptional310

    How long to keep the subscribe loop running before disconnect.


    The value is in seconds.

    nonSubscribeRequestTimeoutIntegerOptional10

    On non subscribe operations, how long to wait for server response.


    The value is in seconds.

    filterExpressionStringOptionalnullFeature to subscribe with a custom filter expression.
    originStringOptionalps.pndsn.comCustom origin if needed.
use PubNub\PNConfiguration;

$pnConfiguration = new PNConfiguration();

// subscribeKey from admin panel
$pnConfiguration->setSubscribeKey("my_sub_key"); // required

// publishKey from admin panel (only required if publishing)
$pnConfiguration->setPublishKey("my_pub_key");

// secretKey (only required for modifying/revealing access permissions)
$pnConfiguration->setSecretKey("my_secretKey");

// if cipherKey is passed, all communicatons to/from pubnub will be encrypted
$pnConfiguration->setCipherKey("my_cipherKey");

// UUID to be used as a device identifier, a default UUID is generated
// if not passsed
$pnConfiguration->setUuid("my_custom_uuid");

// if Access Manager is utilized, client will use this authKey in all restricted
// requests
$pnConfiguration->setAuthKey("my_auth_key");

// use SSL (enabled by default)
$pnConfiguration->setSecure(true);

// how long to wait before giving up connection to client
$pnConfiguration->setConnectTimeout(10);

// how long to keep the subscribe loop running before disconnect
$pnConfiguration->setSubscribeTimeout(310);

// on non subscribe operations, how long to wait for server response
$pnConfiguration->setNonSubscribeRequestTimeout(300);

// PSV2 feature to subscribe with a custom filter expression
$pnConfiguration->setFilterExpression("such = wow");
Configured and ready to use client configuration instance.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 publishKey and subscribeKey.
To Initialize Pubnub you can use the following method(s) in the PHP V4 SDK:
  1. ParameterTypeRequiredDescription
    pnConfigurationPNConfigurationYesGoto Configuration for more details.
use PubNub\PNConfiguration;
use PubNub\PubNub;

$pnconf = new PNConfiguration();

$pnconf->setSubscribeKey("my-key");
$pnconf->setPublishKey("my-key");
$pnconf->setSecure(false);

$pubnub = new PubNub($pnconf);
It returns the Pubnub instance for invoking PubNub APIs like publish(), subscribe(), history(), hereNow(), etc.
  1. use PubNub\PNConfiguration;
    use PubNub\PubNub;
    
    $pnConfiguration = new PNConfiguration();
    
    $pnConfiguration->setSubscribeKey("my_sub_key");
    $pnConfiguration->setPublishKey("my_pub_key");
    $pnConfiguration->setSecure(false);
    
    $pubnub = new PubNub($pnConfiguration);
  2. In the case where a client will only read messages and never publish to a channel, you can simply omit the publishKey when initializing the client:
    use PubNub\PNConfiguration;
    use PubNub\PubNub;
    
    $pnConfiguration = new PNConfiguration();
    
    $pnConfiguration->setSubscribeKey("my_sub_key");
    
    $pubnub = new PubNub($pnConfiguration);
  3. Set a custom UUID to identify your users.
    use PubNub\PNConfiguration;
    use PubNub\PubNub;
    
    $pnconf = new PNConfiguration();
    
    $pnconf->setSubscribeKey("my_sub_key");
    $pnconf->setPublishKey("my_pub_key");
    $pnconf->setUuid("my_uuid");
    
    $pubnub = new PubNub($pnconf);
  4. Requires Access Manager add-on XRequires that the Access Manager add-on is enabled for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
     

    Anyone with the secretKey can grant and revoke permissions to your app. Never let your secret key be discovered, and to only exchange it / deliver it securely. Only use the secretKey on secure server-side platforms.

    When you init with secretKey, 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.

    For applications that will administer PAM permissions, the API is initialized with the secretKey as in the following example:
    use PubNub\PNConfiguration;
    use PubNub\PubNub;
    
    $pnConfiguration = new PNConfiguration();
    
    $pnConfiguration->setSubscribeKey("my_sub_key");
    $pnConfiguration->setPublishKey("my_pub_key");
    $pnConfiguration->setSecretKey("my_secret_key");
    
    $pubnub = new PubNub($pnConfiguration);
    Now that the pubnub object is instantiated the client will be able to access the PAM functions. The pubnub object will use the secretKey to sign all PAM messages to the PubNub Network.
These functions are used to set/get a user ID on the fly.
To set/get UUID you can use the following property(s) in PHP V4 SDK
  1. ParameterTypeRequiredDefaultDescription
    uuidStringYesSDK generated uuid.UUID to be used as a device identifier, a default UUID is generated if not passed.
  2. This method doesn't take any arguments.

$pnconf = new PNConfiguration();
$pnconf->setUuid("my_uuid");
$pubnub->getConfiguration()
    ->getUuid();
Setter and getter for users auth key.
  1. ParameterTypeRequiredDescription
    AuthKeyStringYesIf Access Manager is utilized, client will use this authkey in all restricted requests.
  2. This method doesn't take any argument.

$pubnub->getConfiguration()
    ->setAuthKey("my_newauthkey");
$pubnub->getConfiguration()
    ->getAuthKey();
None.

Go to Publish & Subscribe