When you use PHP: Hypertext Preprocessor (PHP) for programming in Function Compute, you must define a PHP function as the handler function. This topic describes the structure and features of PHP event functions.

Definition

The following example shows the definition of a simple handler function, which is an event function:

<? php
function handler($event, $context) {
    return "hello world";
}        

Function name

handler must correspond to the handler field that you specify when you create a function. For example, if you set handler to index.handler when you create a function, Function Compute loads the handler function defined in the index.php file.

{
"key": "value"
}   

The $event parameter

The $event parameter is an input parameter of the function and indicates the data that is passed in when you call the event function. The value of this parameter is of the string type. PHP functions directly use the $event parameter that you specify, without performing any preprocessing. You can parse the $event parameter in the function as needed. For example, if a JSON string is passed in, you can convert it to an array. The following sample code shows the parsing process of the $event parameter.

In the code of the PHP function, the preceding $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 the function, such as the request ID (requestId) and the temporary identity credentials (securityToken). You can use the runtime information in your code. The value of the $context parameter is of the array type. The $context parameter is defined as follows:

[
  '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 lists the fields contained in the $context parameter.

Field Description
requestId The ID of the call request. This field is usually used to trace problems and count historical calls.
function Basic information of the current 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 credentials in your code to access the related service such as Object Storage Service (OSS). This allows you not to write your AccessKey pair in the function code. For more information, see Permissions for services.
service The information about the service to which the function belongs. This field contains the service name, the project and Logstore information of Log Service to which the service is connected, and qualifier and version_id that indicate the service version information. The qualifier parameter indicates the service version or alias that is specified when the function is called. The version_id parameter indicates the version of the service that is called.
region The region to which the function applies, such as cn-shanghai. For more information, see Regions and zones.
accountId The ID of the Alibaba Cloud account that calls the function. For more information, see Obtain 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 shows 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 'sucess';
};