Creating a TCP socket connection and sending through a XML request in order to get an XML response?

不问归期 提交于 2019-12-11 11:36:16

问题


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".

  1. 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?
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!