\Communicator.php
RouterOS API client implementation.
This package allows you to read and write information from a RouterOS host using MikroTik's RouterOS API.
PHP version 5
- Author
- Vasil Rangelov
- Category
- Net
- Copyright
- 2011 Vasil Rangelov
- License
- LGPL License 2.1
- Link
- http://netrouteros.sourceforge.net/
- Package
- PEAR2_Net_RouterOS
- Version
- 1.0.0b1
\PEAR2\Net\RouterOS\Communicator
A RouterOS communicator.
Implementation of the RouterOS API protocol. Unlike the other classes in this package, this class doesn't provide any conviniences beyond the low level implementation details (automatic word length encoding/decoding, charset translation and data integrity), and because of that, its direct usage is strongly discouraged.
- See
- \PEAR2\Net\RouterOS\Client
- Author
- Vasil Rangelov
- Category
- Net
- License
- LGPL License 2.1
- Link
- http://netrouteros.sourceforge.net/
Constants

CHARSET_REMOTE
= 0
Used when getting/setting the (default) remote charset.
The remote charset is the charset in which RouterOS stores its data. If you want to keep compatibility with your Winbox, this charset should match the default charset from your Windows' regional settings.
Properties


array
$charsets= 'array('
array(
Details- Type
- array


array
$defaultCharsets= 'array(
self::CHARSET_REMOTE => null'
array(
self::CHARSET_REMOTE => null
Details- Type
- array
Methods


__construct(
string $host, int $port
=
8728, bool $persist
=
false, float $timeout
=
null, string $key
=
'', resource $context
=
null
)
:
void
Creates a new connection with the specified options.
Name | Type | Description |
---|---|---|
$host | string | Hostname (IP or domain) of the RouterOS server. |
$port | int | The port on which the RouterOS server provides the API service. |
$persist | bool | Whether or not the connection should be a persistent one. |
$timeout | float | The timeout for the connection. |
$key | string | A string that uniquely identifies the connection. |
$context | resource | A context for the socket. |
- See
- \PEAR2\Net\RouterOS\sendWord()


close(
)
:
bool
Closes the opened connection, even if it is a persistent one.
Type | Description |
---|---|
bool | TRUE on success, FALSE on failure. |


decodeLength(
\PEAR2\Net\Transmitter\StreamTransmitter $trans
)
:
int
Decodes the lenght of the incoming message.
Decodes the lenght of the incoming message, as specified by the RouterOS API.
Name | Type | Description |
---|---|---|
$trans | \PEAR2\Net\Transmitter\StreamTransmitter | The transmitter from which to decode the length of the incoming message. |
Type | Description |
---|---|
int | The decoded length |


encodeLength(
int $length
)
:
string
Encodes the length as requred by the RouterOS API.
Name | Type | Description |
---|---|---|
$length | int | The length to encode |
Type | Description |
---|---|
string | The encoded length |


getCharset(
int $charsetType
)
:
string|array
Gets the charset(s) for this connection.
Name | Type | Description |
---|---|---|
$charsetType | int | Which charset to get. Valid values are the CHARSET_* constants. Any other value is treated as {@link CHARSET_ALL}. |
Type | Description |
---|---|
string|array | The current charset. If $charsetType is {@link CHARSET_ALL}, the current values will be returned as an array with the types as keys, and charsets as values. |
- See
- \PEAR2\Net\RouterOS\getDefaultCharset()
- See
- \PEAR2\Net\RouterOS\setCharset()


getDefaultCharset(
int $charsetType
)
:
string|array
Gets the default charset(s).
Name | Type | Description |
---|---|---|
$charsetType | int | Which charset to get. Valid values are the CHARSET_* constants. Any other value is treated as {@link CHARSET_ALL}. |
Type | Description |
---|---|
string|array | The current charset. If $charsetType is {@link CHARSET_ALL}, the current values will be returned as an array with the types as keys, and charsets as values. |
- See
- \PEAR2\Net\RouterOS\setDefaultCharset()


getNextWord(
)
:
string
Get the next word in queue as a string.
Get the next word in queue as a string, after automatically decoding its length.
Type | Description |
---|---|
string | The word. |
- See
- \PEAR2\Net\RouterOS\close()


getNextWordAsStream(
)
:
resource
Get the next word in queue as a stream.
Get the next word in queue as a stream, after automatically decoding its length.
Type | Description |
---|---|
resource | The word, as a stream. |
- See
- \PEAR2\Net\RouterOS\close()


getTransmitter(
)
:
\PEAR2\Net\Transmitter\SocketClientTransmitter
Gets the transmitter for this connection.
Type | Description |
---|---|
\PEAR2\Net\Transmitter\SocketClientTransmitter | The transmitter for this connection. |


iconvStream(
string $in_charset, string $out_charset, resource $stream
)
:
resource
Uses iconv to convert a stream from one charset to another.
Name | Type | Description |
---|---|---|
$in_charset | string | The charset of the stream. |
$out_charset | string | The desired resulting charset. |
$stream | resource | The stream to convert. |
Type | Description |
---|---|
resource | A new stream that uses the $out_charset. The stream is a subset from the original stream, from its current position to its end. |


sendWord(
string $word
)
:
int
Sends a word.
Sends a word and automatically encodes its length when doing so.
Name | Type | Description |
---|---|---|
$word | string | The word to send. |
Type | Description |
---|---|
int | The number of bytes sent. |
- See
- \PEAR2\Net\RouterOS\sendWordFromStream()
- See
- \PEAR2\Net\RouterOS\getNextWord()


sendWordFromStream(
string $prefix, resource $stream
)
:
int
Sends a word based on a stream.
Sends a word based on a stream and automatically encodes its length when doing so. The stream is read from its current position to its end, and then returned to its current position. Because of those operations, the supplied stream must be seekable.
Name | Type | Description |
---|---|---|
$prefix | string | A string to prepend before the stream contents. |
$stream | resource | The stream to send. |
Type | Description |
---|---|
int | The number of bytes sent. |
- See
- \PEAR2\Net\RouterOS\sendWord()


setCharset(
mixed $charset, int $charsetType
=
self::CHARSET_ALL
)
:
string|array
Sets the charset(s) for this connection.
Sets the charset(s) for this connection. The specified charset(s) will be used for all future words. When sending, {@link CHARSET_LOCAL} is converted to {@link CHARSET_REMOTE}, and when receiving, {@link CHARSET_REMOTE} is converted to {@link CHARSET_LOCAL}. Setting NULL to either charset will disable charset convertion, and data will be both sent and received "as is".
Name | Type | Description |
---|---|---|
$charset | mixed | The charset to set. If $charsetType is {@link CHARSET_ALL}, you can supply either a string to use for all charsets, or an array with the charset types as keys, and the charsets as values. |
$charsetType | int | Which charset to set. Valid values are the Communicator::CHARSET_* constants. Any other value is treated as {@link CHARSET_ALL}. |
Type | Description |
---|---|
string|array | The old charset. If $charsetType is {@link CHARSET_ALL}, the old values will be returned as an array with the types as keys, and charsets as values. |
- See
- \PEAR2\Net\RouterOS\setDefaultCharset()


setDefaultCharset(
mixed $charset, int $charsetType
=
self::CHARSET_ALL
)
:
string|array
Sets the default charset(s) for new connections.
Name | Type | Description |
---|---|---|
$charset | mixed | The charset to set. If $charsetType is {@link CHARSET_ALL}, you can supply either a string to use for all charsets, or an array with the charset types as keys, and the charsets as values. |
$charsetType | int | Which charset to set. Valid values are the CHARSET_* constants. Any other value is treated as {@link CHARSET_ALL}. |
Type | Description |
---|---|
string|array | The old charset. If $charsetType is {@link CHARSET_ALL}, the old values will be returned as an array with the types as keys, and charsets as values. |
- See
- \PEAR2\Net\RouterOS\setCharset()


verifyLengthSupport(
int $length
)
:
void
Verifies that the length is supported.
Verifies if the specified length is supported by the API. Throws a {@link LengthException} if that's not the case. Currently, RouterOS supports words up to 0xFFFFFFF in length, so that's the only check performed.
Name | Type | Description |
---|---|---|
$length | int | The length to verify. |