PHP
    • PDF

    PHP

    • PDF

    Article Summary

    Available in Classic and VPC

    This document describes how to create and use PHP actions in a variety of ways, and includes application examples.

    Create actions

    You can use Composer, which is PHP's package manager like JavaScript's NPM, to manage dependencies, or create an action by packaging it 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 the 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.

    compute-15-2-501.png

    Create actions by packaging with dependency files

    When writing code, there are cases where dependency files need to be packaged along with one action file. In such cases, related files can be compressed in a file, packaged, and an action can be created using the compressed file.
    The main function that works as the entry point for executing actions, like the main(args) function used as the default value, must be defined in the index.php file.
    For example, if you'd like to create an action by packaging together with the helper.php 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 actions by packaging Composer dependencies

    When writing PHP action code, there are cases where multiple libraries are used using Composer, which is a dependency management tool. In such cases, you can create and run actions by packaging dependency modules included in vendor, in the same way as the default packaging. In addition, the main function must be defined in the index.php file.
    The following is an example of creating an action that creates fake data similar to actual addresses by adding the fzaninotto/faker library.

    1. Install the library through the composer command.

      $ composer require fzaninotto/faker
      
    2. Define the main function that returns a fake address under the file name, index.php.

      <?php
      require __DIR__ . '/vendor/autoload.php';
      
      function main(array $args) : array
      {
          $faker = Faker\Factory::create();
          return ["address" => $faker->address];
      } 
      ?>
      
    3. Compress the vendor folder and the index.php file together.

      $ zip -r composerPHP.zip vendor index.php
      
    4. Upload the compressed file, and create an action.


    Was this article helpful?

    What's Next
    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.