1

我不是 php 专家,但我可以做基础知识,所以我编写了一个 php 脚本,当我运行某个 URL 时,即当我进入和离开厨房时,它应该打开或关闭厨房中的色调灯:

$bulb   = $_GET['bulb'];
$status = $_GET["state"];
settype($status,"Boolean"); 

$data = array("on" => $status); 


$lat     = 48.1351253;    // North
$long    = 11.581980599999952;    // East
$offset = 1;    // difference between GMT and local time in hours

$zenith = 90+50/60;
$sunrise   = date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith,$offset);
$sunset = date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
$now        = date("H") + date("i") / 60 + date("s") / 3600; 

if ($sunrise < $sunset)
{
    if (($now > $sunrise) && ($now < $sunset))
    {
        echo "It's daytime";
    }
    else
    {
        if (($now > $sunrise) || ($now < $sunset))
        {
            echo "It's nighttime";

            $data_string = json_encode($data);
            echo $data_string;

            $ch = curl_init("http://xx.xx.xx.xx/api/xxxxxx/lights/$bulb/state");
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
            );

            $result = curl_exec($ch);
            print_r($result);
        }
    }
}

脚本应该只在日落和日出之间打开灯光。但是当我离开厨房时,它也应该再次关灯。

$bulb占位符应该是我放在 uRL 中的灯泡。

不知何故,它不起作用。脚本显示了正确的时间,但即使是白天也会打开灯。

我现在使用的 URL 是http://xxxxxx.com/hue.php?bulb=2&state=1打开 state=0 关闭。

如果您能帮助我编写此脚本,那就太好了。谢谢。

4

0 回答 0