videlalvaro/azserverless

"PHP Custom Handler for Azure Functions"

dev-main 2020-11-18 06:55 UTC

This package is auto-updated.

Last update: 2024-11-18 15:56:40 UTC


README

This projects provides a PHP Custom Handler for Azure Functions.

Installation

In your composer.json add the following dependency:

"require": {
        "videlalvaro/azserverless": "*"
}

Then run:

composer update

Then start an Azure Functions project. In your host.json add the following:

"customHandler": {
    "description": {
        "defaultExecutablePath": "php",
        "arguments": [
            "-S",
            "0.0.0.0:%FUNCTIONS_CUSTOMHANDLER_PORT%",
            "vendor/videlalvaro/azserverless/bin/serverless.php"
        ]
    },
    "enableForwardingHttpRequest": false
},

See host.sample.json for an example of how this file should look like.

Finally, make copy the file local.settings.sample.json into your project and call it local.settings.json. Adapt the contents to include your connection strings in the AzureWebJobsStorage field.

Usage

In your Azure Functions project you will have one folder per serverless function.

To create a function called HttpTrigger, create a folder with the same name, then inside add two files: function.json and index.php. Here's their content:

{
  "disabled": false,
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

And the corresponding index.php file:

<?php
use Azserverless\Context\FunctionContext;

function run(FunctionContext $context) {

    $req = $context->inputs['req'];

    $context->log->info('Http trigger invoked');

    $query = json_decode($req['Query'], true);

    if (array_key_exists('name', $query)) {
        $message = 'Hello ' . $query['name'] . '!';
    } else {
        $message = 'Please pass a name in the query string';
    }

    return [
        'body' => $message,
        'headers' => [
            'Content-type' => 'text/plain'
        ]
    ];
}
?>

As you can see functions are provided with a FunctionContext object where they can access request data, and also log information to the console.

To learn more about the details of serverless on Azure, take a look at the Azure Functions documentation.

Deployment

Follow the instructions here to enable PHP 7.4 & make sure composer is run while deploying your function app to Azure: Configure a PHP app for Azure App Service

When the options for Azure CLI require a --name option, provide your Azure Functions app name.