-
Notifications
You must be signed in to change notification settings - Fork 89
Fixes, improvements, samples #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| // Include Composer autoloader (adjust path if needed) | ||
| require_once 'vendor/autoload.php'; | ||
|
|
||
| use PubNub\PNConfiguration; | ||
| use PubNub\PubNub; | ||
| use PubNub\Exceptions\PubNubServerException; | ||
|
|
||
| // Create configuration | ||
| $pnConfig = new PNConfiguration(); | ||
| $pnConfig->setSubscribeKey(getenv("SUBSCRIBE_KEY") ?? "demo"); | ||
| $pnConfig->setPublishKey(getenv("PUBLISH_KEY") ?? "demo"); | ||
| $pnConfig->setSecretKey(getenv("SECRET_KEY") ?? "demo"); // Required for Access Manager operations | ||
| $pnConfig->setUserId("php-token-granter"); | ||
|
|
||
| // Initialize PubNub instance | ||
| $pubnub = new PubNub($pnConfig); | ||
|
|
||
| try { | ||
| // Grant token with permissions to a channel | ||
| $token = $pubnub->grantToken() | ||
| ->ttl(15) // Time-to-live in minutes (min: 1, max: 43200) | ||
| ->authorizedUuid('php-authorized-user') | ||
| ->addChannelResources([ | ||
| 'my-channel' => ['read' => true, 'write' => true, 'update' => true], | ||
| ]) | ||
| ->sync(); | ||
|
|
||
| // Print the token | ||
| echo "Generated token: " . $token . PHP_EOL; | ||
|
|
||
| // Example of how to use the token in a client application | ||
| echo "How to use this token in a client:" . PHP_EOL; | ||
| echo " 1. Initialize a PubNub client without secretKey" . PHP_EOL; | ||
| echo " 2. Set token with: \$pubnub->setToken(\"" . $token . "\");" . PHP_EOL; | ||
| echo " 3. Use the authorized UUID: php-authorized-user" . PHP_EOL; | ||
| } catch (PubNubServerException $exception) { | ||
| // Handle errors | ||
| echo "Error generating token: " . $exception->getServerErrorMessage() . PHP_EOL; | ||
| echo "Status Code: " . $exception->getStatusCode() . PHP_EOL; | ||
|
|
||
| if ($exception->getServerErrorSource()) { | ||
| echo "Error Source: " . $exception->getServerErrorSource() . PHP_EOL; | ||
| } | ||
|
|
||
| if ($exception->getServerErrorDetails()) { | ||
| echo "Error Details: " . print_r($exception->getServerErrorDetails(), true) . PHP_EOL; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
|
|
||
| // Include Composer autoloader (adjust path if needed) | ||
| require_once 'vendor/autoload.php'; | ||
|
|
||
| use PubNub\PNConfiguration; | ||
| use PubNub\PubNub; | ||
| use PubNub\Exceptions\PubNubServerException; | ||
|
|
||
| // Create configuration | ||
| $pnConfig = new PNConfiguration(); | ||
| $pnConfig->setSubscribeKey(getenv("SUBSCRIBE_KEY") ?? "demo"); | ||
| $pnConfig->setPublishKey(getenv("PUBLISH_KEY") ?? "demo"); | ||
| $pnConfig->setUserId("php-channel-group-demo"); | ||
|
|
||
| // Initialize PubNub instance | ||
| $pubnub = new PubNub($pnConfig); | ||
|
|
||
| try { | ||
| // Add channels to channel group | ||
| $result = $pubnub->addChannelToChannelGroup() | ||
| ->channels(["news", "sports"]) | ||
| ->channelGroup("my-group") | ||
| ->sync(); | ||
|
|
||
| // Print success message | ||
| echo "Channels added to group successfully!" . PHP_EOL; | ||
|
|
||
| // Example of how to use this channel group for subscription | ||
| echo PHP_EOL . "To subscribe to this channel group:" . PHP_EOL; | ||
| echo '$pubnub->subscribe()->channelGroups(["my-group"])->execute();' . PHP_EOL; | ||
| } catch (PubNubServerException $exception) { | ||
| // Handle errors | ||
| echo "Error adding channels to group: " . $exception->getMessage() . PHP_EOL; | ||
|
|
||
| if (method_exists($exception, 'getServerErrorMessage') && $exception->getServerErrorMessage()) { | ||
| echo "Server Error: " . $exception->getServerErrorMessage() . PHP_EOL; | ||
| } | ||
|
|
||
| if (method_exists($exception, 'getStatusCode') && $exception->getStatusCode()) { | ||
| echo "Status Code: " . $exception->getStatusCode() . PHP_EOL; | ||
| } | ||
| } catch (Exception $exception) { | ||
| // Handle general exceptions | ||
| echo "Error: " . $exception->getMessage() . PHP_EOL; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php | ||
|
|
||
| // Include Composer autoloader (adjust path if needed) | ||
| require_once 'vendor/autoload.php'; | ||
|
|
||
| use PubNub\PNConfiguration; | ||
| use PubNub\PubNub; | ||
| // Uncomment the line below to enable encryption | ||
| // use PubNub\CryptoModule; | ||
|
|
||
| // Create a new configuration instance | ||
| $pnConfig = new PNConfiguration(); | ||
|
|
||
| // Set subscribe key (required) | ||
| $pnConfig->setSubscribeKey(getenv("SUBSCRIBE_KEY") ?? "demo"); | ||
seba-aln marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Set publish key (only required if publishing) | ||
| $pnConfig->setPublishKey(getenv("PUBLISH_KEY") ?? "demo"); | ||
seba-aln marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Set UUID (required to connect) | ||
| $pnConfig->setUserId("php-sdk-example-user"); | ||
|
|
||
| // Set up cryptography for message encryption (optional) | ||
| // Uncomment the line below to enable encryption | ||
| // $pnConfig->setCryptoModule(CryptoModule::aesCbcCryptor("your-cipher-key", true)); | ||
|
|
||
| // Set authentication key (optional, required only when using Access Manager) | ||
| // $pnConfig->setAuthKey("my_auth_key"); | ||
|
|
||
| // Configure connection timeout in seconds | ||
| $pnConfig->setConnectTimeout(10); | ||
|
|
||
| // Configure subscribe request timeout in seconds | ||
| $pnConfig->setSubscribeTimeout(310); | ||
|
|
||
| // Configure non-subscribe request timeout in seconds | ||
| $pnConfig->setNonSubscribeRequestTimeout(10); | ||
|
|
||
| // Set filter expression (optional) | ||
| // $pnConfig->setFilterExpression("channel == 'my-channel'"); | ||
|
|
||
| // Create PubNub instance with the configured settings | ||
| $pubnub = new PubNub($pnConfig); | ||
|
|
||
| // Display configuration information | ||
| echo "PubNub Configuration:\n"; | ||
| echo "Subscribe Key: " . $pnConfig->getSubscribeKey() . "\n"; | ||
| echo "Publish Key: " . $pnConfig->getPublishKey() . "\n"; | ||
| echo "User ID: " . $pnConfig->getUserId() . "\n"; | ||
| echo "Encryption: " . ($pnConfig->getCryptoSafe() ? "enabled" : "disabled") . "\n"; | ||
|
|
||
| // Now you can use this PubNub instance to publish and subscribe | ||
|
|
||
| // Example: Create a simple message | ||
| $message = ["text" => "Hello from PHP SDK!"]; | ||
|
|
||
| // Example: Publish the message (uncomment to execute) | ||
| /* | ||
| $pubnub->publish() | ||
| ->channel("demo-channel") | ||
| ->message($message) | ||
| ->sync(); | ||
|
|
||
| echo "Message published to 'demo-channel'\n"; | ||
| */ | ||
|
|
||
| // Keep this code running only if you plan to subscribe to messages | ||
| // Otherwise, the script will exit after publishing | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
|
|
||
| // Include Composer autoloader (adjust path if needed) | ||
| require_once 'vendor/autoload.php'; | ||
|
|
||
| use PubNub\PNConfiguration; | ||
| use PubNub\PubNub; | ||
| use PubNub\Exceptions\PubNubServerException; | ||
|
|
||
| // Create configuration | ||
| $pnConfig = new PNConfiguration(); | ||
| $pnConfig->setSubscribeKey(getenv("SUBSCRIBE_KEY") ?? "demo"); | ||
| $pnConfig->setPublishKey(getenv("PUBLISH_KEY") ?? "demo"); | ||
| $pnConfig->setUserId("php-file-upload-demo"); | ||
|
|
||
| // Initialize PubNub instance | ||
| $pubnub = new PubNub($pnConfig); | ||
|
|
||
| try { | ||
| // Define channel and file paths | ||
| $channelName = "file-sharing-channel"; | ||
| $fileName = "example.txt"; | ||
| $filePath = __DIR__ . DIRECTORY_SEPARATOR . $fileName; | ||
|
|
||
| // Create a sample file if it doesn't exist | ||
| if (!file_exists($filePath)) { | ||
| file_put_contents($filePath, "This is a sample file for PubNub file upload demo."); | ||
| } | ||
|
|
||
| // Open file handle for reading | ||
| $fileHandle = fopen($filePath, "r"); | ||
|
|
||
| // Send file to the channel | ||
| $sendFileResult = $pubnub->sendFile() | ||
| ->channel($channelName) | ||
| ->fileName($fileName) | ||
| ->message("Hello from PHP SDK") | ||
| ->fileHandle($fileHandle) | ||
| ->sync(); | ||
|
|
||
| // Close file handle | ||
| fclose($fileHandle); | ||
|
|
||
| // Print success message | ||
| echo "File uploaded successfully!" . PHP_EOL; | ||
| echo "File name: " . $sendFileResult->getFileName() . PHP_EOL; | ||
| echo "File ID: " . $sendFileResult->getFileId() . PHP_EOL; | ||
|
|
||
| // Example of how to download this file | ||
| echo PHP_EOL . "To download this file, use:" . PHP_EOL; | ||
| echo '$result = $pubnub->downloadFile()' . PHP_EOL; | ||
| echo ' ->channel("' . $channelName . '")' . PHP_EOL; | ||
| echo ' ->fileId("' . $sendFileResult->getFileId() . '")' . PHP_EOL; | ||
| echo ' ->fileName("' . $sendFileResult->getFileName() . '")' . PHP_EOL; | ||
| echo ' ->sync();' . PHP_EOL; | ||
| } catch (PubNubServerException $exception) { | ||
| // Handle PubNub-specific errors | ||
| echo "Error uploading file: " . $exception->getMessage() . PHP_EOL; | ||
|
|
||
| if (method_exists($exception, 'getServerErrorMessage') && $exception->getServerErrorMessage()) { | ||
| echo "Server Error: " . $exception->getServerErrorMessage() . PHP_EOL; | ||
| } | ||
|
|
||
| if (method_exists($exception, 'getStatusCode') && $exception->getStatusCode()) { | ||
| echo "Status Code: " . $exception->getStatusCode() . PHP_EOL; | ||
| } | ||
| } catch (Exception $exception) { | ||
| // Handle general exceptions | ||
| echo "Error: " . $exception->getMessage() . PHP_EOL; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.