This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

PHP

Creating and invoking PHP actions

The process of creating PHP actions is similar to that of other actions. The following sections guide you through creating and invoking a single PHP action, and demonstrate how to bundle multiple PHP files and third party dependencies.

  1. Create a Python_app folder and then create a package directory.Now create a Python file with the following content inside our packages/php. For this example, the file name is hello.php.
<?php
//--web true
//--kind php:default
function main(array $args) : array
{
  $name = $args["name"] ?? "stranger";
  $greeting = "Hello $name!";
  echo $greeting;
  return ["greeting" => $greeting];
}
?>

The PHP file might contain additional functions. However, by convention, a function called main must exist to provide the entry point for the action.

Your directory structure should looks like this:

PHP_app
└── packages
    └── php
        └── hello.php
  1. Run the following command to deploy the action
ops ide deploy
  1. Ops will create the action automatically. For this example, the action is called php/hello.
  /home/openserverless.ops/tmp/deploy.pid
  PID 220917
  > Scan:
  >> Action: packages/php/hello.php
  > Deploying:
  >> Package: php
  $ $OPS package update php 
  ok: updated package php
  >>> Action: packages/php/hello.php
  $ $OPS action update php/hello packages/php/hello.php 
  ok: updated action php/hello
  build process exited with code 0
  UPLOAD ASSETS FROM web
  ==================| UPLOAD RESULTS |==================
  | FILES      : 0
  | COMPLETED  : 0
  | ERRORS     : 0
  | SKIPPED    : 0
  | EXEC. TIME : 2.61 ms
  ======================================================
  URL: http://openserverless.localhost:80

Note: To use a specific version of Python runtime, change the kind property --kind php:8.1, or --kind php:8.3 in the hello.py file.

  1. To invoke the action run the following command:
ops action invoke php/hello --param name Marina --result

1 - php:v8.0

php:v8.0 (default: false)

LibraryVersionLink
guzzlehttp/guzzle7.2.0packagist
ramsey/uuid4.1.1packagist
aws/aws-sdk-php3.209.31 || 3.210.0packagist
bcmathLatest*PHP.net
gdLatest*PHP.net
intlLatest*PHP.net
mysqliLatest*PHP.net
mongodbLatest*PHP.net
opcacheLatest*PHP.net
pgsqlLatest*PHP.net
pdo_mysqlLatest*PHP.net
pdo_pgsqlLatest*PHP.net
redisLatest*PHP.net
soapLatest*PHP.net
zipLatest*PHP.net

*Latest version available for current runtime version

2 - php:v8.1

php:v8.1 (default: false)

LibraryVersionLink
guzzlehttp/guzzle7.4.5packagist
ramsey/uuid4.4.0packagist
openai-php/clientv0.8.5packagist
aws/aws-sdk-php3.306.7packagist
bcmathLatest*PHP.net
gdLatest*PHP.net
intlLatest*PHP.net
mysqliLatest*PHP.net
mongodbLatest*PHP.net
opcacheLatest*PHP.net
pgsqlLatest*PHP.net
pdo_mysqlLatest*PHP.net
pdo_pgsqlLatest*PHP.net
redisLatest*PHP.net
soapLatest*PHP.net
zipLatest*PHP.net

*Latest version available for current runtime version

3 - php:v8.2

php:v8.2 (default: false)

LibraryVersionLink
guzzlehttp/guzzle7.7.0packagist
ramsey/uuid4.7.4packagist
openai-php/clientv0.8.5packagist
aws/aws-sdk-php3.306.7packagist
bcmathLatest*PHP.net
gdLatest*PHP.net
intlLatest*PHP.net
mysqliLatest*PHP.net
mongodbLatest*PHP.net
opcacheLatest*PHP.net
pgsqlLatest*PHP.net
pdo_mysqlLatest*PHP.net
pdo_pgsqlLatest*PHP.net
redisLatest*PHP.net
soapLatest*PHP.net
zipLatest*PHP.net

*Latest version available for current runtime version

4 - php:v8.3

php:v8.3 (default: true)

LibraryVersionLink
guzzlehttp/guzzle7.8.1packagist
ramsey/uuid4.7.5packagist
openai-php/clientv0.8.5packagist
aws/aws-sdk-php3.306.7packagist
bcmathLatest*PHP.net
gdLatest*PHP.net
intlLatest*PHP.net
mysqliLatest*PHP.net
mongodbLatest*PHP.net
opcacheLatest*PHP.net
pgsqlLatest*PHP.net
pdo_mysqlLatest*PHP.net
pdo_pgsqlLatest*PHP.net
redisLatest*PHP.net
soapLatest*PHP.net
zipLatest*PHP.net

*Latest version available for current runtime version