【问题标题】:Parse socket data on a PHP socket_listener在 PHP socket_listener 上解析套接字数据
【发布时间】:2019-10-10 21:13:15
【问题描述】:

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

如何解析响应以获取一侧的 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

已编辑:我能够通过使用“explode”函数获取 XML 数据,但我不知道如何将 2 张图像作为图像文件获取。有什么建议吗?

任何帮助将不胜感激!

谢谢!

【问题讨论】:

    标签: php sockets tcp


    【解决方案1】:

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

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

    $parsedData = explode("\r\n\r\n", $data);    
    

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

    $xml = new \SimpleXMLElement($parsedData[2]);
    
    1. 图像位于位置 4(数组位置 3 包含必须跳过的标头,例如 Content-Disposition):

    将数据保存为图片的代码是:

    //Generate GD image from raw string.
    if (!$source = @imagecreatefromstring($parsedData[4])){
        $message = 'Save Image: Ko. Img path: "' . $imgFullPath . '". Error details: ' . error_get_last()['message'];
        $this->logger->error($message);
    }else {
        //Save GD image on disk
        imagejpeg($source, $imgFullPath);
    
        //Clean resources.
        imagedestroy($source);
    
        $message = 'Save Image: Ok. Image saved successfully on path: "' . $imgFullPath . '"...';
        $this->logger->info($message);
        $this->io->writeln($message);
    

    【讨论】:

      猜你喜欢
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多