1. Go to this page and download the library: Download fanout/gripcontrol 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/ */
fanout / gripcontrol example snippets
function callback($result, $message)
{
if ($result)
Print "Publish successful\r\n";
else
Print "Publish failed with message: {$message}\r\n";
}
// GripPubControl can be initialized with or without an endpoint configuration.
// Each endpoint can dd new endpoints by applying an endpoint configuration:
$grippub->apply_grip_config(array(
array('control_uri' => '<myendpoint_uri_1>'),
array('control_uri' => '<myendpoint_uri_2>')));
// Remove all configured endpoints:
$grippub->remove_all_clients();
// Explicitly add an endpoint as a PubControlClient instance:
$pubclient = new PubControl\PubControlClient('<my_endpoint_uri>');
// Optionally set JWT auth: $pubclient->set_auth_jwt(<claim>, '<key>')
// Optionally set basic auth: $pubclient->set_auth_basic('<user>', '<password>')
$grippub->add_client($pubclient);
// Publish across all configured endpoints:
$grippub->publish_http_response('<channel>', 'Test publish!');
$grippub->publish_http_stream('<channel>', 'Test publish!');
// Use publish_async for async publishing only if pthreads are installed:
// $grippub->publish_http_response_async('<channel>', 'Test async publish!',
// null, null, 'callback');
// $grippub->publish_http_stream_async('<channel>', 'Test async publish!',
// null, null, 'callback');
// Wait for all async publish calls to complete:
// $grippub->finish();
// Validate the Grip-Sig header:
$request_headers = getallheaders();
if (!GripControl\GripControl::validate_sig($request_headers['Grip-Sig'], '<key>'))
return;
// Instruct the client to long poll via the response headers:
http_response_code(200);
header('Grip-Hold: response');
header('Grip-Channel: ' .
GripControl\GripControl::create_grip_channel_header('<channel>'));
// To optionally set a timeout value in seconds:
// header('Grip-Timeout: <timeout_value>');
// Validate the Grip-Sig header:
$request_headers = getallheaders();
if (!GripControl\GripControl::validate_sig($request_headers['Grip-Sig'], '<key>'))
return;
// Instruct the client to long poll via the response body:
http_response_code(200);
header('Content-Type: application/grip-instruct');
echo GripControl\GripControl::create_hold_response('<channel>');
// Or to optionally set a timeout value in seconds:
// echo GripControl\GripControl::create_hold_response(
// '<channel>', null, <timeout_value>);
class PublishMessage extends Thread
{
public function run()
{
// Wait and then publish a message to the subscribed channel:
sleep(5);
$grippub = new GripControl\GripPubControl(
array('control_uri' => '<myendpoint>'));
$grippub->publish('<channel>', new PubControl\Item(
new GripControl\WebSocketMessageFormat(
'Test WebSocket publish!!')));
}
}
// Validate the Grip-Sig header:
$request_headers = getallheaders();
if (!GripControl\GripControl::validate_sig($request_headers['Grip-Sig'], '<key>'))
return;
// Set the headers Control\GripControl::encode_websocket_events($out_events);
ignore_user_abort(true);
header("Connection: close");
header("Content-Length: " . strlen($response));
echo $response;
ob_flush();
flush();
$publish_message = new PublishMessage();
$publish_message->start();
}