This topic describes how to install the CloudFlow PHP SDK, initialize a client, and call API operations to create, query, and start workflow executions.
Prerequisites
Before you begin, make sure that you have:
PHP 5.6 or later
Composer installed globally
An AccessKey pair. For more information, see Create an AccessKey pair
The PHP version used to install the SDK through Composer must be earlier than or equal to the PHP version used to run the SDK. For example, a vendor folder generated on PHP 7.2 works only on PHP 7.2 or later. If Composer installation fails due to network issues, switch to the Alibaba Cloud mirror:
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/Step 1: Install the SDK
CloudFlow SDK supports two call types. Choose one based on your use case:
Specialized calls (recommended) -- Typed request and response classes for each API operation, with IDE autocompletion and compile-time checks.
Generic calls -- A single core package to call any Alibaba Cloud API without service-specific classes.
For more information about the differences, see Generic calls and specialized calls.
Specialized calls
Run the following command to install the CloudFlow SDK package:
composer require alibabacloud/fnf-20190315 1.1.3To find the latest version and installation instructions, visit the CloudFlow SDK page in OpenAPI Explorer. Select PHP in the All languages field, then copy the installation code from the Installation Method section.
Generic calls
Install the darabonba-openapi core package instead of a service-specific SDK:
composer require alibabacloud/darabonba-openapiFor the latest version, see tea-openapi.
Step 2: Initialize the client
Set the endpoint based on the region where you use CloudFlow. The endpoint format is {region-id}.fnf.aliyuncs.com. For a full list of supported regions and endpoints, see Regions.
The following example initializes a client for specialized calls by using an AccessKey pair stored in environment variables. For other authentication methods, see Manage access credentials. To configure environment variables, see Configure environment variables in Linux, macOS, and Windows.
To initialize a client for generic calls, see Generic calls and specialized calls.
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. Do not save AccessKey credentials in your project code. Store them in environment variables instead.
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\SDK\Fnf\V20190315\Fnf;
use Darabonba\OpenApi\Models\Config;
$config = new Config([
// Make sure that ALIBABA_CLOUD_ACCESS_KEY_ID is set in your environment
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
// Make sure that ALIBABA_CLOUD_ACCESS_KEY_SECRET is set in your environment
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
]);
// Replace with your region endpoint. Example: cn-hangzhou.fnf.aliyuncs.com
$config->endpoint = "cn-hangzhou.fnf.aliyuncs.com";
$client = new Fnf($config);Step 3: Call API operations
After you initialize a client, use it to call CloudFlow API operations. All examples in this topic share the same client initialization and error handling pattern.
Handle errors
Wrap every API call in error handling. The following pattern applies to all examples in this topic:
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
$runtime = new RuntimeOptions([]);
try {
// API call goes here
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
var_dump($error->message);
var_dump($error->data["Recommend"]);
Utils::assertAsString($error->message);
}CreateFlow
Call CreateFlow to create a workflow. Define the workflow by using Flow Definition Language (FDL). CloudFlow supports both the legacy and current FDL syntax.
use AlibabaCloud\SDK\Fnf\V20190315\Models\CreateFlowRequest;
$createFlowRequest = new CreateFlowRequest([
"name" => "my_flow_name",
"definition" => '
Type: StateMachine
SpecVersion: v1
Name: my_flow_name
StartAt: my_state
States:
- Type: Pass
Name: my_state
End: true
',
"description" => "your test flow",
"type" => "FDL"
]);
$runtime = new RuntimeOptions([]);
$client->createFlowWithOptions($createFlowRequest, $runtime);| Parameter | Description | Example |
|---|---|---|
name | Workflow name | my_flow_name |
definition | Workflow definition in FDL syntax | See code above |
description | Workflow description | your test flow |
type | Definition language type | FDL |
Legacy FDL syntax
type: flow
version: v1
name: my_flow_name
steps:
- type: pass
name: mypassDescribeFlow
Call DescribeFlow to query information about a workflow.
use AlibabaCloud\SDK\Fnf\V20190315\Models\DescribeFlowRequest;
$describeFlowRequest = new DescribeFlowRequest([
"name" => "my_flow_name"
]);
$runtime = new RuntimeOptions([]);
$client->describeFlowWithOptions($describeFlowRequest, $runtime);StartExecution
Call StartExecution to start a workflow execution asynchronously.
use AlibabaCloud\SDK\Fnf\V20190315\Models\StartExecutionRequest;
$startExecutionRequest = new StartExecutionRequest([
"flowName" => "your_flow_name",
"executionName" => "your_exec_name",
"input" => '{"key":"value"}',
// Call back TaskToken-related tasks after the execution ends
"callbackFnFTaskToken" => "12"
]);
$runtime = new RuntimeOptions([]);
$client->startExecutionWithOptions($startExecutionRequest, $runtime);| Parameter | Description | Example |
|---|---|---|
flowName | Name of the workflow to run | your_flow_name |
executionName | Name for this execution | your_exec_name |
input | JSON input for the execution | {"key":"value"} |
callbackFnFTaskToken | Token for TaskToken callback tasks after the execution ends | 12 |
Complete example
The following example creates a workflow, queries its details, and starts an execution:
<?php
namespace AlibabaCloud\SDK\Sample;
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
use AlibabaCloud\SDK\Fnf\V20190315\Fnf;
use AlibabaCloud\SDK\Fnf\V20190315\Models\CreateFlowRequest;
use AlibabaCloud\SDK\Fnf\V20190315\Models\DescribeFlowRequest;
use AlibabaCloud\SDK\Fnf\V20190315\Models\StartExecutionRequest;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use \Exception;
class Sample
{
public static function createClient()
{
$config = new Config([
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
]);
// Replace with your region endpoint. Example: cn-hangzhou.fnf.aliyuncs.com
$config->endpoint = "cn-hangzhou.fnf.aliyuncs.com";
return new Fnf($config);
}
public static function main($args)
{
$client = self::createClient();
$runtime = new RuntimeOptions([]);
try {
// Create a workflow
$createFlowRequest = new CreateFlowRequest([
"name" => "my_flow_name",
"definition" => '
Type: StateMachine
SpecVersion: v1
Name: my_flow_name
StartAt: my_state
States:
- Type: Pass
Name: my_state
End: true
',
"description" => "your test flow",
"type" => "FDL"
]);
$client->createFlowWithOptions($createFlowRequest, $runtime);
// Query workflow details
$describeFlowRequest = new DescribeFlowRequest([
"name" => "my_flow_name"
]);
$client->describeFlowWithOptions($describeFlowRequest, $runtime);
// Start an execution
$startExecutionRequest = new StartExecutionRequest([
"flowName" => "your_flow_name",
"executionName" => "your_exec_name",
"input" => '{"key":"value"}',
// Call back TaskToken-related tasks after the execution ends
"callbackFnFTaskToken" => "12"
]);
$client->startExecutionWithOptions($startExecutionRequest, $runtime);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
var_dump($error->message);
var_dump($error->data["Recommend"]);
Utils::assertAsString($error->message);
}
}
}
Sample::main(array_slice($argv, 1));More examples
For API-level SDK demos in PHP and other languages, see OpenAPI Explorer.