Download the PHP package tkotosz/fn-fdk-php without Composer
On this page you can find all versions of the php package tkotosz/fn-fdk-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package fn-fdk-php
PHP Fn Development Kit (FDK)
fn-fdk-go provides convenience functions for writing php fn code
Installing fn-fdk-php
You can install it manually with composer:
Or just use the php init image to create new funtion like this:
This will generate the necessary files for your function including the composer json and docker file to install this fdk.
Creating a PHP Function
Writing a PHP function is simply a matter of writing a handler function that you pass to the FDK to invoke each time your function is called.
Start by creating a php function with fn init
:
This creates a simple hello world function in func.php
:
The handler function takes the input that is sent to the function and returns a response. By default the input is treated as json which automatically converted to an array and the response is also an array (or json serializable object) which automatically converted to json. Using the FDK you don't have to worry about reading the http request or sending back the response. The FDK let's you focus on your function logic and not the mechanics.
Now run it!
You should see the following output:
Run it with input:
You should see the following output:
Now you have a basic running php function that you can modify and add what you want.
Function Context
Function invocation context details are available through an optional function argument.
To receive a context object, simply add a second argument to your handler function.
In the following example the callId
is obtained from the context and included in
the response message:
Run it:
You should see a similar output:
The context contains other context information about the request such as:
ctx->getConfig
: An object containing function config variables (from the environment variables)ctx->getHeaders
: An object containing input headers for the event as lists of stringsctx->getDeadline
: ADateTimeImmutable
object indicating when the function call must be processed byctx->getCallId
: The call ID of the current callctx->getId
: The function ID of the current functionctx->getMemory
: Amount of ram in MB allocated to this functionctx->getContentType
: The incoming request content type (if set, otherwise null)ctx->setResponseHeader(key,values...)
: Sets a response header to one or more valuesctx->addResponseHeader(key,values...)
: Appends values to an existing response headerctx->responseContentType
: Sets the response content type of the functionctx->setResponseStatus
: Sets the response status code of the function (default: 200)
Handling input/output
By default the FDK will try to json decode the input, likewise by default the output of a function will be treated as a JSON object and converted using json_encode()
.
To change the handling of the input you can add an additional options
parameter to fdk->handle
that specifies the input handling strategy:
valid input modes are:
json
(the default) attempts to parse the input as jsonstring
always treats input as a stringstream
passes the input stream (streaming request body) to your function
To change the output handling of your function from the default you should wrap the result value using a response decorator:
the available decorators are:
rawResult({string|ReadableStreamInterface})
passes the result directly to the response - the value can be a string or a readable streamstreamResult({resource|ReadableStreamInterface})
pipes the contents of theresource
orReadableStreamInterface
into the output - this allows processing of data from files or HTTP responses
Using HTTP headers and setting HTTP status codes
You can read http headers passed into a function invocation using $ctx->getHeaderValue($key)
, this returns the first header value of the header matching key
or you can use $ctx->getHeaders()
or $ctx->getHeaderValues($key)
methods as well.
Outbound headers and the HTTP status code can be modified in a similar way:
Examples
See examples here.