socket_recv我想要可读数据而不是字节数

Hello All hopefully you guys don't shoot me with virtual guns if already asked but, here goes.

I am socket_send to an ip/port i see the server receives and responds with data back. My code receives xxxx bytes. I want to know what those bytes entail IE: xml string back so I can parse and use the data back in my application. cURL is not doable in this situation because the server receives headers and does not respond at all.

**Code:**
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "
";
} else {
echo "OK.
";
}

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "
";
} else {
echo "OK.
";
}

echo "Attempting to connect to '$address' on port '$port'...";
$result = socket_connect($socket, $address, $port);
if ($result === false) {
echo "socket_connect() failed.
Reason: ($result) " . socket_strerror(socket_last_error($socket)) . "
";
} else {
echo "OK.
";
}

$in = $xml;
$out = '';

echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.
";
socket_shutdown($socket, 1);
echo "Reading response:

";
$buf = '';
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
echo "Read $bytes bytes from socket_recv(). Closing socket...";
} else {
echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "
";
}
socket_close($socket);

echo $buf . "
";
echo "OK.

";
?>

Response:

TCP/IP Connection OK. OK. Attempting to connect to 'xxx.xxx.xxx.xxx' on port '9000'...OK. Sending HTTP HEAD request...OK. Reading response: Read 1365 bytes from socket_recv(). Closing socket... OK.

Firstly, you can use cURL and not send headers! Just an fyi.

Secondly, it's going to be tricky to determine exactly what type of content it is... Is the server returning any sort of "content type"? If so, you may be able to read that.

Afterwards, you can then use $buf and then attempt to use XML libraries to parse the string (such as PHP's SimpleXML Library).

If, for example, it doesn't parse the XML, then you know it's either a regular string or something else.

cURL should return the content type, however.

after crazy headaches and several medicine hits i found the issue: 1 line OMG

If any one else gets this issue with socket the line after socket_close($socket); I added echo html_entity_decode($buf);