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

The following operations are completed:

Example

<?php

require_once __DIR__ . '/vendor/autoload.php';
use AliyunFC\Client;
/*
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. 
We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources under your account may be compromised. 
In this example, the AccessKey pair is saved to the environment variables for authentication. 
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your local environment before you run the sample code. 
The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are automatically configured after you configure the execution permissions in the runtime of Function Compute. 
*/
$fcClient = new Client([
    "endpoint" => '<Your Endpoint>',
    "accessKeyID" => getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'),
    "accessKeySecret" => getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET')
]);

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

    /*
    Creates a function. 
    The current directory has a main.zip file. index.php contains handler and the Initializer function. 
    Set an environment variable. Example: {'testKey': 'testValue'}. 
    */
    $fcClient->createFunction(
        'service_name',
        array(
            'functionName' => "function_name",
            '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",
           'environmentVariables' => ['testKey' => 'testValue'],
                )
            );

    // Invoke the function. 
    $fcClient->invokeFunction('service_name', '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,
            ],
        ],
    ];

    // Replace accountId and bucketName with the actual values.
    $sourceArn = 'acs:oss:cn-shanghai:<AccountID>:bucketName';
    $invocationRole = 'acs:ram::<AccountID>: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. 
    $fcClient->deleteTrigger('service_name', 'function_name', 'trigger_name');

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

    // Delete the service. 
    $fcClient->deleteService('service_name');