PHPPHP V4 Storage & Playback API Reference for Realtime Apps

Go to Channel Groups


 

These docs are for PubNub 4.x for PHP which is our latest and greatest! For the docs of the older versions of the SDK, please check PubNub 3.x for PHP.

If you have questions about the PubNub for PHP SDK, please contact us at support@pubnub.com.

Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
This function fetches historical messages of a channel.
PubNub Storage/Playback Service provides real-time access to an unlimited history for all messages published to PubNub. Stored messages are replicated across multiple availability zones in several geographical data center locations. Stored messages can be encrypted with AES-256 message encryption ensuring that they are not readable while stored on PubNub's network.
It is possible to control how messages are returned and in what order, for example you can:
  • Search for messages starting on the newest end of the timeline (default behavior - reverse = false)
  • Search for messages from the oldest end of the timeline by setting reverse to true.
  • Page through results by providing a start OR end time token.
  • Retrieve a slice of the time line by providing both a start AND end time token.
  • Limit the number of messages to a specific quantity using the count parameter.
 
Start & End parameter usage clarity:
If only the start parameter is specified (without end), you will receive messages that are older than and up to that start timetoken value.
If only the end parameter is specified (without start) you will receive messages that match that end timetoken value and newer.
Specifying values for both start and end parameters will return messages between those timetoken values (inclusive on the end value).
Keep in mind that you will still receive a maximum of 100 messages even if there are more messages that meet the timetoken values. Iterative calls to history adjusting the start timetoken is necessary to page through the full set of results if more than 100 messages meet the timetoken values.
To run History you can use the following method(s) in the PHP V4 SDK
  1. ParameterTypeRequiredDefaultsDescription
    channelStringYesSpecifies channel to return history messages from.
    reverseBooleanOptionalfalseSetting to true will traverse the time line in reverse starting with the oldest message first.
    includeTimetokenBooleanOptionalfalseWhether event dates time tokens should be included in response or not.
    startIntegerOptionalTime token delimiting the start of time slice (exclusive) to pull messages from.
    endIntegerOptionalTime token delimiting the end of time slice (inclusive) to pull messages from.
    countIntegerOptionalSpecifies the number of historical messages to return.
Retrieve the last 100 messages on a channel:
$result = $pubnub->history()
              ->channel("history_channel")
              ->count(100)
              ->sync();
The history() operation returns a PNHistoryResult which contains the following operations:
MethodTypeDescription
getMessages()Arrayarray of messages of type PNHistoryItemResult. See PNHistoryItemResult for more details.
getStartTimetoken()IntegerStart timetoken.
getEndTimetoken()IntegerEnd timetoken.
MethodTypeDescription
getTimetoken()IntegerTimetoken of the message.
getEntry()ObjectMessage.
  1. $pubnub->history()
        ->channel("my_channel")
        ->count(3)
        ->reverse(true)
        ->sync();
    PubNub\Models\Consumer\History\PNHistoryResult Object(
        [messages:PubNub\Models\Consumer\History\PNHistoryResult:private] => Array(
            [0] => PubNub\Models\Consumer\History\PNHistoryItemResult Object(
                [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array(
                    [a] => 11
                    [b] => 22
                )
                [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] =>
                [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 1111
            )
            [1] => PubNub\Models\Consumer\History\PNHistoryItemResult Object(
                [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array(
                    [a] => 33
                    [b] => 44
                )
                [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] =>
                [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222
            )
            [2] => PubNub\Models\Consumer\History\PNHistoryItemResult Object(
                [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array(
                    [a] => 55
                    [b] => 66
                )
                [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] =>
                [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222
            )
        )
     
        [startTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746729185766
        [endTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746780720711
    )
  2. $pubnub->history()
        ->channel("my_channel")
        ->start(13847168620721752)
        ->reverse(true)
        ->sync();
    PubNub\Models\Consumer\History\PNHistoryResult Object(
        [messages:PubNub\Models\Consumer\History\PNHistoryResult:private] => Array(
            [0] => PubNub\Models\Consumer\History\PNHistoryItemResult Object(
                [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array
                    (
                        [a] => 11
                        [b] => 22
                    )
    
                [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] =>
                [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 1111
            )
            [1] => PubNub\Models\Consumer\History\PNHistoryItemResult Object(
                [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array(
                    [a] => 33
                    [b] => 44
                )
    
                [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] =>
                [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222
            )
            [2] => PubNub\Models\Consumer\History\PNHistoryItemResult Object(
                [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array(
                    [a] => 55
                    [b] => 66
                )
    
                [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] =>
                [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222
            )
    
        )
     
        [startTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746729185766
        [endTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746780720711
    )
  3. $pubnub->history()
        ->channel("my_channel")
        ->count(100)
        ->start(-1)
        ->end(13847168819178600)
        ->reverse(true)
        ->sync();
    PubNub\Models\Consumer\History\PNHistoryResult Object(
        [messages:PubNub\Models\Consumer\History\PNHistoryResult:private] => Array(
            [0] => PubNub\Models\Consumer\History\PNHistoryItemResult Object(
                [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array(
                    [a] => 11
                    [b] => 22
                )
    
                [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] =>
                [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 1111
            )
            [1] => PubNub\Models\Consumer\History\PNHistoryItemResult Object(
                [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array(
                    [a] => 33
                    [b] => 44
                )
    
                [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] =>
                [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222
            )
            [2] => PubNub\Models\Consumer\History\PNHistoryItemResult Object(
                [entry:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => Array(
                    [a] => 55
                    [b] => 66
                )
    
                [crypto:PubNub\Models\Consumer\History\PNHistoryItemResult:private] =>
                [timetoken:PubNub\Models\Consumer\History\PNHistoryItemResult:private] => 2222
            )
    
        )
     
        [startTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746729185766
        [endTimetoken:PubNub\Models\Consumer\History\PNHistoryResult:private] => 13406746780720711
    )
  4. $pubnub->history()
        ->channel("my_channel")
        ->count(100)
        ->includeTimetoken(true)
        ->sync();
Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
Removes the messages from the history of a specific channel.
 
There is a setting to accept delete from history requests for a key, which you must enable by checking the Enable Delete-From-History checkbox in the key settings for your key in the Administration Portal.

Requires Initialization with secret key.

To Delete Messages from History you can use the following method(s) in the PHP V4 SDK.
  1. ParameterTypeRequiredDefaultsDescription
    channelStringYesSpecifies channels to delete messages from.
    startIntegerOptionalTime token delimiting the start of time slice (inclusive) to delete messages from.
    endIntegerOptionalTime token delimiting the end of time slice (exclusive) to delete messages from.
$pubnub->deleteMessages()
    ->channel("ch")
    ->start(123)
    ->end(456)
    ->sync();

  1. To delete a specific message, pass the publish timetoken (received from a successful publish) in the End parameter and timetoken +/- 1 in the Start parameter. e.g. if 15526611838554310 is the publish timetoken, pass 15526611838554309 in Start and 15526611838554310 in End parameters respectively as shown in the following code snippet.
    $pubnub->deleteMessages()
        ->channel("ch")
        ->start(15526611838554309)
        ->end(15526611838554310)
        ->sync();
Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
Returns the number of messages published on one or more channels since a given time. The count returned is the number of messages in history with a timetoken value greater than the passed value in the channelsTimetokenparameter.
 For keys with unlimited message retention enabled, this method considers only messages published in the last 7 days.
You can use the following method(s) in the PHP V4 SDK:
  1. ParameterTypeRequiredDefaultsDescription
    channels ArrayYesThe channels to fetch the message count
    channelsTimetoken ArrayYesArray of timetokens, in order of the channels list. Specify a single timetoken to apply it to all channels. Otherwise, the list of timetokens must be the same length as the list of channels, or the function returns a PNStatuswith an error flag.
$response = $pubnub->messageCounts()
    ->channels(["mychannel"])
    ->channelsTimetoken(["15513576173381797"])
    ->sync();

print_r($response->getChannels());
The operation returns a PNMessageCountsResult which contains the following operations
MethodTypeDescription
getChannels()ArrayAn associative array with channel name as key and messages count as value. Channels without messages have a count of 0. Channels with 10,000 messages or more have a count of 10000.
  1. $response = $pubnub->messageCounts()
        ->channels(["mychannel", "another_channel"])
        ->channelsTimetoken(["15513576173381797","15513574291261651"])
        ->sync();
    
    print_r($response->getChannels());

Go to Mobile Push