1. Go to this page and download the library: Download xp-forge/lambda 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/ */
xp-forge / lambda example snippets
use com\amazon\aws\lambda\Handler;
class Greet extends Handler {
/** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */
public function target() {
return fn($event, $context) => sprintf(
'Hello %s from PHP %s via %s @ %s',
$event['name'],
PHP_VERSION,
$context->functionName,
$context->region
);
}
}
use com\amazon\aws\lambda\Handler;
class Greet extends Handler {
/** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */
public function target() {
$default= $this->environment->properties('task')->readString('greet', 'default');
return fn($event, $context) => sprintf(
'Hello %s from PHP %s via %s @ %s',
$event['name'] ?? $default,
PHP_VERSION,
$context->functionName,
$context->region
);
}
}
use com\amazon\aws\lambda\Handler;
class Greet extends Handler {
/** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */
public function target() {
return function($event, $context) {
$this->environment->trace('Invoked with ', $event);
return sprintf(/* Shortened for brevity */);
};
}
}
use com\amazon\aws\lambda\{Context, Handler, Stream};
class Streamed extends Handler {
public function target(): callable {
return function($event, Stream $stream, Context $context) {
$stream->use('text/plain');
$stream->write("[".date('r')."] Hello world...\n");
sleep(1);
$stream->write("[".date('r')."] ...from Lambda\n");
$stream->end();
};
}
}
public interface com.amazon.aws.lambda.Stream extends io.streams.OutputStream, lang.Closeable {
public function transmit(io.Channel|io.streams.InputStream $source, string $mimeType): void
public function use(string $mimeType): void
public function write(string $bytes): void
public function end(): void
public function flush(): void
public function close(): var
}
use com\amazon\aws\{Credentials, ServiceEndpoint};
use com\amazon\aws\lambda\Handler;
class WebSockets extends Handler {
/** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */
public function target() {
return function($event, $context) {
// Send message to WebSocket connection
$this->environment->endpoint('execute-api')
->in($context->region)
->using($event['requestContext']['apiId'])
->resource('/{stage}/@connections/{connectionId}', $event['requestContext'])
->transmit(['message' => 'Reply'])
;
return ['statusCode' => 200];
};
}
}
public class com.amazon.aws.lambda.Context implements lang.Value {
public string $awsRequestId
public string $invokedFunctionArn
public string $traceId
public string $clientContext
public string $cognitoIdentity
public string $deadline
public string $functionName
public string $functionVersion
public string $memoryLimitInMB
public string $logGroupName
public string $logStreamName
public string $region
public int $payloadLength
public function __construct(array $headers, array $environment)
public function remainingTime(?float $now): ?float
public function toString(): string
public function hashCode(): string
public function compareTo(var $value): int
}
public class com.amazon.aws.lambda.Environment {
public string $root
public [:string] $variables
public io.streams.StringWriter $writer
public util.PropertySource $properties
public function __construct(string $root, ?io.streams.StringWriter $writer)
public function taskroot(): io.Path
public function path(string $path): io.Path
public function tempDir(): io.Path
public function local(): bool
public function variable(string $name): ?string
public function credentials(): com.amazon.aws.Credentials
public function trace(var... $args): void
public function properties(string $name): util.PropertyAccess
}
public interface com.amazon.aws.lambda.Lambda {
public function process(var $event, com.amazon.aws.lambda.Context $context): var
}
public interface com.amazon.aws.lambda.Streaming {
public function handle(
var $event,
com.amazon.aws.lambda.Stream $stream,
com.amazon.aws.lambda.Context $context
): void
}