This is the official PubNub PHP SDK repository.
PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.
The SDK supports PHP 7.4 and 8.x.
You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.
-
Integrate the PHP SDK into your project:
-
Without composer
-
Clone the following repository:
git clone https://github.com/pubnub/php.git ./pubnub-php -
Copy the
srcfolder to your project. -
Include
autoloader.phpfile in your project:require_once('src/autoloader.php');
-
Download dependency
monologfrom https://github.com/Seldaek/monolog and copy themonologfolder from thesrcfolder to thesrcfolder of your project. -
Download dependency
psr/Logfrom https://github.com/php-fig/log/tree/master and copy thepsrfolder to thesrcfolder of your project. -
Download dependency
rmccuefrom https://github.com/WordPress/Requests and copy theRequestsfolder and the fileRequests.phpfrom thelibraryfolder to thesrcfolder of your project.
-
-
With composer
-
Add the PubNub package to your
composer.jsonfile:{ "require": { <!-- include the latest version from the badge at the top --> "pubnub/pubnub": "8.0.2" } } -
Run
composer install --no-devfrom the command line. This installs the PubNub PHP SDK and all its dependencies in thevendorfolder of the project. -
Include
autoload.phpfile in your project:require_once('vendor/autoload.php');
-
-
-
Configure your keys:
$pnconf = new PNConfiguration(); $pubnub = new PubNub($pnconf); $pnconf->setSubscribeKey("mySubscribeKey"); $pnconf->setPublishKey("myPublishKey"); $pnconf->setUserId("ReplaceWithYourClientIdentifier");
class MySubscribeCallback extends SubscribeCallback {
function status($pubnub, $status) {
if ($status->getCategory() === PNStatusCategory::PNUnexpectedDisconnectCategory) {
// This event happens when radio / connectivity is lost
} else if ($status->getCategory() === PNStatusCategory::PNConnectedCategory){
// Connect event. You can do stuff like publish, and know you'll get it // Or just use the connected event to confirm you are subscribed for // UI / internal notifications, etc
} else if ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory){
// Handle message decryption error. Probably client configured to // encrypt messages and on live data feed it received plain text.
}
}
function message($pubnub, $message){
// Handle new message stored in message.message
}
function presence($pubnub, $presence){
// handle incoming presence data
}
}
$subscribeCallback = new MySubscribeCallback();
$pubnub->addListener($subscribeCallback);$pubnub->subscribe()
->channels("hello_world")
->execute();
$pubnub->publish()
->channel("hello_world")
->message("Hello PubNub!")
->sync();If you need help or have a general question, contact [email protected].