Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
name: php
version: 8.0.0
version: 8.0.1
schema: 1
scm: github.com/pubnub/php
changelog:
- date: 2025-04-01
version: 8.0.1
changes:
- type: bug
text: "Added missing information in file publish endpoint."
- type: improvement
text: "Basic usage examples have been added."
- date: 2025-03-19
version: 8.0.0
changes:
Expand Down Expand Up @@ -457,8 +464,8 @@ sdks:
- x86-64
- distribution-type: library
distribution-repository: GitHub release
package-name: php-8.0.0.zip
location: https://github.com/pubnub/php/releases/tag/8.0.0
package-name: php-8.0.1.zip
location: https://github.com/pubnub/php/releases/tag/8.0.1
requires:
- name: rmccue/requests
min-version: 1.0.0
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 8.0.1
April 01 2025

#### Fixed
- Added missing information in file publish endpoint.

#### Modified
- Basic usage examples have been added.

## 8.0.0
March 19 2025

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ You will need the publish and subscribe keys to authenticate your app. Get your
{
"require": {
<!-- include the latest version from the badge at the top -->
"pubnub/pubnub": "8.0.0"
"pubnub/pubnub": "8.0.1"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": ["api", "real-time", "realtime", "real time", "ajax", "push"],
"homepage": "http://www.pubnub.com/",
"license": "proprietary",
"version": "8.0.0",
"version": "8.0.1",
"authors": [
{
"name": "PubNub",
Expand Down
26 changes: 18 additions & 8 deletions examples/Membersips/channel_members.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@

set_time_limit(0);

require('../../vendor/autoload.php');
require('vendor/autoload.php');

use PubNub\PubNub;
use PubNub\PNConfiguration;
use PubNub\Models\Consumer\Objects\Member\PNMemberIncludes;
use PubNub\Models\Consumer\Objects\Member\PNChannelMember;

$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey('demo');
$pnConfig->setSubscribeKey('demo');
$pnConfig->setUserId('demo');
$config = new PNConfiguration();
$config->setPublishKey(getenv('PUBLISH_KEY') ?? 'demo');
$config->setSubscribeKey(getenv('SUBSCRIBE_KEY') ?? 'demo');
$config->setUserId('demo');

$pubnub = new PubNub($pnConfig);
$pubnub = new PubNub($config);

$includes = new PNMemberIncludes();
$includes->user()->userId()->userCustom()->userType()->userStatus()->custom()->status()->type();
$includes->user()
->userCustom()
->userType()
->userStatus()
->custom()
->status()
->type();

$channel_members = [
(new PNChannelMember('uuid1'))
Expand All @@ -32,6 +38,10 @@
->setType("type")
];

$set_response = $pubnub->setMembers()->include($includes)->channel("ch")->members($channel_members)->sync();
$set_response = $pubnub->setMembers()
->include($includes)
->channel("ch")
->members($channel_members)
->sync();

var_dump($set_response);
50 changes: 50 additions & 0 deletions examples/basic_usage/access_manager.php
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;
}
}
46 changes: 46 additions & 0 deletions examples/basic_usage/channel_groups.php
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;
}
68 changes: 68 additions & 0 deletions examples/basic_usage/configuration.php
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");

// Set publish key (only required if publishing)
$pnConfig->setPublishKey(getenv("PUBLISH_KEY") ?? "demo");

// 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
70 changes: 70 additions & 0 deletions examples/basic_usage/files.php
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;
}
Loading
Loading