PHP Function Development Overview
FunctionGraph supports the following PHP runtimes:
- PHP 7.3
- PHP 8.3
Function Syntax
Use the following syntax when creating a handler function in PHP:
function handler($event, $context)
- $handler: name of the function that FunctionGraph invokes to execute your code. The name must be consistent with that you define when creating a function.
- $event: event parameter defined for the function. The parameter is in JSON format.
- $context: runtime information provided for executing the function. For details, see SDK APIs.
- Function handler: index.handler.
The PHP function handler is in the format of [File name].[Function name]. You can configure or modify the handler on the FunctionGraph console.
PHP Initializer
For details about the initializer, see Initializer.
The initializer format of a PHP function is [File name].[Initializer name].
For example, if the initializer is named main.my_initializer, FunctionGraph loads the my_initializer function defined in the main.php file.
To use PHP to build initialization logic, define a PHP function as the initializer. The following is a simple initializer:
<?php
Function my_initializer($context) {
echo 'hello world' . PHP_EOL;
}
?>
- Function name
The function name my_initializer must be the initializer function name specified for a function.
For example, if the initializer is named main.my_initializer, FunctionGraph loads the my_initializer function defined in the main.php file.
- context
The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata.
SDK APIs
The following table describes the context methods provided by FunctionGraph.
|
Method |
Description |
|---|---|
|
getRequestID() |
Obtains a request ID. |
|
getRemainingTimeInMilliSeconds () |
Obtains the remaining running time of a function. |
|
getAccessKey() |
Obtains the AK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function. FunctionGraph has stopped maintaining the getAccessKey API in the Runtime SDK. You cannot use this API to obtain a temporary AK. |
|
getSecretKey() |
Obtains the SK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function. FunctionGraph has stopped maintaining the getSecretKey API in the Runtime SDK. You cannot use this API to obtain a temporary SK. |
|
getSecurityAccessKey() |
Obtains the SecurityAccessKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function. |
|
getSecuritySecretKey() |
Obtains the SecuritySecretKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function. |
|
getSecurityToken() |
Obtains the SecurityToken (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function. |
|
getUserData(string key) |
Uses keys to obtain the values passed by environment variables. |
|
getFunctionName() |
Obtains the name of a function. |
|
getRunningTimeInSeconds () |
Obtains the timeout of a function. |
|
getVersion() |
Obtains the version of a function. |
|
getMemorySize() |
Obtains the allocated memory. |
|
getCPUNumber() |
Obtains CPU usage of a function. |
|
getPackage() |
Obtains a function group. |
|
getToken() |
Obtains the token (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function. |
|
getLogger() |
Obtains the logger method provided by the context and returns a log output class. Logs are output in the format of Time-Request ID-Content by using the info method. For example, use the info method to output logs: logg = context.getLogger()$ $logg->info("hello") |
|
getAlias() |
Obtains function alias. |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot