PHP/Curl triggered JSON call not working. - SOLVED

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
Scones
Posts: 47
Joined: Wednesday 10 December 2014 16:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

PHP/Curl triggered JSON call not working. - SOLVED

Post by Scones »

I'm trying to integrate my Domoticz system with services on my VPS box. If I issue the command

Code: Select all

wget -O - --no-check-certificate --header "Authorization: Basic $authStr" 'https://${Server}/json.htm?type=command&param=switchlight&idx=30&switchcmd=Off'
I get the following:

Code: Select all

HTTP request sent, awaiting response... 200 OK
Length: 51 [application/json]
Saving to: ‘STDOUT’
{
   "status" : "OK",
   "title" : "SwitchLight"
}
However, the following PHP code on the same VPS server

Code: Select all

function get_json($url) {
  global $JSON_User,$JSON_Password,$JSON_Base;
  $service_url = "${JSON_Base}?$url";
  $curl = curl_init($service_url);

  curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($curl, CURLOPT_USERPWD, "$JSON_User:$JSON_Password");
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_FAILONERROR,true);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  $curl_response = curl_exec($curl);
  if (!$curl_response) {
    $response['status']='Err';
    $response['message']="JSON get failed: ".curl_error($curl);
  } else {
    $response = json_decode($curl_response);
  }
  curl_close($curl);
  return $response;

} # get_json
returns the following:

Code: Select all

{"message":"Error sending switch command, check device\/hardware !","status":"ERROR","title":"SwitchLight"}
The $service_url generated looks *exactly* like the one used in wget. I've been looking at this for ages. Can anyone see what I've done wrong?

In both cases I get the following showing in /var/log/domoticz.log

Code: Select all

User: JSONRemote initiated a switch command (30/DM1_Gary/off)
Last edited by Scones on Wednesday 11 January 2017 18:27, edited 1 time in total.
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: PHP/Curl triggered JSON call not working.

Post by Egregius »

Strip the authentication stuff and disable authentication for 127.0.0.1 under Setup -> settings -> local networks ;)
Or have a look at my pass2php script how I handle the json api...
Scones
Posts: 47
Joined: Wednesday 10 December 2014 16:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: PHP/Curl triggered JSON call not working. - SOLVED

Post by Scones »

Egregius wrote:Strip the authentication stuff and disable authentication for 127.0.0.1 under Setup -> settings -> local networks ;)
Or have a look at my pass2php script how I handle the json api...
My VPS is not on my LAN, it's a cloud based server. I need to keep the authentication.

Most of my code is in PHP which is what I'm used to.

Anyway, I have found out why it didn't work, and why I have wasted a whole day.

When running the command on the command line I correctly gave the 'switchcmd=Off'. Inside my PHP I was passing 'switchcmd=off'

A more helpful error message, or more checking from me would have made for a far less stressful day
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: PHP/Curl triggered JSON call not working. - SOLVED

Post by Egregius »

Case sensitive it is ;)

I do wonder what you want to sent between vps and domoticz tough...
Scones
Posts: 47
Joined: Wednesday 10 December 2014 16:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: PHP/Curl triggered JSON call not working. - SOLVED

Post by Scones »

Egregius wrote:Case sensitive it is ;)

I do wonder what you want to sent between vps and domoticz tough...
I'm playing with Amazon Alexa - mainly in an Echo Dot which I love, but also available on my mobile phone using the comms App Roger - and IFTTT.

I am already using the habridge to directly connect my Echo Dot to my Domoticz and that works great, within limitations. For example I can tell Alexa to turn on my heated blanket. I am having trouble getting it to work with scenes and more complex issues though, although that may be another example of case sensitive issues.

My Alexa account is already connected to IFTTT, and I can do "alexa trigger ........." to trigger IFTTT events. I also have IFTTT on my mobile with location services enabled. This means that when I pass into or out of an area I can trigger IFTTT events. However, IFTTT is purely one trigger one effect

One event that IFTTT does have is the Maker Channel, which allows the event to be a HTTP GET/POST to any URL. This means I can get my mobile to trigger IFTTT, which then triggers an event on my virtual server. As this virtual server is a linux box I now have all the facilities I need to build intelligent event engines. The use of JSON to interrogate and update Domoticz is a core requirement of this.

I could build all of this on the Domoticz box directly but that would involve storing user ID's and passwords on someone else's servers, and would increase the workload on the Dom box which is a Raspberry Pi. By doing it this way, I offload some of the workload, and store all of the user credentials on one of my own boxes.

An example use is that when I'm driving home, as soon as I get within 10 miles of home, it emails my wife to tell her I'm almost there. In future, it will at the same time change my central heating system from "absent" to "home", so that by the time I get home it should be nice and warm.

It also means that using Alexa and Roger I should be able to talk to my house from anywhere.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest