This topic shows you how to perform operations by using SDK for PHP to call the required API operations with efficiency. For example, you can create and delete services and functions, and invoke functions.

Prerequisites

Before you begin, make sure that you have completed the following operations:

Sample code of SDK for PHP

The following code provides an example on how to create and delete services, functions, and triggers:

<? php

require_once __DIR__ . '/vendor/autoload.php';
use AliyunFC\Client;

fcClient = new Client([
    "endpoint" => '<Your Endpoint>',
    "accessKeyID" =>'<Your AccessKeyID>',
    "accessKeySecret" =>'<Your AccessKeySecret>'
]);

    // Create a service.
    fcClient->createService('service_name');

    /*
    Create a function.
    The main.zip package is in the current directory. The package contains the main.php file, which contains the my_handler and my_initializer functions.
    Set an environment variable. Example: {'testKey': 'testValue'}.
    */
    fcClient->createFunction(
        'service_name',
        array(
            'functionName' => $functionName,
            'handler' => 'index.handler',
            'runtime' => 'php7.2',
            'memorySize' => 128,
            'code' => array(
                'zipFile' => base64_encode(file_get_contents(__DIR__ . '/main.zip')),
            ),
           'description' => "test function",
           'environmentVariables' => ['testKey' => 'testValue'],
                )
            );

    // Invoke the function.
    fcClient->invokeFunction('service_name', 'function_name');

    /*
    Create an initializer function.
    The main.zip package is in the current directory. The package contains the main.php file, which contains the my_handler and my_initializer functions.
    Set an environment variable. Example: {'testKey': 'testValue'}.
    */
    fcClient->createFunction(
        'service_name_with_initializer',
        array(
            'functionName' => $functionName,
            'handler' => 'index.handler',
            'initializer' => 'index.initializer',
            'runtime' => 'php7.2',
            'memorySize' => 128,
            'code' => array(
            'zipFile' => base64_encode(file_get_contents(__DIR__ . '/main.zip')),
            ),
            'description' => "test function with initializer",
            'environmentVariables' => ['testKey' => 'testValue'],
            )
             );

    // Invoke the initializer function.
    fcClient->invokeFunction('service_name_with_initializer', 'function_name');

    // Create a trigger, such as an Object Storage Service (OSS) trigger.
    prefix = 'pre';
    suffix = 'suf';
    triggerConfig = [
        'events' => ['oss:ObjectCreated:*'],
        'filter' => [
            'key' => [
                'prefix' => $prefix,
                'suffix' => $suffix,
            ],
        ],
    ];
    sourceArn = 'acs:oss:cn-shanghai:12345678:bucketName';
    invocationRole = 'acs:ram::12345678:role/aliyunosseventnotificationrole';
    ret = $fcClient->createTrigger(
        'service_name',
        'function_name',
        [
            'triggerName' => 'trigger_name',
            'triggerType' => 'oss',
            'invocationRole' => $invocationRole,
            'sourceArn' => $sourceArn,
            'triggerConfig' => $triggerConfig,
        ]
    );


    // Invoke the function based on the input parameters.
    fcClient->invokeFunction('service_name', 'function_name', $payload='hello_world');


    // Asynchronously invoke the function.
    fcClient->invokeFunction('service_name', 'function_name', 'hello world', ['x-fc-invocation-type' => 'Async']);

    // Query services.
    fcClient->listServices();

    // Query the functions that meet the conditions specified by the prefix and limit parameters.
    fcClient->listFunctions('service_name', ['prefix' => 'hello', "limit" => 2]);

    // Query triggers.
    fcClient->listTriggers('service_name', 'function_name');

    // Delete the trigger that you create.
    fcClient->deleteTrigger('service_name', 'function_name', 'trigger_name');

    // Delete the function that you create.
    fcClient->deleteFunction('service_name', 'function_name');

    // Delete the service that you create.
    fcClient->deleteService('service_name');