### Including Autoloader With Composer - PHP Source: https://github.com/pubnub/php/blob/master/README.md Includes the Composer autoloader file, which loads the PubNub SDK and its dependencies installed via Composer. ```php require_once('vendor/autoload.php');‌ ``` -------------------------------- ### Including Autoloader Without Composer - PHP Source: https://github.com/pubnub/php/blob/master/README.md Includes the PubNub SDK autoloader file when installing manually without Composer. This makes the SDK classes available in your project. ```php require_once('src/autoloader.php'); ``` -------------------------------- ### Publishing and Subscribing to a Channel - PHP Source: https://github.com/pubnub/php/blob/master/README.md Demonstrates how to subscribe to a channel to receive messages and how to publish a message to that same channel using the PubNub client. ```php $pubnub->subscribe() ->channels("hello_world") ->execute(); $pubnub->publish() ->channel("hello_world") ->message("Hello PubNub!") ->sync(); ``` -------------------------------- ### Configuring PubNub Keys and User ID - PHP Source: https://github.com/pubnub/php/blob/master/README.md Initializes the PubNub configuration and client objects. Sets the required subscribe key, publish key, and a unique user identifier for the client. ```php $pnconf = new PNConfiguration(); $pubnub = new PubNub($pnconf); $pnconf->setSubscribeKey("mySubscribeKey"); $pnconf->setPublishKey("myPublishKey"); $pnconf->setUserId("ReplaceWithYourClientIdentifier"); ``` -------------------------------- ### Adding PubNub Dependency - composer.json Source: https://github.com/pubnub/php/blob/master/README.md Adds the PubNub PHP SDK as a required dependency in your project's composer.json file. Specifies the package name and version constraint. ```json { "require": { "pubnub/pubnub": "8.0.1" } } ``` -------------------------------- ### Adding Custom Event Listener - PHP Source: https://github.com/pubnub/php/blob/master/README.md Defines a custom class extending SubscribeCallback to handle various PubNub events like status changes, incoming messages, and presence updates. Adds an instance of this listener to the PubNub client. ```php 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); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.