This topic describes the PHP runtimes in which you can write function code in Function Compute.
PHP runtimes
The following table lists the PHP runtimes that are supported by Function Compute.
PHP version | OS | Architecture |
PHP 7.2 | Linux | x86_64 |
Built-in PHP packages
Function Compute provides the following built-in packages for PHP runtimes.
Package | Version | Reference |
oss | v2.4.3 | |
tablestore | v4.1.1 | |
mns | v1.3.5.5 | |
fc | v1.2.1 |
Built-in PHP extensions
Built-in extensions
If you want to view and print the information about installed PHP extensions, include print_r(get_loaded_extensions());
in the code of your function.
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 | None | None |
Built-in extension 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 a PHP runtime, perform the following steps. In the following example, MongoDB is installed.
Prerequisites
Docker is installed. For more information, see Step 2: Configure Serverless Devs.
A function whose runtime is PHP is created. For more information, see Create an event function.
Procedure
Run the following command in a project directory to start the image of the PHP runtime and mount the current directory to the /code directory of the container:
sudo docker run -v $(pwd):/code -it --entrypoint="" registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-php7.2:latest bash
NoteIf your computer runs a Windows operating system, you can change
$(pwd)
to a specific absolute directory.Install the MongoDB extension and copy mongodb.so to the /code directory.
Run the following command to install the MongoDB extension in the container:
pecl install mongodb
Run the following command to find the ZIP extension:
find /usr -name "mongodb.so"
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
Run the following command to exit the container:
exit
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
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page of the function, click the Code tab. Then, create the extension folder in the directory in which the handler files are stored.
NoteThe extension folder that you create must be at the same level as bootstrap.
Add the mongodb.so file to the extension folder, and create the mongodb.ini file in the extension folder.
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:
Additional information
For more information about how to create a custom extension based on a built-in extension, see How do I add or remove built-in extensions to or from the PHP runtime?
If you use a custom runtime and want to install MongoDB extensions, see How to install a custom extension in a custom runtime.