Page 1 of 1

Trying to pass value into domoticz API using PHP-CURL

Posted: Thursday 18 July 2019 18:20
by chamsters
Hi folks,

Been trying to get a PHP script of mine to load the domoticz API page using CURL to store some data. Going absolutely crazy after reading 4 different ways to do it, implementing each, but not a single one works.

One version of code. This computer resides on the same network as the domotics server. I have authentication disabled for all 192.168.1.* ips.

Code: Select all

$endpoint = 'http://192.168.1.5:3003/json.htm';
$params = array('type' => 'command', 'param' => 'udevice', 'idx' => '30', 'svalue' => $currentwatts);
$url = $endpoint . '?' . http_build_query($params);
curl_setopt($ch, URLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
This doesn't work.

If i issue echo commands to see what URL is being generated, it's 100% right. If I open the URL via a browser, it works. If I try using wget via Shell for the same it doesn't, presumably because I need escape characters / URL encoding.

Any ideas?

Thanks!

Re: Trying to pass value into domoticz API using PHP-CURL

Posted: Friday 19 July 2019 6:39
by Egregius
This is de update device function of pass2php:

Code: Select all

function ud($name,$nvalue,$svalue,$check=false)
{
    global $user,$d,$domoticzurl;
    if ($d[$name]['i']>0) {
        if ($check==true) {
            if ($d['name']['s']!=$svalue) {
                return file_get_contents($domoticzurl.'/json.htm?type=command&param=udevice&idx='.$d[$name]['i'].'&nvalue='.$nvalue.'&svalue='.$svalue);
            }
        } else {
            return file_get_contents($domoticzurl.'/json.htm?type=command&param=udevice&idx='.$d[$name]['i'].'&nvalue='.$nvalue.'&svalue='.$svalue);
        }
    } else {
        store($name, $svalue, basename(__FILE__).':'.__LINE__);
    }
    lg(' (udevice) | '.$user.' => '.$name.' => '.$nvalue.','.$svalue);
}

Re: Trying to pass value into domoticz API using PHP-CURL

Posted: Friday 19 July 2019 11:29
by chamsters
Egregius wrote: Friday 19 July 2019 6:39 This is de update device function of pass2php:

Code: Select all

function ud($name,$nvalue,$svalue,$check=false)
{
    global $user,$d,$domoticzurl;
    if ($d[$name]['i']>0) {
        if ($check==true) {
            if ($d['name']['s']!=$svalue) {
                return file_get_contents($domoticzurl.'/json.htm?type=command&param=udevice&idx='.$d[$name]['i'].'&nvalue='.$nvalue.'&svalue='.$svalue);
            }
        } else {
            return file_get_contents($domoticzurl.'/json.htm?type=command&param=udevice&idx='.$d[$name]['i'].'&nvalue='.$nvalue.'&svalue='.$svalue);
        }
    } else {
        store($name, $svalue, basename(__FILE__).':'.__LINE__);
    }
    lg(' (udevice) | '.$user.' => '.$name.' => '.$nvalue.','.$svalue);
}
I tried with the file_get_contents method too.

For e.g.:

Code: Select all

$domoticzoutput = file_get_contents('http://192.168.1.5:3003/json.htm?type=commant&param=udevice&idx=30&svalue=30');
basically hardcoding the entire string before even thinking of adding variables, I get no response. The value in domiticzoutput is "." which makes no sense. When I look at my httpd error log file I see

PHP Warning: file_get_contents(http://192.168.1.5:3003/json.htm?type=c ... ;svalue=30): failed to open stream: Permission denied in /var/www/html/script.php on line 131

Any ideas?

Re: Trying to pass value into domoticz API using PHP-CURL

Posted: Friday 19 July 2019 11:40
by chamsters
Looks like I have senlinux which was limiting me.

Followed guidance here: https://stackoverflow.com/questions/488 ... ion-denied


Which solved it :)