Environment description

更新时间:
复制 MD 格式

Function Compute supports PHP as a managed runtime. This page covers the supported PHP version, built-in packages and extensions, and how to install custom extensions.

PHP runtime

VersionOperating systemArchitecture
PHP 7.2Linuxx86_64

Built-in packages

The following packages are pre-installed and available without any additional setup.

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

Built-in extensions

The following extensions are enabled by default in the PHP 7.2 runtime.

To list all installed extensions at runtime, include print_r(get_loaded_extensions()); in your function code.

bcmath, bz2, calendar, Core, ctype, curl, date, dom, exif, fileinfo, filter, ftp, gd, gettext, gmp, hash, iconv, imagick, json, libxml, mbstring, memcached, mysqli, mysqlnd, openSSL, pcntl, pcre, PDO, pdo_mysql, Phar, posix, protobuf, readline, redis, Reflection, session, shmop, SimpleXML, soap, sockets, SPL, standard, sysvmsg, sysvsem, sysvshm, tokenizer, Xdebug, xml, xmlreader, xmlrpc, xmlwriter, Zend OPcache, zip, zlib

Example: parse a JSON string

The following example uses the built-in json extension to parse a JSON string and print the result to stdout.

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

Install a non-built-in extension

If the extension you need is not in the built-in list, install it using the PHP runtime Docker image and deploy it alongside your function code. The following steps use MongoDB as an example.

Prerequisites

Before you begin, ensure that you have:

  • Docker installed on your machine. For more information, see the "Install Docker" section of Install Serverless Devs and Docker.

  • A function with the PHP runtime already created. For more information, see the "Create a function" section of Manage functions.

Install and deploy the extension

  1. In your project directory, start the PHP runtime container and mount the current directory to /code:

    On Windows, replace $(pwd) with an absolute path.
    sudo docker run -v $(pwd):/code -it --entrypoint="" registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-php7.2:latest bash
  2. Inside the container, install the MongoDB extension:

    pecl install mongodb
  3. Find the compiled .so file:

    find /usr -name "mongodb.so"
  4. Copy the extension file to the /code directory (which maps to your project directory):

    cp /usr/local/lib/php/extensions/no-debug-non-zts-20170718/mongodb.so /code
  5. Exit the container:

    exit
  6. Verify that mongodb.so exists in your project directory:

    ls

    Expected output:

    mongodb.so
  7. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.

  8. In the top navigation bar, select a region. On the Services page, click the target service.

  9. Click the target function. On the Code tab, create a folder named extension in the same directory as your handler file.

  10. Upload mongodb.so to the extension folder, then create a file named mongodb.ini in the same folder.

  11. Add the following content to mongodb.ini and save the file:

     extension=/code/extension/mongodb.so

Other custom extensions

Download the following pre-built extension packages based on your requirements:

What's next