Skip to content

PHP

Created: 2016-02-07 12:38:24 -0800 Modified: 2018-11-12 21:13:35 -0800

1/30/2017

Solution: use curl.

$curl = curl_init('http://nexusdigital.agency/API/....');
curl_setopt($curl, CURLOPT_PORT, 81);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 81);
$result = curl_exec($curl);

I had a problem with my light-control server when I’d switched web hosts. It turned out that the new host could only connect when I was using port 80 on my personal computer. I ended up just using port 80 instead of trying to get the server administrator to fix anything.

Helpful functions for debugging this:

error_reporting(E_ALL);

ini_set(‘display_errors’, 1);

Also, there’s a site called httpbin.org that will tell you about a query that you send.

contents=filegetcontents(http://httpbin.org/get?user=.contents = file_get_contents('http://httpbin.org/get?user=' . user . ‘&token=’ . token . '&cmd=' . cmd_number . ‘&light=’ . $light_number, false);

echo $contents;

Finally, apparently “allow_url_fopen” needs to be set in php.ini for this to work (although I think it was on in my case already).

Note: if you didn’t want to use file_get_contents for some reason, you could use curl (reference), but note that this won’t help if the server is blocking outgoing ports:

$curl = curl_init('http://somesite.com/lights?user=hi&token=bye&cmd=4&light=6');
curl_setopt($curl, CURLOPT_PORT, 7895);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 7895);
$result = curl_exec($curl);