问题
The port is 5792 and the ip is 123.123.123.123. I am able to send data to the ip, like so:
$host = "tcp://123.123.123.123";
$port = 5792;
$errstr = '';
$errno = '';
$fp = fsockopen($host, $port ,$errno, $errstr, 30);
if (!$fp) {
print 'COULD NOT CONNECT! <br />';
echo "$errstr ($errno)<br />\n";
die();
}
else {
print 'SUCCESS!<br />'
}
The sending seems to also work:
$message = 'hello';
fputs ($fp, $message );
The problem comes in when receiving data:
print fread($fp, 128);
This prints:
hello
... to the screen! So in other words, it's echoing what I'm sending it. Now, I know all messages are encapsulated within an XML element. Within this element a service request can be placed, which is also encapsulated in a XML element.
The encapsulated XML element is called "ROOT" and within this I can place the service request request. Let's call the actual service request I'm trying to accomplish "topUp".
- Assuming there is a root xml element called ROOT, which encapsulates the service request "topUp", what would be the standard way to submit this XML as a string?
- Is it normal to expect a server to echo your request whenever it can't understand what you are saying?
回答1:
It is vital that u know the XML structure of the request command. In any case you can send you command in such a way too
$message = "<root>"."\n";
$message .= "<request>topUp</request>"."\n";
$message .= "</root>"."\n";
fputs ($fp, $message );
But unless you send your request structure defined you might not get the result you want.
来源:https://stackoverflow.com/questions/11773769/creating-a-tcp-socket-connection-and-sending-through-a-xml-request-in-order-to-g