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.
- Create a Python_app folder and then create a
package
directory.Now create a Python file with the following content inside ourpackages/php
. For this example, the file name ishello.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
- Run the following command to deploy the action
ops ide deploy
- 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.
- To invoke the action run the following command:
ops action invoke php/hello --param name Marina --result
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.