Giter Club home page Giter Club logo

azurehelper's Introduction

Azure Functions PHP Helper

A helper package to create PHP based Azure Function

How to setup

Fir do not forget to create your azure environment. Or if your function is ready, just add library.

func init

Later, please add PHP Helper library.

composer require gazatem/azurehelper

Now, you need to update host.js file. All you need is to update Cumtom Handler section.

{
"customHandler": {
    "description": {
    "defaultExecutablePath": "php",
    "arguments": [
	    "-S",
	    "0.0.0.0:%FUNCTIONS_CUSTOMHANDLER_PORT%",
	    "worker.php"
	    ]
	    },
    "enableForwardingHttpRequest": false
    }
  }

Now this is the last step. You need to create worker.php to run your function(s).

<?php
declare(strict_types=1);
error_reporting(E_ERROR | E_PARSE);
require_once __DIR__ . '/vendor/autoload.php';
$request = new Gazatem\Phpfunc\Request();
$context = new Gazatem\Phpfunc\FunctionContext();
require __DIR__  . $request->getRequestUri() . '/index.php';
$request->getRequestBody();
$context->request = $request;
$returnValue = run($context);

$response = [
'Outputs' => $context->outputs,
'ReturnValue' => $returnValue,
'Logs' => $context->log->getMessages()
];
header("Content-type: application/json");
echo(json_encode($response));

I want to add here a simple Azure Function for you.

<?php
use Gazatem\Phpfunc\FunctionContext;
function run(FunctionContext $context) {

// Post or Get request
$query = $context->request->query;

// Json body
$body = json_decode($context->request->body, true);

$message = "Hello ". $query['name'];
return [
    'body' => $message,
    'headers' => [
        'Content-type' => 'text/plain'
    ]
];
}

Helper Attributes

This is the list that you can call from Helper:

  • requestUri
  • requestBody
  • data
  • req
  • url
  • method
  • query
  • headers
  • params
  • identities
  • body

Logging & Exceptions

I added a ready to use logger to output to console. I'll add more features for logging and exception.

$context->log->warning('Get method not worked!');

This is just a starter that I used in my works. I added example usage in example folder.

azurehelper's People

Contributors

kemalyen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.