You can use the custom runtime of Function Compute to write functions in PHP. This topic describes how to deploy and invoke a PHP function. You can modify the sample PHP code as required.

Background information

Before you begin, make sure that the following operations are complete:
  • Install Node.js 8.6.0 or later. For more information, visit Downloads.
  • Install Docker. For more information, visit Docker Hub.

Step 1: Prepare the environment

The easiest way to install Funcraft is to download the executable binary files.

  1. Install Funcraft on an on-premises machine. For more information, see Install Funcraft.
  2. Run the fun --version command to check whether the installation is successful.
  3. Run the fun config command to configure Funcraft. Set the Account ID, Access Key ID, Access Key Secret, and Default region name parameters as prompted.
    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)

Step 2: Deploy and invoke a function

  1. Run the following command to clone the sample project to the on-premises machine:
    git clone https://github.com/awesome-fc/fc-custom-demo          
    Note If you do not install Git, you can download the sample project from https://github.com/awesome-fc/fc-custom-demo.
  2. Optional:Modify the sample code in the php74-swoole-demo directory to implement your business logic.
  3. Run the following commands to go to the cloned sample project:
    1. Run the following command to go to the fc-custom-demo directory:
      cd fc-custom-demo
    2. Run the following command to go to the php74-swoole-demo directory:
      cd php74-swoole-demo
  4. Run the following command to deploy the project to Function Compute:
    fun deploy -y
    The following result is returned:
    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
  5. Run the following command to invoke the deployed function:
    fun invoke -e "Hello World"
    The following result is returned:
    ========= 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