Browse Source

small fixes, readme updated

tags/0.1 0.1
pasha 7 years ago
parent
commit
33e00299cd
  1. 41
      README.md
  2. 8
      src/Client.php
  3. 3
      src/Interfaces/ClientInterface.php

41
README.md

@ -1,3 +1,8 @@
[![Latest Stable Version](https://poser.pugx.org/evilfreelancer/routeros-api-php/v/stable)](https://packagist.org/packages/evilfreelancer/routeros-api-php)
[![Total Downloads](https://poser.pugx.org/evilfreelancer/routeros-api-php/downloads)](https://packagist.org/packages/evilfreelancer/routeros-api-php)
[![License](https://poser.pugx.org/evilfreelancer/routeros-api-php/license)](https://packagist.org/packages/evilfreelancer/routeros-api-php)
[![Scrutinizer CQ](https://scrutinizer-ci.com/g/evilfreelancer/routeros-api-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/evilfreelancer/routeros-api-php/)
# RouterOS PHP7 API Client
composer require evilfreelancer/routeros-api-php
@ -54,6 +59,42 @@ $response = $client->write($query)->read();
var_dump($response);
```
### How to write queries
You can write absolutely any queries to your router, for this you
need to create a "Query" object whose first argument is the
required command, after this you can add the attributes of the
command to "Query" object.
More about attributes and "words" from which this attributes
should be created [here](https://wiki.mikrotik.com/wiki/Manual:API#Command_word).
```php
use \RouterOS\Query;
// One line query: Get all packages
$query = new Query('/system/package/getall');
// Multiline query: Enable interface and add tag
$query = new Query('/interface/set');
$query
->add('=disabled=no')
->add('=.id=ether1')
->add('.tag=4');
// Multiline query: Get all ethernet and VLAN interfaces
$query = new Query('/interface/print');
$query
->add('?type=ether')
->add('?type=vlan')
->add('?#|');
// Multiline query: Get all routes that have non-empty comment
$query = new Query('/ip/route/print');
$query
->add('?>comment=');
```
## Links
* [Cloud Hosted Router](https://mikrotik.com/download#chr) - Virtual images of RouterOS for your hypervisor

8
src/Client.php

@ -89,10 +89,9 @@ class Client implements Interfaces\ClientInterface
* Send write query to RouterOS (with or without tag)
*
* @param Query $query
* @param string|null $tag
* @return ClientInterface
*/
public function write(Query $query, string $tag = null): ClientInterface
public function write(Query $query): ClientInterface
{
// Send commands via loop to router
foreach ($query->getQuery() as $command) {
@ -100,11 +99,6 @@ class Client implements Interfaces\ClientInterface
fwrite($this->_socket, $this->encodeLength(\strlen($command)) . $command);
}
// If tag is not empty, send to socket
if (null !== $tag) {
fwrite($this->_socket, $this->encodeLength(\strlen('.tag=' . $tag)) . '.tag=' . $tag);
}
// Write zero-terminator
fwrite($this->_socket, \chr(0));

3
src/Interfaces/ClientInterface.php

@ -62,9 +62,8 @@ interface ClientInterface
* Send write query to RouterOS (with or without tag)
*
* @param Query $query
* @param string|null $tag
* @return ClientInterface
*/
public function write(Query $query, string $tag = null): ClientInterface;
public function write(Query $query): ClientInterface;
}
Loading…
Cancel
Save