Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/monkeymars/pusher-php
PHP Interface to the Pusher API
https://github.com/monkeymars/pusher-php
Last synced: 3 months ago
JSON representation
PHP Interface to the Pusher API
- Host: GitHub
- URL: https://github.com/monkeymars/pusher-php
- Owner: monkeymars
- Fork: true (cj/Pusher-PHP)
- Created: 2012-11-12T10:23:13.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2012-08-14T22:41:03.000Z (over 12 years ago)
- Last Synced: 2023-04-01T16:02:50.186Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 108 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Pusher PHP Library
This is a very simple PHP library to the Pusher API (http://pusher.com).
Using it is easy as pie:require('Pusher.php');
$pusher = new Pusher($key, $secret, $app_id);
If you prefer to use the Singleton pattern usage is similar, but like this:require('Pusher.php');
$pusher = PusherInstance::get_pusher();
Then call the appropriate function.## Trigger
To trigger an event on a channel use the `trigger` function.
$pusher->trigger('my-channel', 'my_event', 'hello world');Note: You need to set your API information in Pusher.php
### Arrays
Objects are automatically converted to JSON format:
$array['name'] = 'joe';
$array['message_count'] = 23;$pusher->trigger('my_channel', 'my_event', $array);
The output of this will be:
"{'name': 'joe', 'message_count': 23}"
### Socket id
In order to avoid duplicates you can optionally specify the sender's socket id while triggering an event (http://pusherapp.com/docs/duplicates):
$pusher->trigger('my-channel','event','data','socket_id');
### Debugging
You can either turn on debugging by setting the fifth argument to true, like so:
$pusher->trigger('my-channel', 'event', 'data', null, true)
or with all requests:
$pusher = new Pusher($key, $secret, $app_id, true);
On failed requests, this will return the server's response, instead of false.
### JSON format
If your data is already encoded in JSON format, you can avoid a second encoding step by setting the sixth argument true, like so:
$pusher->trigger('my-channel', 'event', 'data', null, false, true)
## Authenticating Private channels
To authorise your users to access private channels on Pusher, you can use the socket_auth function:
$pusher->socket_auth('my-channel','socket_id');
## Authenticating Presence channels
Using presence channels is similar to private channels, but you can specify extra data to identify that particular user:
$pusher->presence_auth('my-channel','socket_id', 'user_id', 'user_info');
### Presence example
First set this variable in your JS app:
Pusher.channel_auth_endpoint = '/presence_auth.php';
Next, create the following in presence_auth.php:
$user['name']);
echo $pusher->presence_auth($_POST['channel_name'], $_POST['socket_id'], $user['id'], $presence_data);
?>Note: this assumes that you store your users in a table called `users` and that those users have a `name` column. It also assumes that you have a login mechanism that stores the `user_id` of the logged in user in the session.
## Channel Stats
It's also possible to get statistics about a channel from the Pusher REST API.
$stats = $pusher->get_channel_stats('channel-name');
$channel_occupied = $stats->occupied;
## Channels listIt's also possible to get a list of channels for an application from the Pusher REST API.
$channels = $pusher->get_channels();
$channel_count = count($channels); // $channels is an Array## License
Copyright 2010, Squeeks. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php