migratorydata/migratorydata-client-php

MigratoryData Client API for PHP

6.0.4 2023-07-31 10:56 UTC

This package is not auto-updated.

Last update: 2024-12-16 17:54:33 UTC


README

Below you can find a tutorial and usage example. For more information please refer to MigratoryData Documentation 6.x.

Usage

Install the MigratoryData client library 5.x using composer (MigratoryData client version 5 can be used with MigratoryData Server 5.0.*):

composer require migratorydata/migratorydata-client-php:5.*

Install the MigratoryData client library 6.x using composer (MigratoryData client version 6 can be used with the MigratoryData server 6.0.1 or later):

composer require migratorydata/migratorydata-client-php:6.*

Create a MigratoryData client:

require __DIR__ . '/vendor/autoload.php';

use MigratoryData\Client\MigratoryDataClient;
use MigratoryData\Client\MigratoryDataMessage;

$client = new MigratoryDataClient(); 

Initialize the MigratoryData client:

$client->setEntitlementToken("some-token");
$client->setServers(array("http://127.0.0.1:8800"));

// call connect for client API for PHP v6
$client->connect();

Publish a message to MigratoryData server:

$message = new MigratoryDataMessage("/server/status", time());
$response = $client->publish($message);

Example client application

Copy the code below to a file named echo-time-client.php and run it using the following command:

php echo-time-client.php

Example for Client API for PHP V5 The client application connects to the MigratoryData server deployed at localhost:8800 and publishes a message every second on the subject /server/status.

If you don't have a MigratoryData server installed on your machine but there is docker installed you can run the following command to start MigratoryData server, otherwise you can download and install the latest version for your os from here.

docker pull migratorydata/server:latest
docker run -d --name my_migratorydata -p 8800:8800 migratorydata/server:latest
<?php
require __DIR__ . '/vendor/autoload.php';

use MigratoryData\Client\MigratoryDataClient;
use MigratoryData\Client\MigratoryDataException;
use MigratoryData\Client\MigratoryDataMessage;

$client = new MigratoryDataClient();
$client->setEntitlementToken("some-token");

try {
    $client->setServers(array("http://127.0.0.1:8800"));
} catch (MigratoryDataException $e) {
	// Exceptions with one of the error codes: 
	//		E_INVALID_URL_LIST
	//		E_CLUSTER_MEMBERS_CONNECTION_FAILED
	//		E_ENTITLEMENT_TOKEN
	// See the documentation of MigratoryDataException for more details
	echo("Exception: " . $e->getDetail() . "\n");
	exit(1);
}
while(true) {
    $start = microtime(true);

    $message = new MigratoryDataMessage("/server/status", time());
    try {
        $response = $client->publish($message);
    } catch (MigratoryDataException $e) {
		// Exception with one of the error codes:
		//		E_MSG_NULL
		//		E_MSG_FIELD_INVALID
		//		E_MSG_INVALID
		//		E_INVALID_SUBJECT
		//		E_INVALID_PROTOCOL
		// See the documentation of MigratoryDataException for more details
		echo("Exception: " . $e->getDetail() . "\n");
    }

    $end = microtime(true);

    echo $response . ' ' . ($end - $start) . ' ms' . "\n";

    sleep(1);
} 

Example for Client API for PHP V6 The client application connects to the MigratoryData server deployed at localhost:8800 and publishes a message every second on the subject /server/status.

<?php

require __DIR__ . '/vendor/autoload.php';

use MigratoryData\Client\MigratoryDataClient;
use MigratoryData\Client\MigratoryDataException;
use MigratoryData\Client\MigratoryDataMessage;

// Create a MigratoryData client
$client = new MigratoryDataClient();

try 
{
	// Attach the entitlement token to the client.
	$client->setEntitlementToken("some-token");

	// Connect to a MigratoryData server
	$client->setServers(array("http://127.0.0.1:8800"));
	$client->connect();
} 
catch (MigratoryDataException $e) 
{
	// Exceptions with one of the error codes: 
	//		E_INVALID_URL_LIST
	//		E_CLUSTER_MEMBERS_CONNECTION_FAILED
	//		E_ENTITLEMENT_TOKEN
	//		E_RUNNING
	//      E_CONNECTION_DENY
	// See the documentation of MigratoryDataException for more details
	echo("Exception: " . $e->getDetail() . "\n");
	exit(1);
}

while (true) 
{
	// Publish a message
	try 
	{
        $message = new MigratoryDataMessage("/server/status", time());
        $response = $client->publish($message);
		echo("Got response: " . $response . "\n");
	} 
	catch (MigratoryDataException $e) 
	{
		// Exception with one of the error codes:
        //		E_NOT_CONNECTED
		//		E_MSG_NULL
		//		E_MSG_INVALID
		//		E_INVALID_SUBJECT
		//		E_INVALID_PROTOCOL
		// See the documentation of MigratoryDataException for more details
		echo("Exception: " . $e->getDetail() . "\n");
	}
	
	sleep(1);
}