<?php
require_once('vendor/autoload.php');
use PubNub\PubNub;
use PubNub\Enums\PNStatusCategory;
use PubNub\Callbacks\SubscribeCallback;
use PubNub\PNConfiguration;
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
}
}
$pnconf = new PNConfiguration();
$pubnub = new PubNub($pnconf);
$pnconf->setSubscribeKey("my_sub_key");
$pnconf->setPublishKey("my_pub_key");
$pubnub->addListener($subscribeCallback);
// Subscribe to a channel, this is not async.
$pubnub->subscribe()
->channels("hello_world")
->execute();
// Use the publish command separately from the Subscribe code shown above.
// Subscribe is not async and will block the execution until complete.
$result = $pubnub->publish()
->channel("hello_world")
->message("Hello PubNub")
->sync();
print_r($result);
There is some special character with the line require and it is giving error:
require_once('vendor/autoload.php');
See below code, it is working for me:
require_once('vendor/autoload.php');
use PubNub\PubNub;
use PubNub\Enums\PNStatusCategory;
use PubNub\Callbacks\SubscribeCallback;
use PubNub\PNConfiguration;
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
}
}
$pnconf = new PNConfiguration();
$pubnub = new PubNub($pnconf);
$pnconf->setSubscribeKey("demo");
$pnconf->setPublishKey("demo");
$subscribeCallback = new MySubscribeCallback();
$pubnub->addListener($subscribeCallback);
// Subscribe to a channel, this is not async.
$pubnub->subscribe()
->channels("hello_world")
->execute();
// Use the publish command separately from the Subscribe code shown above.
// Subscribe is not async and will block the execution until complete.
$result = $pubnub->publish()
->channel("hello_world")
->message("Hello PubNub")
->sync();
print_r($result);