|
| 1 | +<?php |
| 2 | +// snippet.hide |
| 3 | +// Ignoring example because of the "no side effects" rule |
| 4 | +// phpcs:ignoreFile |
| 5 | +// snippet.show |
| 6 | + |
| 7 | +// This file contains code snippets for the Getting Started guide |
| 8 | +// Include Composer autoloader (adjust path if needed) |
| 9 | +require_once 'vendor/autoload.php'; |
| 10 | + |
| 11 | +use PubNub\PNConfiguration; |
| 12 | +use PubNub\PubNub; |
| 13 | +use PubNub\Callbacks\SubscribeCallback; |
| 14 | +use PubNub\Enums\PNStatusCategory; |
| 15 | +use PubNub\Exceptions\PubNubServerException; |
| 16 | +use PubNub\Exceptions\PubNubException; |
| 17 | + |
| 18 | +// snippet.initialize_pubnub |
| 19 | +// Create a new configuration instance |
| 20 | +$pnConfiguration = new PNConfiguration(); |
| 21 | + |
| 22 | +// Set subscribe key (required) |
| 23 | +$pnConfiguration->setSubscribeKey(getenv("SUBSCRIBE_KEY") ?? "demo"); // Replace with your subscribe key |
| 24 | + |
| 25 | +// Set publish key (only required if publishing) |
| 26 | +$pnConfiguration->setPublishKey(getenv("PUBLISH_KEY") ?? "demo"); // Replace with your publish key |
| 27 | + |
| 28 | +// Set UUID (required to connect) |
| 29 | +$pnConfiguration->setUserId("php-sdk-user-" . uniqid()); |
| 30 | + |
| 31 | +// Set up cryptography for message encryption (optional) |
| 32 | +// $pnConfiguration->setCryptoModule(CryptoModule::aesCbcCryptor("your-cipher-key", true)); |
| 33 | + |
| 34 | +// Configure connection timeout in seconds |
| 35 | +$pnConfiguration->setConnectTimeout(10); |
| 36 | + |
| 37 | +// Create PubNub instance with the configured settings |
| 38 | +$pubnub = new PubNub($pnConfiguration); |
| 39 | +// snippet.end |
| 40 | + |
| 41 | +// snippet.event_listeners |
| 42 | +class MySubscribeCallback extends SubscribeCallback |
| 43 | +{ |
| 44 | + public function status($pubnub, $status) |
| 45 | + { |
| 46 | + if ($status->getCategory() === PNStatusCategory::PNUnexpectedDisconnectCategory) { |
| 47 | + // This event happens when radio / connectivity is lost |
| 48 | + echo "Unexpected disconnect - network may be down" . PHP_EOL; |
| 49 | + } elseif ($status->getCategory() === PNStatusCategory::PNConnectedCategory) { |
| 50 | + // Connect event. You can do stuff like publish, and know you'll get it |
| 51 | + echo "Connected to PubNub!" . PHP_EOL; |
| 52 | + } elseif ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory) { |
| 53 | + // Handle message decryption error |
| 54 | + echo "Decryption error: " . $status->getException() . PHP_EOL; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public function message($pubnub, $message) |
| 59 | + { |
| 60 | + // Handle new message stored in message.message |
| 61 | + echo "Received message: " . json_encode($message->getMessage()) . PHP_EOL; |
| 62 | + echo "Publisher: " . $message->getPublisher() . PHP_EOL; |
| 63 | + echo "Channel: " . $message->getChannel() . PHP_EOL; |
| 64 | + echo "Timetoken: " . $message->getTimetoken() . PHP_EOL; |
| 65 | + } |
| 66 | + |
| 67 | + public function presence($pubnub, $presence) |
| 68 | + { |
| 69 | + // Handle incoming presence data |
| 70 | + echo "Presence event: " . $presence->getEvent() . PHP_EOL; |
| 71 | + echo "UUID: " . $presence->getUuid() . PHP_EOL; |
| 72 | + echo "Channel: " . $presence->getChannel() . PHP_EOL; |
| 73 | + echo "Occupancy: " . $presence->getOccupancy() . PHP_EOL; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Add listener |
| 78 | +$subscribeCallback = new MySubscribeCallback(); |
| 79 | +$pubnub->addListener($subscribeCallback); |
| 80 | +// snippet.end |
| 81 | + |
| 82 | +// snippet.create_subscription |
| 83 | +// Subscribe to a channel |
| 84 | +$pubnub->subscribe() |
| 85 | + ->channels("hello_world") |
| 86 | + ->withPresence(true) // Optional: subscribe to presence events |
| 87 | + ->execute(); |
| 88 | +// snippet.end |
| 89 | + |
| 90 | +// snippet.publish_message |
| 91 | +// Assuming $pubnub is already initialized |
| 92 | + |
| 93 | +try { |
| 94 | + // Create message data |
| 95 | + $messageData = [ |
| 96 | + "text" => "Hello from PHP SDK!", |
| 97 | + "timestamp" => time(), |
| 98 | + "sender" => [ |
| 99 | + "name" => "PHP Publisher", |
| 100 | + "id" => $pnConfiguration->getUserId() |
| 101 | + ] |
| 102 | + ]; |
| 103 | + |
| 104 | + // Publish a message to a channel |
| 105 | + $result = $pubnub->publish() |
| 106 | + ->channel("hello_world") // Channel to publish to |
| 107 | + ->message($messageData) // Message content |
| 108 | + ->shouldStore(true) // Store in history |
| 109 | + ->sync(); // Execute synchronously |
| 110 | + |
| 111 | + // Display success message |
| 112 | + echo "Message published successfully!" . PHP_EOL; |
| 113 | + echo "Timetoken: " . $result->getTimetoken() . PHP_EOL; |
| 114 | + |
| 115 | + // Convert timetoken to readable date |
| 116 | + $timestamp = floor($result->getTimetoken() / 10000000); |
| 117 | + $readableDate = date('Y-m-d H:i:s', $timestamp); |
| 118 | + echo "Published at: " . $readableDate . PHP_EOL; |
| 119 | + |
| 120 | + // Display published message |
| 121 | + echo PHP_EOL . "Published message: " . PHP_EOL; |
| 122 | + echo json_encode($messageData, JSON_PRETTY_PRINT) . PHP_EOL; |
| 123 | +} catch (PubNubServerException $exception) { |
| 124 | + // Handle PubNub server-specific errors |
| 125 | + echo "Error publishing message: " . $exception->getMessage() . PHP_EOL; |
| 126 | + |
| 127 | + if (method_exists($exception, 'getServerErrorMessage') && $exception->getServerErrorMessage()) { |
| 128 | + echo "Server Error: " . $exception->getServerErrorMessage() . PHP_EOL; |
| 129 | + } |
| 130 | + |
| 131 | + if (method_exists($exception, 'getStatusCode') && $exception->getStatusCode()) { |
| 132 | + echo "Status Code: " . $exception->getStatusCode() . PHP_EOL; |
| 133 | + } |
| 134 | +} catch (PubNubException $exception) { |
| 135 | + // Handle PubNub-specific errors |
| 136 | + echo "PubNub Error: " . $exception->getMessage() . PHP_EOL; |
| 137 | +} catch (Exception $exception) { |
| 138 | + // Handle general exceptions |
| 139 | + echo "Error: " . $exception->getMessage() . PHP_EOL; |
| 140 | +} |
| 141 | +// snippet.end |
0 commit comments