PHP:如何从UDP服务器获取答案

时间:2018-06-28 05:53:50

标签: php sockets

我正在尝试为Yeelight灯实施此发现消息(链接至手册:http://www.yeelight.com/download/Yeelight_Inter-Operation_Spec.pdf

The searching message generated by 3rd device should follow below format and rules and
being sent to multi-cast address 239.255.255.250:1982 over UDP.
-------------------------------------------------------------
M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1982
MAN: "ssdp:discover"
ST: wifi_bulb
-------------------------------------------------------------
1. The start line must be "M-SEARCH * HTTP/1.1" without any leading LWP.
2. "HOST" header is optional, if it's present, the value should be "239.255.255.250:1982".
3. "MAN" header is required. The value for "MAN" header must be "ssdp:discover",
double quotes included.
4. "ST" header is required. The value for "ST" header must be "wifi_bulb".
5. The headers are case-insensitive while the start line and all the header values are case
sensitive. Each line should be terminated by "\r\n".

我承认:我无法使用套接字,所以我确实尝试并失败了(没有答案,在var dump上只有“ int(116)”)。

$discover_message = "M-SEARCH * HTTP/1.1\r\n
        HOST: 239.255.255.250:1982\r\n
        MAN: \"ssdp:discover\"\r\n
        ST: wifi_bulb\r\n";

        $socket=socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die("Cannot create a socket");
        socket_connect($socket,'239.255.255.250','1982') or die("Couldnot connect to socket");
        $ret = socket_sendto($socket,$discover_message,strlen($discover_message),0,'239.255.255.250','1982');
        socket_set_nonblock($socket);
        var_dump($ret);

我也尝试过添加

$socket=socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die("Cannot create a socket");
        socket_connect($socket,'239.255.255.250','1982') or die("Couldnot connect to socket");
        $ret = socket_sendto($socket,$discover_message,strlen($discover_message),0,'239.255.255.250','1982');
        socket_set_nonblock($socket);
        var_dump($ret);
        $sec = 0;
        $usec = $sec === null ? null : (($sec - floor($sec)) * 1000000);
        $r = array($socket);
        while (socket_select($r, $x, $x, $sec, $usec)) {
            $data = socket_read($socket, 4096, PHP_BINARY_READ);
            var_dump($data);
        }

(阅读如何在另一个github项目上实现,该项目使用了一个包装容器本身的包装容器)

但是我再次得到int(116)

答案必须是(如手册所述)

-------------------------------------------------------------
HTTP/1.1 200 OK
Cache-Control: max-age=3600
Date:
Ext:
Location: yeelight://192.168.1.239:55443
Server: POSIX UPnP/1.0 YGLC/1
id: 0x000000000015243f
model: color
fw_ver: 18
support: get_prop set_default set_power toggle set_bright start_cf stop_cf set_scene
cron_add cron_get cron_del set_ct_abx set_rgb 

power: on
bright: 100
color_mode: 2
ct: 4000
rgb: 16711680
hue: 100
sat: 35
name: my_bulb
-------------------------------------------------------------

如何获得手册中所述的灯泡清单?

谢谢

0 个答案:

没有答案