PHP code example of topic-advisor / php-lambda-runtime-api

1. Go to this page and download the library: Download topic-advisor/php-lambda-runtime-api 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/ */

    

topic-advisor / php-lambda-runtime-api example snippets




namespace App;

use TopicAdvisor\Lambda\RuntimeApi\InvocationRequestHandlerInterface;
use TopicAdvisor\Lambda\RuntimeApi\InvocationRequestInterface;
use TopicAdvisor\Lambda\RuntimeApi\InvocationResponseInterface;
use TopicAdvisor\Lambda\RuntimeApi\Http\HttpRequestInterface;
use TopicAdvisor\Lambda\RuntimeApi\Http\HttpResponse;

class ApplicationHandler implements InvocationRequestHandlerInterface
{
    /**
     * @param InvocationRequestInterface $request
     * @return bool
     */
    public function canHandle(InvocationRequestInterface $request): bool
    {
        return $request instanceof HttpRequestInterface;
    }

    /**
     * @param InvocationRequestInterface $lambdaRequest
     * @return InvocationResponseInterface
     * @throws \Exception
     */
    public function handle(InvocationRequestInterface $lambdaRequest): InvocationResponseInterface
    {
        // Execute your application code and return an instance of InvocationResponseInterface
        
        $response = new HttpResponse($lambdaRequest->getInvocationId());
        $response->setStatusCode(200);
        $response->setHeaders(['content-type' => 'application/json']);
        $response->setBody('{"hello":"world"}');
        
        return $response;
    }
    
    /**
     * @param InvocationRequestInterface $request
     * @return void
     */
    public function preHandle(InvocationRequestInterface $request)
    {
        // Do any pre-request handling
    }

    /**
     * @param InvocationRequestInterface $request
     * @param InvocationResponseInterface $response
     * @return void
     */
    public function postHandle(InvocationRequestInterface $request, InvocationResponseInterface $response)
    {
        // Do any post-request handling such as unsetting variables, resetting services, etc
    }
}


#!/opt/bin/php


bda\RuntimeApi\RuntimeApiLoop;


$loop = new RuntimeApiLoop();
$loop
    ->setHandlers([
        new App\ApplicationHandler(),
    ])
    ->run();

#!/opt/bin/php


t\Dotenv\Dotenv;
use TopicAdvisor\Lambda\RuntimeApi\Client\CliClient;
use TopicAdvisor\Lambda\RuntimeApi\RuntimeApiLoop;

$options = [];
if (getenv('APP_LOCAL')) {
    if (!class_exists(Dotenv::class)) {
        throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
    }
    (new Dotenv())->load(__DIR__.'/.env');
    $options[RuntimeApiLoop::OPTION_CLIENT_CLASS] = CliClient::class;
}

$loop = new TopicAdvisor\Lambda\RuntimeApi\RuntimeApiLoop($options);
$loop
    ->setHandlers([
        new App\ApplicationHandler(),
    ])
    ->run();

 $ APP_LOCAL=1 php bootstrap.php