PHP code example of maxbeckers / amazon-alexa-php

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

    

maxbeckers / amazon-alexa-php example snippets


use MaxBeckers\AmazonAlexa\Request\Request;
...
$requestBody  = file_get_contents('php://input');
$alexaRequest = Request::fromAmazonRequest($requestBody, $_SERVER['HTTP_SIGNATURECERTCHAINURL'], $_SERVER['HTTP_SIGNATURE']);

use MaxBeckers\AmazonAlexa\Validation\RequestValidator;
...
$validator = new RequestValidator();
$validator->validate($alexaRequest);

use MaxBeckers\AmazonAlexa\RequestHandler\RequestHandlerRegistry;
...
$requestHandlerRegistry = new RequestHandlerRegistry();
$requestHandlerRegistry->addHandler($myRequestHandler);

use MaxBeckers\AmazonAlexa\RequestHandler\RequestHandlerRegistry;
...
$requestHandler = $requestHandlerRegistry->getSupportingHandler($alexaRequest);
$response       = $requestHandler->handleRequest($alexaRequest);

header('Content-Type: application/json');
echo json_encode($response);
exit();

public function __construct()
{
    $this->supportedApplicationIds = ['my_amazon_skill_id'];
}

public function supportsRequest(Request $request): bool
{
    return $request->request instanceOf MaxBeckers\AmazonAlexa\Request\Request\Standard\IntentRequest &&
        'MyTestIntent' === $request->request->intent->name;
}

use MaxBeckers\AmazonAlexa\Helper\ResponseHelper;
...
public function handleRequest(Request $request): Response
{
    return $this->responseHelper->respond('Success :)');
}

$helper = new DeviceAddressInformationHelper();
$fullAddress = $helper->getAddress($request);
$countryAndPostalCode = $helper->getCountryAndPostalCode($request);

$ssmlGenerator = new SsmlGenerator();
$ssmlGenerator->say('one');
$ssmlGenerator->pauseStrength(SsmlGenerator::BREAK_STRENGTH_MEDIUM);
$ssmlGenerator->say('two');
$ssml = $ssmlGenerator->getSsml();
// $ssml === '<speak>one <break strength="medium" /> two</speak>'

composer