4

I'm a beginner in web server and trying to execute some PHP script from Amazon Echo.

Basically I have an apache web server running (accessible from the internet over port 443). I also have a PHP script inside my web server https://web-server.mine/script.php

I've successfully run the PHP script from a web browser with a basic web authentication.

Now I'm trying to make a POST request from Amazon Alexa skills kit but I don't know how to pass the credentials so it could make a call to the URL.

Casper
  • 1,663
  • 7
  • 35
  • 62

1 Answers1

2

I am not entirely clear on what you are asking, so I will try to answer all of my interpretations.

  1. If you're trying to make sure that it is the Alexa Skill that is trying to access your URL, the easiest way that I've found to authenticate by checking the applicationId from the POST data:

    // 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!';
    }
    

    If going this route, you'll need to make sure that you create a valid SSL certificate as outlined here: Create a Private Key and Self-Signed Certificate for Testing

  2. If you're trying to make a POST request to another URL from within your script, try using file_get_contents() as described here: How to post data in PHP using file_get_contents?

On a side note, it may be worth looking into using a set of PHP classes that have been developed for making the creation of Alexa Skills much easier, like this one: https://github.com/develpr/alexa-app

I personally run Node.js on a Raspberry Pi, and I use the Node packages alexa-app-server and alexa-app to make creation and hosting of multiple skills much easier. If this is something that interests you, just Google "nodejs alexa-app-server" for the latest links to these packages.

Community
  • 1
  • 1
dale3h
  • 48
  • 3
  • 1
    I actually found an open source PHP code that can handles the utterances on my web server and it also uses the validation methods similar you what you mentioned too. I'm actually interested in the nodejs as well, thanks – Casper Dec 16 '15 at 17:21
  • 1
    BTW how do you create API endpoint on apache web server with nodejs? – Casper Dec 16 '15 at 17:28
  • @Casper If you're wanting to handle multiple "good looking" endpoints with one script, it's relatively simple by using $_SERVER['REQUEST_URI']: `echo "Requested API endpoint: " . htmlspecialchars( $_SERVER['REQUEST_URI'] );` Save this as something like "alexa.php" and then visit https://web-server.mine/alexa.php/my-skill-1 and https://web-server.mine/alexa.php/my-skill-2 – dale3h Dec 18 '15 at 05:53
  • 1
    I love Node.js. There are a plethora of packages available, and they are very quick and easy to install! Node.js is simple to install on just about any platform, and the scripts can be written in JavaScript. It's definitely worth checking out, regardless of whether you use it for your skill or not! More information about Node.js can be found at https://nodejs.org/en/ Once you have that installed, an HTTP server is simple to write: http://blog.modulus.io/build-your-first-http-server-in-nodejs However, If you do decide to use Node.js for your skill, I highly recommend `alexa-app-server` – dale3h Dec 18 '15 at 06:01
  • I don't make app to sell, just for my home automation. But I actually tried to create a nodejs server, it was easy, but I actually have other stuff running on apache with SSL and whatnot, so I decided to keep using the PHP with apache for now since it does what I need, thanks though. – Casper Dec 18 '15 at 17:05