When you use Hypertext Preprocessor (PHP) for programming in Function Compute, you must define a PHP function as a handler. This topic describes the structure and features of PHP event functions.
Event function handler
The following sample code defines a simple handler function:
<?php
function handler($event, $context) {
return "hello world";
}
Handler function name
The handler corresponds to the handler
parameter that you specify when you create a function. For example, if you specify
index.handler
as the handler when you create a function, Function Compute loads the handler
function defined in the index.php
file.
The $event parameter
The $event
parameter is an input parameter of a function, which contains data that is passed
in when you invoke the function. The value of this parameter is of the STRING type.
PHP functions use the $event
parameter that you specify without preprocessing. You can parse the $event
parameter in the functions as needed. For example, the following sample JSON string
is passed in. You can convert it to an array.
{
"key": "value"
}
In the following PHP function code, the $event
parameter is parsed, and value
is returned.
<?php
function handler($event, $context) {
$eventObj = json_decode($event, $assoc = true);
return $eventObj['key'];
}
The $context parameter
The $context
parameter contains the runtime information of a function, such as the ID of the request
specified by the requestId
parameter and the temporary identity credentials specified by the securityToken
parameter. You can use the runtime information in your code. The value of the $context
parameter is of the ARRAY type. The following sample code defines the $context parameter:
[
'requestId' => 'b1c5100f-819d-c421-3a5e-7782a27d8a33',
'credentials' => [
'accessKeyId' => 'STS.access_key_id',
'accessKeySecret' => 'access_key_secret',
'securityToken' => 'security_token',
],
'function' => [
'name' => 'my-func',
'handler' => 'index.handler',
'memory' => 128,
'timeout' => 10,
],
'service' =>[
'name' => 'my-service',
'logProject' => 'my-log-project',
'logStore' => 'my-log-store',
'qualifier' => 'qualifier',
'versionId' => '1',
'initializer' => 'index.initializer',
'initializationTimeout' => 10,
],
'region' => 'cn-shanghai',
'accountId' => '123456'
]
The following table describes the parameters contained in the $context
parameter.
Parameter | Description |
---|---|
requestId | The unique ID of the invocation request. The value of this parameter is used to troubleshoot issues and count historical invocations. |
function | The basic information of the invoked function, such as the name, handler, memory, and timeout period of the function. |
credentials | The temporary AccessKey pair obtained by Function Compute from assuming your service role. The temporary AccessKey pair is updated every 15 minutes. You can use such credentials in your code to access related services such as Object Storage Service (OSS). This allows you not to write your AccessKey pair in the function code. For more information, see Grant permissions to a RAM user by using an Alibaba Cloud account. |
service | The information about the service to which the function belongs, such as the name,
the related project and Logstore in Log Service, the version, and the alias of the
service. The qualifier parameter specifies the version or alias of the service. The version_id parameter specifies the version of the service.
|
region | The ID of the region to which the function applies, such as cn-shanghai. For more information, see Endpoints. |
accountId | The ID of the Alibaba Cloud account that is used to invoke the function. For more information, see Obtain your Alibaba Cloud account ID. |
Example
The temporary AccessKey pair is used to verify the identity and permissions of the
request sender. When you use a temporary AccessKey pair to access other services such
as OSS, you must specify the $securityToken
parameter. The following sample code provides an example on how to upload a .txt
file to an OSS bucket by using a temporary AccessKey pair:
<?php
use OSS\OssClient;
function handler($event, $context) {
$accessKeyId = $context["credentials"]["accessKeyId"];
$accessKeySecret = $context["credentials"]["accessKeySecret"];
$securityToken = $context["credentials"]["securityToken"];
$endpoint = "oss-cn-shenzhen.aliyuncs.com";
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken);
$bucket = "my-bucket";
$object = "php.txt";
$content = "Hello fc.";
try {
$ossClient->putObject($bucket, $object, $content);
} catch (OssException $e) {
print($e->getMessage());
}
return "success";
};