aliyunfc / fc-php-sdk
阿里云 FunctionCompute SDK for PHP
1.2.1
2020-09-22 09:38 UTC
Requires
- php: >=5.5
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- phpunit/phpunit: ^4.8.35 || ^5.7 || ^6.4 || ^7.0
- satooshi/php-coveralls: ~1.0
- dev-master
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-fix-update-function
- dev-fix-psr4
- dev-fix-updateFunc
- dev-release
- dev-provision
- dev-fix-ci
- dev-add-reserved-capacity-list-interface
- dev-support-tag
- dev-fix-readme
- dev-add-description-to-trigger
- dev-fix-bug
- dev-change-readme
- dev-new_delimiter
- dev-versioning
- dev-add-initializer-interface-for-php7-sdk
- dev-rename_php72
This package is auto-updated.
Last update: 2024-09-05 09:45:20 UTC
README
概述
本版本的 SDK 依赖于第三方 HTTP 库 guzzlehttp/guzzle.
运行环境
- PHP 5.6+
- cURL 扩展
安装
推荐通过 Composer 安装 fc-php-sdk。
- 安装 Composer: https://getcomposer.org.cn/doc/00-intro.md
- 安装 fc-php-sdk
$ composer require aliyunfc/fc-php-sdk
您也可以在 composer.json 文件中声明对阿里云 FC SDK for PHP 的依赖。
"require": { "aliyunfc/fc-php-sdk": "~1.2" }
然后运行 composer install --no-dev
安装依赖。Composer 依赖管理器安装后,在您的 PHP 代码中导入依赖
require_once __DIR__ . '/vendor/autoload.php';
入门
<?php require_once __DIR__ . '/vendor/autoload.php'; use AliyunFC\Client; // To know the endpoint and access key id/secret info, please refer to: // https://help.aliyun.com/document_detail/52984.html $fcClient = new Client([ "endpoint" => '<Your Endpoint>', "accessKeyID" =>'<Your AccessKeyID>', "accessKeySecret" =>'<Your AccessKeySecret>' ]); // Create service. $fcClient->createService('service_name'); /* Create function. the current directory has a main.zip file (main.php which has a function of my_handler) set environment variables {'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 function synchronously. $fcClient->invokeFunction('service_name', 'function_name'); /* Create function with initializer. the current directory has a main.zip file (main.php which hava functions of my_handler and my_initializer) set environment variables {'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 function synchronously. $fcClient->invokeFunction('service_name_with_initializer', 'function_name'); //Create trigger, for example: 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 a function with a input parameter. $fcClient->invokeFunction('service_name', 'function_name', $payload='hello_world'); // Invoke function asynchronously. $fcClient->invokeFunction('service_name', 'function_name', 'hello world', ['x-fc-invocation-type' => 'Async']); // List services. $fcClient->listServices(); //List functions with prefix and limit. $fcClient->listFunctions('service_name', ['prefix' => 'hello', "limit" => 2]); //List triggers $fcClient->listTriggers('service_name', 'function_name'); //Delete trigger $fcClient->deleteTrigger('service_name', 'function_name', 'trigger_name'); //Delete function $fcClient->deleteFunction('service_name', 'function_name'); //Delete service. $fcClient->deleteService('service_name');
测试
要运行测试,请设置访问密钥 ID/密钥、端点作为环境变量。以 Linux 系统为例
$ export ENDPOINT=<endpoint> $ export ACCESS_KEY_ID=<AccessKeyId> $ export ACCESS_KEY_SECRET=<AccessKeySecret> $ export ACCOUNT_ID=<AccountId> ...
有关详细信息,请参阅 client_test.php
以以下方式运行测试
$ phpunit