PHP code example of skollro / alexa-php-sdk

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

    

skollro / alexa-php-sdk example snippets


use Skollro\Alexa\Alexa;
use MaxBeckers\AmazonAlexa\Request\Request;

$request = Request::fromAmazonRequest(file_get_contents('php://input'), $_SERVER['HTTP_SIGNATURECERTCHAINURL'], $_SERVER['HTTP_SIGNATURE']);

Alexa::skill('amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
    ->intent('HelloIntent', function ($request, $response) {
        $response->say('Hello');
    })
    ->handle($request, function ($response) {
        header('Content-Type: application/json');
        echo json_encode($response);
    });

use MaxBeckers\AmazonAlexa\Request\Request;

$request = Request::fromAmazonRequest(file_get_contents('php://input'), $_SERVER['HTTP_SIGNATURECERTCHAINURL'], $_SERVER['HTTP_SIGNATURE']);

$alexa = Alexa::skill('amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX');

// define middlewares and request handlers...

$response = $alexa->handle($request);

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

$alexa->middleware(function ($next, $request, $response) {
    if (! $request->context->system->user->accessToken) {
        return $response->say('Link your account')->linkAccount();
    }

    return $next($request, $response);
});

$alexa->middleware(function ($next, $request, $response) {
    $response = $next($request, $response);

    // modify $response here...

    return $response;
});

class BarIntent
{
    public function __invoke($request, $response)
    {
        $response->say('Use an invokable class as request handler');
    }
}

$alexa->launch(function ($request, $response) {
    $response->say('Welcome to your skill');
});

$alexa->intent('HelloIntent', function ($request, $response) {
    $response->say('Hello world');
});

$alexa->intent('BarIntent', new BarIntent);

$alexa->intent('HelloIntent', function ($request, $response) {
     // Basic response
    $response->say('Hello world');

    // Use cards for showing information in a user's Alexa App
    $response->simple($title, $content);
    $response->standard($title, $content, $smallImageUrl, $largeImageUrl);
    $response->linkAccount();

    // Use fluent syntax
    $response->say('Link your account')->linkAccount();
});

$alexa->exception(function ($e, $request, $response) {
    if ($e instanceof MyException) {
        return $response->say('An error occurred');
    }

    throw $e;
});
 bash
composer