This topic describes the PHP runtime environment in which you can write function code in Function Compute.

PHP runtime

Function Compute supports the following PHP runtime environment.

PHP versionOperating systemArchitecture
PHP 7.2Linuxx86_64

Built-in PHP packages

Function Compute provides the following built-in packages for the PHP runtime environment.

PackageVersionDescription
ossv2.4.3Object Storage Service (OSS) SDK for PHP
tablestorev4.1.1TableStore SDK for PHP
mnsv1.3.5.5Message Service (MNS) SDK for PHP
fcv1.2.1Function Compute SDK for PHP

Built-in PHP extensions

Built-in extensions

Note If you want to view and print the information about installed PHP extensions, include print_r(get_loaded_extensions()); in the code of the specified function.
bcmathbz2calendarCore
ctypecurldatedom
exiffileinfofilterftp
gdgettextgmphash
iconvimagickjsonlibxml
mbstring memcached mysqli mysqlnd
opensslpcntlpcrePDO
pdo_mysqlPharposixprotobuf
readlineredisReflectionsession
shmopSimpleXMLsoapsockets
SPLstandardsysvmsgsysvsem
sysvshmtokenizerXdebugxml
xmlreaderxmlrpcxmlwriterZend OPcache
zipzlibNoneNone

Example

The following sample code uses json to parse a JSON string and return the parsed content to stdout.

<?php
function handler($event, $context) {
    var_dump(json_decode('{"a":123, "b":true, "c":"abcd", "d":{"a":123}}', true));
    return "bye";
}         

Install non-built-in PHP extensions

To install a non-built-in extension for the PHP runtime, perform the following steps. MongoDB is used in this example.

Prerequisites

  • Docker is installed. For more information, see Install Docker.
  • A function whose runtime environment is PHP is created. For more information, see Create a function.

Procedure

  1. Run the following command in a project directory to start the image of the PHP runtime environment and mount the current directory to the /code directory of a container:
    sudo docker run -v $(pwd):/code -it --entrypoint=""  registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-php7.2:latest  bash
    Note If your computer runs Windows, you can change $(pwd) to a specific absolute directory.
  2. Install the MongoDB extension and copy the mongodb.so file to the /code directory.
    1. Run the following command to install the MongoDB extension in the container:
      pecl install mongodb
    2. Run the following command to find the ZIP extension:
      find /usr -name "mongodb.so"
    3. Run the following command to copy the extension file to the /code directory:
      cp /usr/local/lib/php/extensions/no-debug-non-zts-20170718/mongodb.so /code
    4. Run the following command to exit the container:
      exit
    5. Run the following command to check whether the mongodb.so file exists. If the file exists, the .so file is stored on your computer.
      ls
      Expected output:
      mongodb.so
  3. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
  4. In the top navigation bar, select a region. On the Services page, click the desired service.
  5. On the Functions page of the specified service, click the specified function. On the Code tab, create the extension folder in the directory in which the handler files are stored.
    Note The extension folder that you create must be at the same level as the bootstrap file.
  6. Add the mongodb.so file to the extension folder, and create the mongodb.ini file in the extension folder.
  7. Edit the mongodb.ini file.
    Add the following content to the mongodb.ini file and save the file:
    extension=/code/extension/mongodb.so
If you want to install other custom extensions, click the following links to download the corresponding files based on your business requirements:

References