PHP code example of macromindonline / pusher-php-server

1. Go to this page and download the library: Download macromindonline/pusher-php-server library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

macromindonline / pusher-php-server example snippets


$app_id = 'YOUR_APP_ID';
$app_key = 'YOUR_APP_KEY';
$app_secret = 'YOUR_APP_SECRET';
$app_cluster = 'YOUR_APP_CLUSTER';

$pusher = new Pusher\Pusher( $app_key, $app_secret, $app_id, array('cluster' => $app_cluster) );

$pusher = new Pusher\Pusher( $app_key, $app_secret, $app_id, array( 'cluster' => $app_cluster, 'encrypted' => true ) );

$pusher = new Pusher\Pusher( $app_key, $app_secret, $app_id, array( 'cluster' => $app_cluster, 'encrypted' => true, 'curl_options' => array( CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4 ) ) );

$pusher->trigger( 'my-channel', 'my_event', 'hello world' );

$pusher->trigger( [ 'channel-1', 'channel-2' ], 'my_event', 'hello world' );

$batch = array();
$batch[] = array('channel' => 'my-channel', 'name' => 'my_event', 'data' => array('hello' => 'world'));
$batch[] = array('channel' => 'my-channel', 'name' => 'my_event', 'data' => array('myname' => 'bob'));
$pusher->triggerBatch($batch);

$array['name'] = 'joe';
$array['message_count'] = 23;

$pusher->trigger('my_channel', 'my_event', $array);

$pusher->trigger('my-channel','event','data','socket_id');

$pusher->trigger('my-channel', 'event', 'data', null, false, true)

$pusher->socket_auth('private-my-channel','socket_id');

$pusher->presence_auth('presence-my-channel','socket_id', 'user_id', 'user_info');

Pusher.channel_auth_endpoint = '/presence_auth.php';


if (isset($_SESSION['user_id'])) {
  $stmt = $pdo->prepare("SELECT * FROM `users` WHERE id = :id");
  $stmt->bindValue(':id', $_SESSION['user_id'], PDO::PARAM_INT);
  $stmt->execute();
  $user = $stmt->fetch();
} else {
  die('aaargh, no-one is logged in');
}

header('Content-Type: application/json');

$pusher = new Pusher\Pusher($key, $secret, $app_id);
$presence_data = array('name' => $user['name']);

echo $pusher->presence_auth($_POST['channel_name'], $_POST['socket_id'], $user['id'], $presence_data);

$pusher->get_channel_info( $name );

$info = $pusher->get_channel_info('channel-name');
$channel_occupied = $info->occupied;

$info = $pusher->get_channel_info('presence-channel-name', array('info' => 'user_count'));
$user_count = $info->user_count;

$info = $pusher->get_channel_info('presence-channel-name', array('info' => 'subscription_count'));
$subscription_count = $info->subscription_count;

$pusher->get_channels()

$result = $pusher->get_channels();
$channel_count = count($result->channels); // $channels is an Array

$pusher->get_channels( array( 'filter_by_prefix' => 'some_filter' ) )

$results = $pusher->get_channels( array( 'filter_by_prefix' => 'presence-') );
$channel_count = count($result->channels); // $channels is an Array

$pusher->get( '/channels', array( 'filter_by_prefix' => 'presence-' ) );

$response = $pusher->get( '/channels/presence-channel-name/users' )

Array
(
    [body] => {"users":[{"id":"a_user_id"}]}
    [status] => 200
    [result] => Array
        (
            [users] => Array
                (
                    [0] => Array
                        (
                            [id] => a_user_id
                        )
                    /* Additional users */
                )
        )
)

$pusher->get( $path, $params );

$response = $pusher->get( '/channels' );
$http_status_code = $response[ 'status' ];
$result = $response[ 'result' ];

$pusher = new Pusher\Pusher($app_key, $app_secret, $app_id, array('notification_host' => 'custom notifications host'))

$data = array(
  'apns' => array(
    'aps' => array(
      'alert' => array(
        'body' => 'tada'
      ),
    ),
  ),
  'gcm' => array(
    'notification' => array(
      'title' => 'title',
      'icon' => 'icon'
    ),
  ),
);

$pusher->notify(array("test"), $data);

$data = array(
  'apns' => array("..."),
  'gcm' => array("..."),
  'webhook_url' => "http://my.company.com/pusher/nativepush/results"
);

$pusher->notify(array("test"), $data);

class MyLogger {
  public function log( $msg ) {
    print_r( $msg . "\n" );
  }
}

$pusher->set_logger( new MyLogger() );
bash
$ composer 
json
""pusher/pusher-php-server": "^3.0"
}