在PHP socket_listener上解析套接字数据

时间:2019-05-24 09:32:08

标签: php sockets tcp

我能够打开一个PHP TCP侦听器套接字,但是我不知道如何解析缓冲区。连接到我的套接字的客户端发送带有附加边界数据(带有xml文件和图像文件)的text / html。

我该如何解析响应以在一侧获取XML文件而在另一侧获取图像?

server = socket_create_listen(8086);
socket_getsockname($server, $addr, $port);
if (!$server) {
    $message = 'Start TCP socket: Ko. Could not create socket.';
    $this->logger->info($message);
    die($message);
} else {
    $message = 'Start TCP socket: Ok. TCP socket opened on: ' . $addr . ':' . $port . '.';
    $this->logger->info($message);

    while ($c = socket_accept($server)) {
        socket_getpeername($c, $raddr, $rport);
        $this->logger->info("Received Connection from $raddr:$rport\n");

        $data = '';
        while ($bytes = socket_recv($c, $r_data, 1024, MSG_WAITALL)) {
            $data .= $r_data;
        }

        //Edited: Explode with double line and got data
        $parsedData = explode("\r\n\r\n", $data);
        $xml = new \SimpleXMLElement($parsedData[2]);

        print_r($xml);

        else {
            echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($c)) . "\n";
        }

        socket_close($c);
    }
}
fclose($server);

这是我收到的输出:

从:59048接收到的连接

Read 3096 bytes from socket_recv(). Closing socket...POST /test HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: zh-CN
Content-Type: multipart/form-data;boundary=-------------------------7e13971310878
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: 0.0.0.0:7200
Content-Length: 516032
Connection: Keep-Alive
Cache-Control: no-cache

---------------------------7e13971310878
Content-Disposition: form-data; name="file.xml";filename="file.xml";
Content-Type: text/xml
Content-Length: 2273

<EventNotificationAlert version="2.0" xmlns="http://www.isapi.org/ver20/XMLSchema">
<ipAddress></ipAddress>
<ipv6Address></ipv6Address>
<portNo></portNo>
<!--REST OF XML DATA-->
</EventNotificationAlert>

---------------------------7e13971310878
Content-Disposition: form-data;name="image.jpg";filename="image.jpg";
Content-Type: image/pjpeg
Content-Length: 7164

����JFIF���



       !"$"^C

已编辑:我可以使用“爆炸”功能来获取XML数据,但是我不知道如何将2张图像作为图像文件来获取。有什么建议吗?

任何帮助将不胜感激!

谢谢!

1 个答案:

答案 0 :(得分:0)

我在这里写下我的最终解决方案:

1-将字符串中的数据分解为数组(使用\ r \ n \ r \ n):

choco list --source=schily-artifacts
Chocolatey v0.10.15
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
[NuGet] Not able to contact source 'https://flapjacks.pkgs.visualstudio.com/_packaging/schily-artifacts/nuget/v2'. Error was The remote server returned an error: (401) Unauthorized.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
[NuGet] Not able to contact source 'https://flapjacks.pkgs.visualstudio.com/_packaging/schily-artifacts/nuget/v2'. Error was The remote server returned an error: (401) Unauthorized.
0 packages found.

2-XML实际上位于位置2(位置0和1包含POST HTTP标头和边界起始部分):

$parsedData = explode("\r\n\r\n", $data);    
  1. 图像在位置4上(数组位置3包含标头,例如Content-Disposition,必须跳过):

将数据另存为图像的代码为:

$xml = new \SimpleXMLElement($parsedData[2]);