0

I'm trying to simply read the Philips Hue lights information from my home with the following code:

$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'a');

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://119.119.20.20:2827/api/Js82jH2lao-pAiws89S9A-k9hHsukw72/lights',
    CURLOPT_VERBOSE => true,
    CURLOPT_STDERR => $fp
));
$resp = curl_exec($ch);
curl_close($ch);
print_r($resp);

It returns nothing. Looking at errorlog.txt it says:

* About to connect() to 119.119.20.20 port 2827 (#0)
*   Trying 119.119.20.20... * Connection refused
* couldn't connect to host
* Closing connection #0

I'm able to read the data and change light settings through a site like hurl.it which tells me I've setup my router correctly. allow_url_fopen on my server is on. I'm using curl because I want to do a PUT request as well. I don't want to use a library for simply turning on and off an light.

How can I make this work?

Edit to clarify: I'm using an external server to host the php, which communicates to my Philips Hue bridge at home. You can assume I forwarded my port correctly. No VPN.

Bob
  • 1,066
  • 2
  • 14
  • 28
  • So not on home network? Over Internet w/ no VPN? So port forwarding? – ficuscr Jun 14 '17 at 21:28
  • @ficuscr I edited my post to clarify that – Bob Jun 14 '17 at 21:35
  • Connection refused means there's no server running on that port. So either the address or port is wrong. – Barmar Jun 14 '17 at 21:40
  • @Bob in that case, [CURLOPT_FOLLOWLOCATION](https://stackoverflow.com/questions/3519939/make-curl-follow-redirects)? That forwarding technically some kind of 3x redirect? Not sure what layer that be... – ficuscr Jun 14 '17 at 21:43
  • @Barmar I can open the URL in my browser. So that part is correct. – Bob Jun 14 '17 at 21:50
  • 1
    What does your [CURL error](http://php.net/manual/en/function.curl-error.php) say? – APAD1 Jun 14 '17 at 21:52
  • @ficuscr That is giving CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set. `safe_mode` is disabled on my server. – Bob Jun 14 '17 at 21:56
  • @APAD1 It's returning: couldn't connect to host – Bob Jun 14 '17 at 21:56
  • So is it doing a redirect? Try a verbose curl from command line elsewhere. If so get rid of open_basedir, add needed directive so it follows. Otherwise sounds like wireshark/burp etc could be needed to see what is going on – ficuscr Jun 14 '17 at 22:10
  • 1
    Tried it on a external ubuntu server: https://pastebin.com/sqMTTsd5 and worked right away. No redirects it seems. So `CURLOPT_FOLLOWLOCATION` shouldn't be necessary right? – Bob Jun 14 '17 at 22:20
  • 1
    "Probably not". I tend to use that directive always - maybe unnecessarily. Think you'll have to sniff the request made from PHP that fails to get to bottom of this. Good Luck! – ficuscr Jun 14 '17 at 22:30
  • Thanks! Now I at least know where to start looking. :) – Bob Jun 14 '17 at 22:33

1 Answers1

1

My guess is it's a users/permissions issue.

Does www-data/http user have permission to use curl on your server? All php scripts will be executed as that user so without correct permissions curl will fail giving this error.

Which user created the php script? Have you changed file permissions to allow other users correct privileges?

Having said all that, you stated that you don't want to use a library for something so simple, why even bother with curl? file_get_contents can PUT, POST etc out of the box.

Get status from bridge as associative array:

$result = json_decode(file_get_contents('http://119.119.20.20:2827/api/Js82jH2lao-pAiws89S9A-k9hHsukw72/lights'), true);

Turn off all lights with a PUT request:

$data = '{"on":false}';
$result = file_get_contents('http://119.119.20.20:2827/api/Js82jH2lao-pAiws89S9A-k9hHsukw72/groups/0/action', null, stream_context_create(array(
'http' => array(
'method' => 'PUT',
'header' => 'Content-Type: application/json' . "\r\n"
. 'Content-Length: ' . strlen($data) . "\r\n",
'content' => $data
),
)));

Just tried it on my Hue setup. Works for me.

miknik
  • 5,748
  • 1
  • 10
  • 26