From 33e00299cd60ebdc852c5f5b9d70136ba6da6f84 Mon Sep 17 00:00:00 2001 From: pasha Date: Sun, 19 Aug 2018 23:55:47 +0300 Subject: [PATCH] small fixes, readme updated --- README.md | 41 ++++++++++++++++++++++++++++++++++++++ src/Client.php | 8 +------- src/Interfaces/ClientInterface.php | 3 +-- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9ff35fe..2424ee4 100644 --- a/README.md +++ b/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 diff --git a/src/Client.php b/src/Client.php index 5782abf..fecdf83 100644 --- a/src/Client.php +++ b/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)); diff --git a/src/Interfaces/ClientInterface.php b/src/Interfaces/ClientInterface.php index d118336..0fb4c5b 100644 --- a/src/Interfaces/ClientInterface.php +++ b/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; }