php socket发送XML

时间:2013-10-16 00:51:07

标签: php xml sockets tcp

我一直在尝试将此XML块发送到设备,但没有运气。如果我使用wireshark它似乎是发送但它就像我错过了一个小东西,回车或其他东西。我已经发布了需要发送的xml和我的PHP代码。有任何想法吗?对不起,我是PHP的新手。

<c4soap name="SendToDeviceAsync" async="True">
<param name="iddevice" type="number">14</param>
<param name="data" type="string">
<devicecommand owneridtype="" owneriditem="-1">
<command>SET_LEVEL</command>
<params>
<param>
<name>LEVEL</name>
<value type="INTEGER">
<static>10</static>
</value>
</param>
</params>
</devicecommand>
</param>
</c4soap>

PHP代码:

<?php

$host = "192.168.0.210";
$port = 5020;

$package[0]  = "<c4soap name=\"SendToDeviceAsync\" async=\"True\">";
$package[1]  = "<param name=\"iddevice\" type=\"number\">14</param>";
$package[2]  = "<param name=\"data\" type=\"string\">";
$package[3]  = "<devicecommand owneridtype=\"\" owneriditem=\"-1\">";
$package[4]  = "<command>SET_LEVEL</command>";
$package[5]  = "<params>";
$package[6]  = "<param>";
$package[7]  = "<name>LEVEL</name>";
$package[8]  = "<value type=\"INTEGER\">";
$package[9]  = "<static>100</static>";
$package[10] = "</value>";
$package[11] = "</param>";
$package[12] = "</params>";
$package[13] = "</devicecommand>";
$package[14] = "</param>";
$package[15] = "</c4soap>";

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 

echo "Attempting to connect to '$host' on port '$port'...<br>\n";

$result = socket_connect($sock, $host, $port);

if ($result === false) {
    $socketError  = socket_last_error($sock);
    $socketString = socket_strerror($socketError);
    $resultString = var_export($result, 1);
    echo "socket_connect() failed.<br>\n",
         "Reason: ($resultString) ", $socketString, "<br>\n";
}

echo "Sending XML...<br>\n";

foreach ($package as $onePackage)
{
    $buffer = $onePackage . "\n";
    $bufferLength = strlen($buffer);
    socket_write($sock, $buffer, $bufferLength);
}

socket_close($sock);

echo "Completed sending!<br>\n";

0 个答案:

没有答案