gruessung / amazon-alexa-php
PHP的Amazon Alexa接口
0.1.5
2015-11-29 21:50 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ~4.6
This package is not auto-updated.
Last update: 2024-09-29 02:41:12 UTC
README
变更(分支原因)
- 自动验证时间戳
- 将时间戳验证更改为当前认证指南
此库提供了一个方便的接口,用于开发您PHP应用程序的Amazon Alexa技能。
用法
通过composer安装: composer require gruessung/amazon-alexa-php
。
请求
当Amazon Alexa触发您的技能时,将向您为应用程序指定的URL发送HTTP请求。
您可以如此解析请求的JSON
正文
$jsonDataAsArray = $request->json()->all(); // This is how you would retrieve this with Laravel $alexaRequest = \Alexa\Request\Request::fromData($jsonDataAsArray);
您可以使用instanceof
确定请求的类型,例如
if ($alexaRequest instanceof IntentRequest) { // Handle intent here }
响应
您可以使用Response
类构建Alexa响应。您还可以选择设置卡片或重置提示。
以下是一些示例。
$response = new \Alexa\Response\Response; $response->respond('Cooool. I\'ll lower the temperature a bit for you!') ->withCard('Temperature decreased by 2 degrees');
$response = new \Alexa\Response\Response; $response->respond('What is your favorite color?') ->reprompt('Please tell me your favorite color');
要输出响应,只需使用->render()
函数即可,例如,在Laravel中,您将创建响应如下
return response()->json($response->render());
纯PHP
header('Content-Type: application/json'); echo json_encode($response->render()); exit;
待办事项
- 自动验证请求时间戳的完整性