This topic describes how to install CloudFlow SDK for PHP and provides examples on how to use the SDK to call CloudFlow API operations. In the following examples, CloudFlow SDK for PHP
is used to call CloudFlow API operations to create a workflow, query the information about a workflow, and asynchronously start the execution of a workflow. This topic also provides the complete SDK integration steps.
Prerequisites
An AccessKey pair is configured. Make sure that an AccessKey pair is created. For more information, see Create an AccessKey pair. To prevent credential leaks, you can write the credentials into environment variables. For more information, see Best practices for using an access credential to call API operations.
Environment requirements
PHP 5.6 or later is installed.
Composer is installed globally. For more information, see Globally.
The version of PHP used to install Alibaba Cloud SDK by using Composer must be earlier than or equal to the version of PHP used to run Alibaba Cloud SDK. For example, the vendor folder generated after Alibaba Cloud SDK is installed in PHP 7.2 can be used only in PHP 7.2 or later. If the vendor folder is copied to PHP 5.6, the dependency is incompatible with PHP 5.6. If you fail to install Composer due to network issues, you can run the following command to use the full image of Composer provided by Alibaba Cloud:
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
Step 1: Import CloudFlow SDK for PHP to your project
Alibaba Cloud SDK allows you to initiate generic and specialized calls. For more information, see Generic calls and specialized calls. The SDK that you need to import varies based on the call type.
Specialized calls
You can visit OpenAPI Explorer, find the service that you want to use, and view the supported languages and installation methods. Then, import the SDK of the service to your project. In this example, CloudFlow SDK for PHP is imported. Perform the following steps to import the SDK:
Go to the CloudFlow SDK page in OpenAPI Explorer.
In the All languages field, select PHP.
In the Installation Method section, select an installation method and then copy the installation code to your project.
Add the dependency to your project.
Run the following command to install CloudFlow SDK for PHP:
composer require alibabacloud/fnf-20190315 1.1.3
Generic calls
You do not need to install a service-specific SDK to initiate generic calls. You need to only import the com.aliyun.tea-openapi
core package. For information about the latest version of the core package, see tea-openapi. Run the following command to install the core package:
composer require alibabacloud/darabonba-openapi
Step 2: Initialize a client
Specify the endpoint of CloudFlow based on the region in which you use CloudFlow. For information about the endpoint of CloudFlow in each region, see Regions.
The following section provides sample code on how to initialize a client for specialized calls. For information about how to initiate generic calls, see Generic calls and specialized calls.
Use an AccessKey pair
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 Resource Access Management (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 in your account may be compromised.
In this example, the AccessKey pair is obtained from the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.
For more information about how to configure authentication information, see Manage access credentials.
The method for configuring environment variables varies based on the operating system. For more information, see Configure environment variables in Linux, macOS, and Windows.
<?php
namespace AlibabaCloud\SDK\Sample;
use AlibabaCloud\SDK\Fnf\V20190315\Fnf;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Fnf\V20190315\Models\DescribeFlowRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class Sample {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @return Fnf Client
*/
public static function createClient() {
$config = new Config([
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
]);
$config->endpoint = "cn-hangzhou.fnf.aliyuncs.com";
return new Fnf($config);
}
}
Step 3: Use the initialized client to call CloudFlow API operations
After you initialize a client, you can use the client to call CloudFlow API operations.
API operation: CreateFlow
Call the CreateFlow operation to create a workflow. When you call the CreateFlow operation, create a request and configure parameters and runtime settings based on your business requirements. You can also customize runtime settings to meet specific requirements.
// Create a request.
$createFlowRequest = new CreateFlowRequest([
// Specify a name for the workflow that you want to create.
"name" => "my_flow_name",
// Define the workflow against the Flow Definition Language (FDL) syntax standard. To ensure forward compatibility, the system supports the old and new versions of workflow definition specifications.
"definition" => "The old version:
\"
type: flow
version: v1
name: my_flow_name
steps:
- type: pass
name: mypass
\"
The new version:
\"
Type: StateMachine
SpecVersion: v1
Name: my_flow_name
StartAt: my_state
States:
- Type: Pass
Name: my_state
End: true
\"",
// Specify a description for the workflow.
"description" => "your test flow",
// Configure the type of the workflow.
"type" => "FDL"
]);
// Configure runtime settings.
$runtime = new RuntimeOptions([]);
The following sample code provides a complete example on how to create a workflow by using the AccessKey pair to call the CreateFlow operation:
<?php
namespace AlibabaCloud\SDK\Sample;
use AlibabaCloud\SDK\Fnf\V20190315\Fnf;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Fnf\V20190315\Models\CreateFlowRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class Sample {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @return Fnf Client
*/
public static function createClient(){
$config = new Config([
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
]);
$config->endpoint = "cn-hangzhou.fnf.aliyuncs.com";
return new Fnf($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
$client = self::createClient();
// Create a request.
$createFlowRequest = new CreateFlowRequest([
// Specify a name for the workflow that you want to create.
"name" => "my_flow_name",
// Define the workflow against the FDL syntax standard. To ensure forward compatibility, the system supports the old and new versions of workflow definition specifications.
"definition" => "The old version:
\"
type: flow
version: v1
name: my_flow_name
steps:
- type: pass
name: mypass
\"
The new version:
\"
Type: StateMachine
SpecVersion: v1
Name: my_flow_name
StartAt: my_state
States:
- Type: Pass
Name: my_state
End: true
\"",
// Specify a description for the workflow.
"description" => "your test flow",
// Configure the type of the workflow.
"type" => "FDL"
]);
// Configure runtime settings.
$runtime = new RuntimeOptions([]);
try {
// If you copy and run the sample code, write your own code to display the response of the API operation.
$client->createFlowWithOptions($createFlowRequest, $runtime);
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
// Display error messages.
var_dump($error->message);
// Provide the URL for troubleshooting.
var_dump($error->data["Recommend"]);
Utils::assertAsString($error->message);
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
Sample::main(array_slice($argv, 1));
API operation: DescribeFlow
Call the DescribeFlow operation to query the information of a workflow. When you call the DescribeFlow operation, create a request and configure parameters and runtime settings based on your business requirements. You can also customize runtime settings to meet specific requirements.
// Create a request and specify the request parameters.
$describeFlowRequest = new DescribeFlowRequest([
// Specify the name of the workflow that you want to query.
"name" => "my_flow_name"
]);
// Create a runtime configuration object.
$runtime = new RuntimeOptions([]);
The following sample code provides a complete example on how to query the information of a workflow by using the AccessKey pair to call the DescribeFlow operation:
<?php
namespace AlibabaCloud\SDK\Sample;
use AlibabaCloud\SDK\Fnf\V20190315\Fnf;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Fnf\V20190315\Models\DescribeFlowRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class Sample {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @return Fnf Client
*/
public static function createClient(){
$config = new Config([
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
]);
$config->endpoint = "cn-hangzhou.fnf.aliyuncs.com";
return new Fnf($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
$client = self::createClient();
// Create a request and specify the request parameters.
$describeFlowRequest = new DescribeFlowRequest([
// Specify the name of the workflow that you want to query.
"name" => "my_flow_name"
]);
// Create a runtime configuration object.
$runtime = new RuntimeOptions([]);
try {
// If you copy and run the sample code, write your own code to display the response of the API operation.
$client->describeFlowWithOptions($describeFlowRequest, $runtime);
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
// Display error messages.
var_dump($error->message);
// Provide the URL for troubleshooting.
var_dump($error->data["Recommend"]);
Utils::assertAsString($error->message);
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
Sample::main(array_slice($argv, 1));
API operation: StartExecution
The StartExecution operation is an asynchronous operation that starts the execution of a workflow. When you call the StartExecution operation, create a request and configure parameters and runtime settings based on your business requirements. You can also customize runtime settings to meet specific requirements.
// Create a request.
$startExecutionRequest = new StartExecutionRequest([
// Specify the name of the workflow that you want to execute.
"flowName" => "your_flow_name",
// Specify a name for the execution.
"executionName" => "your_exec_name",
// Specify the input of the execution.
"input" => "{\"key\":\"value\"}",
// Call back TaskToken-related tasks after the workflow execution ends. Example value: 12.
"callbackFnFTaskToken" => "12"
]);
// Configure runtime settings.
$runtime = new RuntimeOptions([]);
The following sample code provides a complete example on how to start the execution of a workflow by using the AccessKey pair to call the StartExecution operation:
<?php
namespace AlibabaCloud\SDK\Sample;
use AlibabaCloud\SDK\Fnf\V20190315\Fnf;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Fnf\V20190315\Models\StartExecutionRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class Sample {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @return Fnf Client
*/
public static function createClient(){
$config = new Config([
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
]);
Specify the endpoint. For information about endpoints, visit https://api.aliyun.com/product/fnf.
$config->endpoint = "cn-hangzhou.fnf.aliyuncs.com";
return new Fnf($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
$client = self::createClient();
// Create a request.
$startExecutionRequest = new StartExecutionRequest([
// Specify the name of the workflow that you want to execute.
"flowName" => "your_flow_name",
// Specify a name for the execution.
"executionName" => "your_exec_name",
// Specify the input of the execution.
"input" => "{\"key\":\"value\"}",
// Call back TaskToken-related tasks after the workflow execution ends. Example value: 12.
"callbackFnFTaskToken" => "12"
]);
// Configure runtime settings.
$runtime = new RuntimeOptions([]);
try {
// If you copy and run the sample code, write your own code to display the response of the API operation.
$client->startExecutionWithOptions($startExecutionRequest, $runtime);
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
// Display error messages.
var_dump($error->message);
// Provide the URL for troubleshooting.
var_dump($error->data["Recommend"]);
Utils::assertAsString($error->message);
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
Sample::main(array_slice($argv, 1));
SDK call example
You can use the API-level SDK demos in different programming languages for debugging. To view the sample code, visit OpenAPI Explorer.