I’m almost figuring out what I’m doing here, but I have a couple of questions:
After establishing a socket connection using fsockopen, I get a filepoint of stream type. Ok.
We are now able to use fwrite and other functions like if it was a file.
/**
* @desc sends a XML frame with 4 bytes at the beginning
*
*/
public function enviarFrame($xml)
{
$frame = fwrite($this->_filePointer, pack('N', (strlen($xml)+4)).$xml);
return $frame;
}
Should we return $frame, or this is redundant, and doing only:
public function enviarFrame($xml)
{
fwrite($this->_filePointer, pack('N', (strlen($xml)+4)).$xml);
}
Will be enough ?
The pack with ‘N’ means, format the returned integer as Big Endian.
The server where this needs to be send, seems to request this “format?” my question is, what is the INT format of php by default? Or this does “something” to our returned strlen that I’m not aware about?
/**
* @desc sends a XML frame with 4 bytes at the beginning
*
*/
public function enviarFrame($xml)
{
$frame = fwrite($this->_filePointer, pack('N', (strlen($xml)+4)).$xml);
return $frame;
}
We will have returned the number of bytes written. If we wanted that, this would be the solution, but we just want to write to the socket, hence, the method without return will be enough.
If you’ll never check the return value of the method, then don’t return anything. If you’ll have some want/need to check the return value (e.g. to confirm that some bytes were written) then return something useful. It really is that simple.