通过函数计算的Custom Runtime,您可以在函数计算平台使用PHP来编写函数。本文介绍快速部署和调用PHP函数的详细步骤。您也可以修改PHP示例中的代码,实现您的需求。
前提条件
您已安装8.6.0或以上版本的Node.js。更多信息,请参见
官方下载地址。
步骤一:准备环境
安装Funcraft,最简单的方式就是直接下载可执行的二进制文件。
- 安装Funcraft到本机。更多信息,请参见安装Funcraft。
- 执行
fun --version
检查安装是否成功。 - 执行
fun config
配置Funcraft。然后按照提示依次配置Account ID、Access Key ID、Access Key Secret、Default region name。fun config Aliyun Account ID 1234xxx Aliyun Access Key ID xxxx Aliyun Access Key Secret xxxx Default region name cn-xxxx The timeout in seconds for each SDK client invoking 300 The maximum number of retries for each SDK client 5 Allow to anonynously report usage statistics to improve the tool over time? (Y/n)
步骤二:部署和调用函数
- 执行以下命令克隆示例工程到本地。
git clone https://github.com/awesome-fc/fc-custom-demo
说明 如果您没有安装Git,可以直接在浏览器地址栏输入
https://github.com/awesome-fc/fc-custom-demo,然后单击下载按钮直接下载示例代码。 - 可选:修改php74-swoole-demo目录中的示例代码,实现您的业务逻辑。
- 执行以下命令进入克隆的示例项目中。
- 执行以下命令进入fc-custom-demo文件。
cd fc-custom-demo
- 执行以下命令进入php74-swoole-demo文件。
cd php74-swoole-demo
- 执行以下命令进入fc-custom-demo文件。
- 执行以下命令将项目部署至云上。
fun deploy -y
返回结果如下。Waiting for service php74-swoole-demo to be deployed... Waiting for function fc-php74-swoole to be deployed... Waiting for packaging function fc-php74-swoole code... The function fc-php74-swoole has been packaged. A total of 2 files were compressed and the final size was 800 B function fc-php74-swoole deploy success service php74-swoole-demo deploy success
- 执行以下命令调用部署的函数。
fun invoke -e "Hello World"
说明 如果您使用的是Windows操作系统,您需要将
bootstrap文件格式修改为UNIX格式。返回结果如下。========= FC invoke Logs begin ========= FC Invoke Start RequestId: 2d7d089d-7a41-4d81-a932-01d452ead907 string(11) "Hello World" FC Invoke End RequestId: 2d7d089d-7a41-4d81-a932-01d452ead907 Duration: 1.01 ms, Billed Duration: 100 ms, Memory Size: 512 MB, Max Memory Used: 32.68 MB ========= FC invoke Logs end ========= FC Invoke Result: Hello World