Available in Classic and VPC
This section describes how to create and use PHP actions in a variety of ways, and includes application examples.
Create action
Like NPM for JavaScript, you can manage dependencies using PHP's package manager Composer and create actions by packaging with dependency libraries. A code written in PHP may contain multiple functions, but the main function must be declared as the starting point of a program. With this taken into account, the following is a simple example code hello.php in PHP, printing "Hello World" along with a name and location:
<?php
function main(array $args) : array
{
$name = $args["name"] ?? "World";
$place = $args["place"] ?? "Naver";
$greeting = "Hello $name in $place!";
echo $greeting;
return ["payload" => $greeting];
}
?>
The following is the process of an action called "hello" in the console, using the code written above.

Create action by packaging with dependency files
When writing codes, you may need to package dependency files along with one action file. In this case, you can compress related files into one file for packaging and create an action using the compressed file.
The main function that serves as the entry point when running the action, like the default main(args) function, must be defined in the index.php file.
For example, to create an action by packaging together with the helper.py file where functions used by the main action are written, use the following command to compress the action-related files to helloPHP.zip.
zip -r helloPHP.zip index.php hello.php
You can then use the compressed file created to create the action.
Create action with Composer dependencies
When writing PHP action code, you may use multiple libraries with the Composer dependency management tool. In this case, you can create and run actions by packaging with dependency modules included in vendor, the same way as basic packaging. The main function must also be defined in the index.php file.
The following is an example of creating an action that generates fake data similar to real addresses by adding the fzaninotto/faker library:
-
Install the library using the composer command.
$ composer require fzaninotto/faker -
Define a main function that returns fake addresses in a file named index.php.
<?php require __DIR__ . '/vendor/autoload.php'; function main(array $args) : array { $faker = Faker\Factory::create(); return ["address" => $faker->address]; } ?> -
Compress the vendor folder and index.php file together.
$ zip -r composerPHP.zip vendor index.php -
Create an action by uploading the compressed file.