PHP Method Reference

List of Functions

CategoryMethodDescription

Overview

In addition to RPC, Homegear can be controlled through it's built-in PHP script engine. Through this engine, scripts are executed much faster as there is no TCP socket connection to set up. Depending on the system that can safe up to a few hundred milliseconds. The PHP scripts can be executed the following ways:

  • Webserver: By placing the files in the webserver root directory (/var/www/homegear/www/rpc by default, customizable in "/etc/homegear/rpcservers.conf"). The script can be executed by opening one of the RPC IP addresses in a web browser (by default HOMEGEAR_IP:2001, HOMEGEAR_IP:2002 [SSL, no auth], HOMEGEAR_IP:2003 [SSL, auth]).
  • CLI: By placing the files in the script directory (/var/www/homegear/scripts by default) and executing:
    runscript MyScript.php FirstParam SecondParam
    or
    rs MyScript.php FirstParam SecondParam
  • Terminal or shell script: By also placing the files in the script directory (/var/www/homegear/scripts by default) and executing:
    homegear -e runscript MyScript.php FirstParam SecondParam
    or
    homegear -e rs MyScript.php FirstParam SecondParam
  • Inline script on CLI, terminal or shell: By executing ($hg is already defined and you don't need to prepend the namespace):
    homegear -e runcommand "PHP"
    or
    homegear -e rc "PHP"
    For example with double quotes (weak quoting, you need to escape double quotes, "$" and "\"):
    homegear -e rc "\$hg->setValue(12, 2, \"STATE\", true);"
    or with single qotes (strong quoting, you only need to escape single quotes):
    homegear -e rc '$hg->setValue(12, 2, "STATE", true); print '\''Hello World!'\'';'
    or in CLI (no need to escape, quotes are optional):
    rc $hg->setValue(12, 2, "STATE", true);
    if the command starts with "$" you can even omit the "rc":
    $hg->setValue(12, 2, "STATE", true);
  • RPC: By calling the RPC method "runScript":
    curl -X POST -d '{
        "jsonrpc": "2.0",
        "method": "runScript",
        "id": 123,
        "params": ["Test.php", "param1 param2"]
    }' http://HOMEGEAR_IP:2001 --header "Content-Type: application/json"

Starting with Homegear 0.6 PHP 7 is used as script engine. All RPC methods now are accessible through the "Homegear" class in the namespace "Homegear". Either statically:

<?php
Homegear\Homegear::setValue(12, 1, "STATE", true);
?>
or non-statically:
<?php
$hg = new Homegear\Homegear();
$hg->setValue(12, 1, "STATE", true);
?>

In addition to the class Homegear, the following classes are available:

Class Description
HomegearGpio Class with functions to get and set GPIO states or wait for GPIO state changes.
HomegearSerial Class with functions for reading and writing to serial devices (UART, RS232, ...).
HomegearI2c Class with functions for communicating with I²C devices.
HomegearException This class is thrown on errors. Inherited from "Exception".