-
Hahn Axel (hahn) authoredHahn Axel (hahn) authored
40_Embed.md 1.27 KiB
Introduction
To embed the cli tool into your web application you need to
- perform the execution of the binary with referencing the ini
<PATH>amcli --ini=<INI-FILE>
- catch exitcode and output of STDOUT
- check returncode:
- if it is
0
then the excution was successful - we have a result. You can send the JSON after setting the content type as http response header - if it is
> 0
the response is no JSON syntax. You should send the output that the error is seen in the appm nitor server.
- if it is
PHP
This code snippet is for demonstration purposes only. A PHP web app should use the PHP client - see https://os-docs.iml.unibe.ch/appmonitor/PHP_client/index.html
<?php
$AMCLI = __DIR__ . '/../built_packages/amcli';
$INI = __DIR__ . '/../src/simple.ini';
$sCommand = "$AMCLI --ini='$INI'";
$aOut = [];
exec($sCommand, $aOut, $iRc);
if ($iRc == 0) {
header('Content-type: application/json');
} else {
header("HTTP/1.1 503 Service unavailable");
echo "<h1>503 Service unavailable</h1>\n";
}
echo implode("\n", $aOut);
To test it:
(1)
Start PHP webserver
$ cd examples
$ php -S localhost:9000
[Fri Mar 7 09:01:52 2025] PHP 8.4.4 Development Server (http://localhost:9000) started
(2)
In a webbrowser open http://localhost:9000/embed_example.php