PHP code example of leoplatform / php

1. Go to this page and download the library: Download leoplatform/php 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/ */

    

leoplatform / php example snippets




if (!defined('IN_LEO')) {
	// only call this from within the leo app
	exit;
}

$env_name = 'PHP_ENV';
$env = getenv($env_name);

switch ($env) {
	case 'dev':
	default:
		$config = [
			'leoaws' => [
				'profile'   => 'leotest',
				'region'    => 'us-west-2',
			],
			'leosdk' => [
				'region' => 'us-west-2',
				'resources' => [
					'LeoStream'         => 'LeoTestBus-LeoStream-10FA3RB1DTZDR',
					'LeoCron'           => 'LeoTestBus-LeoCron-7M9FPAWMLT8R',
					'LeoEvent'          => 'LeoTestBus-LeoEvent-Y1USS8D22B25',
					'LeoSettings'       => 'LeoTestBus-LeoSettings-UYVBZUNIWVNC',
					'LeoSystem'         => 'LeoTestBus-LeoSystem-U9IAZZQITSL1',
					'LeoS3'             => 'leotestbus-leos3-si2vssrd13ya',
					'LeoKinesisStream'  => 'LeoTestBus-LeoKinesisStream-1AYT0L5T0OPBM',
					'LeoFirehoseStream' => 'LeoTestBus-LeoFirehoseStream-1KR1P9Y50OT8R',
					'Region'            => 'us-west-2',
				],
				'kinesis'   => 'LeoTestBus-LeoKinesisStream-1AYT0L5T0OPBM',
				's3'        => 'leotestbus-leos3-si2vssrd13ya',
				'firehose'  => 'LeoTestBus-LeoFirehoseStream-1KR1P9Y50OT8R',
			],
			'leoauth' => [
				'region' => 'us-west-2',
				'resources' => [
					'Region'            => 'us-west-2',
					'LeoAuth'           => 'LeoTestAuth-LeoAuth-7W9Y5BV1DV6Q',
					'LeoAuthUser'       => 'LeoTestAuth-LeoAuthUser-10TEEBU3R6G9B',
					'LeoAuthIdentity'   => 'LeoTestAuth-LeoAuthIdentity-YN1KPFYLMET5',
					'LeoAuthPolicy'     => 'LeoTestAuth-LeoAuthPolicy-9SYIP05HBKTF',
				],
			],
			'enableLogging' => false,
			'debug'         => false,
		];
	break;

	case 'staging':
		$config = [
			'leoaws' => [
				'profile'   => 'leo',
				'region'    => 'us-west-2',
			],
			'leosdk' => [
				'region' => 'us-west-2',
				'resources' => [
					'LeoStream'         => 'nestedTest1-Bus-1719AK03AAD1-LeoStream-1PY4VIHESLXJ9',
					'LeoCron'           => 'nestedTest1-Bus-1719AK03AAD1-LeoCron-E6Z5B776RE2H',
					'LeoEvent'          => 'nestedTest1-Bus-1719AK03AAD1-LeoEvent-1X2ZR5Y4S9XSK',
					'LeoSettings'       => 'nestedTest1-Bus-1719AK03AAD1-LeoSettings-199TCN54F27AQ',
					'LeoSystem'         => 'nestedTest1-Bus-1719AK03AAD1-LeoSystem-1OA77QUNZAH0U',
					'LeoS3'             => 'nestedtest1-bus-1719ak03aad1-leos3-pgljot8blqgw',
					'LeoKinesisStream'  => 'nestedTest1-Bus-1719AK03AAD1-LeoKinesisStream-7LTMBRYPBAI5',
					'LeoFirehoseStream' => 'nestedTest1-Bus-1719AK03AAD1-LeoFirehoseStream-Y34YJGWNRO3U',
					'Region'            => 'us-west-2',
				],
				'kinesis'   => 'nestedTest1-Bus-1719AK03AAD1-LeoKinesisStream-7LTMBRYPBAI5',
				's3'        => 'nestedtest1-bus-1719ak03aad1-leos3-pgljot8blqgw',
				'firehose'  => 'nestedTest1-Bus-1719AK03AAD1-LeoFirehoseStream-Y34YJGWNRO3U',
			],
			'leoauth' => [
				'region' => 'us-west-2',
				'resources' => [
					'Region'            => 'us-west-2',
					'LeoAuth'           => 'nestedTest1-Auth-FNCUKVEJGXP4-LeoAuth-44CCD6TCUF0N',
					'LeoAuthUser'       => 'nestedTest1-Auth-FNCUKVEJGXP4-LeoAuthUser-1FNK1G58XA8QM',
					'LeoAuthIdentity'   => 'nestedTest1-Auth-FNCUKVEJGXP4-LeoAuthIdentity-1E8F4X1TFDJ9L',
					'LeoAuthPolicy'     => 'nestedTest1-Auth-FNCUKVEJGXP4-LeoAuthPolicy-8PDU1RYSKZ2U',
				],
			],
			'enableLogging' => false,
			'debug'         => false,
		];
	break;

	case 'production':
	break;
}

define('IN_LEO', true);
 automatically load if in the same directory as this file. If not, explicitly load it:
new \Leo\lib\Config('./leo_config.php');

//create a Leo-sdk object
$leo = new Leo\Sdk("BotName");

//These are optional parameters, see the docs for possible values
$stream_options = [];

//function is called with every commit to the stream
//returns data about the checkpoint
$checkpoint_function = function ($checkpointData) {
	var_dump($checkpointData);
};

// create a loader stream
$stream = $leo->createLoader($checkpoint_function, $stream_options);

for($i = 0; $i < 100000; $i++) {
	$event = [
		"id"=>"testing-$i",
		"data"=>"some order data",
		"other data"=>"some more data"
	];
	$meta = ["source"=>null, "start"=>$i];
  
	//write an event to the stream
	$stream->write("QueueName", $event, $meta);
}
$stream->end();

$bot_name = "EnrichmentBot";
$in_queue_name = "QueueName";
$enriched_queue_name = "EnrichedQueueName";
$read_options = ['limit'=>100000, 'run_time'=> "4 minutes"];

$transform_function = function ($event, $meta) {
			var_dump($event);
			$event["newdata"] = "this is some new data";
			return $event;
		};

$leo = new Leo\Sdk($bot_name, $config);

$stream = $leo->createEnrichment($in_queue_name, $transform_function, $enriched_queue_name, $read_options );
 
	$bot_name = 'PHPOffloaderBot';
	$queue_name = 'PHPEnrichedQueue';
	$read_options = ['limit' => 500, 'run_time' => '4 minutes'];

	$leo = new Leo\Sdk($bot_name);
	$checkpointCount = $count = 0;

	$reader = $leo->createOffloader($queue_name, $read_options);
	foreach ($reader->events as $i => $event) {
		$count++;
		\Leo\lib\Utils::log($event);
		
		// print out events so we can see them - for debugging
		// print_r($event);

		if (++$checkpointCount == 1000) {
			$reader->checkpoint();
			$checkpointCount = 0;
		}
		
		// sleep for 0.1 of a second to be able to read events as they pass through the command line - for debugging
		// usleep(100000);
	}
	
	// checkpoint after we're done to tell the bot where we stopped last
	$reader->checkpoint();
	\Leo\lib\Utils::log(++$count);


curl -sS https://getcomposer.org/installer | php
php composer.phar 

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/LeoPlatform/PHP.git"
        }
    ],
    "

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install