gpt4 book ai didi

php - 从 Amazon Alexa 技能包发送 HTTP post 请求

转载 作者:搜寻专家 更新时间:2023-10-31 21:04:54 25 4
gpt4 key购买 nike

我是网络服务器的初学者,正在尝试从 Amazon Echo 执行一些 PHP 脚本。

基本上我有一个正在运行的 apache 网络服务器(可通过端口 443 从互联网访问)。我的网络服务器中也有一个 PHP 脚本 https://web-server.mine/script.php

我已经通过基本的 Web 身份验证从 Web 浏览器成功运行了 PHP 脚本。

现在我正在尝试从 Amazon Alexa 技能包发出 POST 请求,但我不知道如何传递凭据以便它可以调用 URL。

最佳答案

我不是很清楚你在问什么,所以我会尽量回答我所有的解释。

  1. 如果您要确保是 Alexa Skill 正在尝试访问您的 URL,我发现最简单的身份验证方法是检查 POST 数据中的 applicationId:

    // Get raw POST data
    $post = file_get_contents( 'php://input' );

    // Decode the JSON into a stdClass object
    $post = json_decode( $post );

    // Check the applicationId to make sure it's your Alexa Skill
    if ( 'amzn1.echo-sdk-ams.app.[your-unique-value-here]' == $post->session->application->applicationId ) {
    // Insert code to run if the applicationId matches
    echo 'The applicationId matches!';
    } else {
    // Insert code to run if the applicationId does NOT match
    echo 'The applicationId does NOT match!';
    }

    如果走这条路,您需要确保创建一个有效的 SSL 证书,如下所述:Create a Private Key and Self-Signed Certificate for Testing

  2. 如果您尝试从脚本中向另一个 URL 发出 POST 请求,请尝试使用 file_get_contents(),如下所述:How to post data in PHP using file_get_contents?

附带说明一下,可能值得研究使用一组 PHP 类,这些类是为简化 Alexa Skills 的创建而开发的,例如:https://github.com/develpr/alexa-app

我个人在 Raspberry Pi 上运行 Node.js,我使用 Node 包 alexa-app-serveralexa-app 创建和托管多个技能容易多了。如果您对此感兴趣,只需谷歌“nodejs alexa-app-server”即可获取这些包的最新链接。

关于php - 从 Amazon Alexa 技能包发送 HTTP post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34259268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com