Community discussions

MUM Europe 2020
 
User avatar
SandroDIniz
just joined
Topic Author
Posts: 3
Joined: Wed Apr 17, 2013 1:49 am
Location: São Vicente do Sul - Brazil
Contact:

Structure system for beginners in web API with PHP

Wed Apr 17, 2013 4:39 am

Someone to help build the structure for API PHP WEB CONFIG step by step? with their appropriate folders?


SOS. I would like to help with web programming to add new user in hotspot change settings or add new configuration exists in mikrotik someone help ae?


Português do Brazil
alguém para me ajudar a montar a página em PHP com formulários de criação de profiles, user, etc.
só consegui fazer consultas com codigo API class PHP
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Structure system for beginners in web API with PHP

Wed Apr 17, 2013 7:06 pm

8)
1. Set up the environment
1.1. Set up a web server with PHP 5.3.0 or higher (ideally, the latest PHP version; currently 5.4.14).
1.2. Open a RouterOS terminal (e.g. from Winbox, the "New Terminal" button), type
/ip service enable "api"
1.3. On your web server's firewall, allow the php-cgi executable and/or your web server's executable (e.g. httpd.exe for Apache on Windows) to make outgoing connections. How to do this depends on your OS and firewall software.

2. Install the client.
2.1. Click the link in my signature.
2.2. Click the icon with "PHAR" on it to download a ".phar" file with the client.
2.3. Place the file anywhere on your web server, as long as it is accessible from PHP. If you're new to PHP, you'll want to place the file in the same folder as the PHP file you'll be creating in a moment.

3. Create the PHP file(s) that will connect to the router.
3.1. Include the ".phar" file and create a Client object. Assuming the ".phar" file is in the same folder as the PHP file in question, have the following code first, as a test run:
<?php
use PEAR2\Net\RouterOS;
echo 'Parsing is OK if you are seeing this...';
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
echo 'File included!...';

try {
    $client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
    echo 'Connection OK!';
} catch (Exception $e) {
    echo 'Connection FAIL! Details:', $e;
} 
(NOTE: Adjust your RouterOS IP, username and password)
3.2. Run your test file. The example above should output
Parsing is OK if you are seeing this...File included!...Connection OK!
If that's what you're seeing, you can move on to the real deal.

4. Doing stuff to RouterOS.
4.1a Here's an example file that adds a new concrete user, with a concrete profile called "default":
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

try {
    $client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
    
    $addRequest = new RouterOS\Request('/ip hotspot user add profile=default name=username password=hotspotPassword');
    $client($addRequest);
} catch (Exception $e) {
    echo $e;
} 
4.1b Here's a a more elaborate example file that add a new user with a username and password based on fields in a form, and a concrete profile called "default":
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

header('Content-Type: text/html;charset=UTF-8');
$errors = array();

if (isset($_POST['username']) && isset($_POST['password'])) {
    try {
        $client = new RouterOS\Client('192.168.0.1', 'admin', 'password');

        $addRequest = new RouterOS\Request('/ip hotspot user add profile=default');
        $addReques
            ->setArgument('name', $_POST['username'])
            ->setArgument('password', $_POST['password']);
        $errorResponses = $client($addRequest)->getAllOfType(RouterOS\Response::TYPE_ERROR);

        foreach ($errorResponses as $error) {
            $errors[] = $error->getArgument('message');
        }
    } catch (Exception $e) {
        $errors[] = $e;
    }
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
        <title>Add new user</title>
        <style type="text/css">
            #OK {background-color:darkgreen;color:white;}
            #errors {background-color:darkred;color:white;}
        </style>
    </head>
    <body>
        <form action="" method="post">
            <?php
                if (!empty($_POST)) {
                    if (empty($errors)) {
                        echo '<div id="OK">User added successfully</div>';
                    } else {
                        echo '<div id="errors"><ol>';
                        foreach ($errors as $error) {
                            echo '<li>', $error, '</li>';
                        }
                        echo '</ol></div>';
                    }
                }
            ?>
            <div>
                <ol>
                    <li>
                        <label for="username">Username:</label>
                        <input type="text" id="username" name="username" value="" />
                    </li>
                    <li>
                        <label for="password">Password:</label>
                        <input type="password" id="password" name="password" value="" />
                    </li>
                    <li>
                        <input type="submit" value="Add" />
                    </li>
                </ol>
            </div>
        </form>
    </body>
</html>

If you're seeing the pattern here, doing anything from this point on should be relatively easy. The harder part is the UI, session maintenance, and other common difficulties with any more complex PHP application.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
User avatar
SandroDIniz
just joined
Topic Author
Posts: 3
Joined: Wed Apr 17, 2013 1:49 am
Location: São Vicente do Sul - Brazil
Contact:

Re: Structure system for beginners in web API with PHP

Wed Apr 17, 2013 8:12 pm

8)
1. Set up the environment
1.1. Set up a web server with PHP 5.3.0 or higher (ideally, the latest PHP version; currently 5.4.14).
1.2. Open a RouterOS terminal (e.g. from Winbox, the "New Terminal" button), type
/ip service enable "api"
1.3. On your web server's firewall, allow the php-cgi executable and/or your web server's executable (e.g. httpd.exe for Apache on Windows) to make outgoing connections. How to do this depends on your OS and firewall software.

2. Install the client.
2.1. Click the link in my signature.
2.2. Click the icon with "PHAR" on it to download a ".phar" file with the client.
2.3. Place the file anywhere on your web server, as long as it is accessible from PHP. If you're new to PHP, you'll want to place the file in the same folder as the PHP file you'll be creating in a moment.

3. Create the PHP file(s) that will connect to the router.
3.1. Include the ".phar" file and create a Client object. Assuming the ".phar" file is in the same folder as the PHP file in question, have the following code first, as a test run:
<?php
use PEAR2\Net\RouterOS;
echo 'Parsing is OK if you are seeing this...';
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
echo 'File included!...';

try {
    $client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
    echo 'Connection OK!';
} catch (Exception $e) {
    echo 'Connection FAIL! Details:', $e;
} 
(NOTE: Adjust your RouterOS IP, username and password)
3.2. Run your test file. The example above should output
Parsing is OK if you are seeing this...File included!...Connection OK!
If that's what you're seeing, you can move on to the real deal.

4. Doing stuff to RouterOS.
4.1a Here's an example file that adds a new concrete user, with a concrete profile called "default":
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

try {
    $client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
    
    $addRequest = new RouterOS\Request('/ip hotspot user add profile=default name=username password=hotspotPassword');
    $client($addRequest);
} catch (Exception $e) {
    echo $e;
} 
4.1b Here's a a more elaborate example file that add a new user with a username and password based on fields in a form, and a concrete profile called "default":
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

header('Content-Type: text/html;charset=UTF-8');
$errors = array();

if (isset($_POST['username']) && isset($_POST['password'])) {
    try {
        $client = new RouterOS\Client('192.168.0.1', 'admin', 'password');

        $addRequest = new RouterOS\Request('/ip hotspot user add profile=default');
        $addReques
            ->setArgument('name', $_POST['username'])
            ->setArgument('password', $_POST['password']);
        $errorResponses = $client($addRequest)->getAllOfType(RouterOS\Response::TYPE_ERROR);

        foreach ($errorResponses as $error) {
            $errors[] = $error->getArgument('message');
        }
    } catch (Exception $e) {
        $errors[] = $e;
    }
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
        <title>Add new user</title>
        <style type="text/css">
            #OK {background-color:darkgreen;color:white;}
            #errors {background-color:darkred;color:white;}
        </style>
    </head>
    <body>
        <form action="" method="post">
            <?php
                if (!empty($_POST)) {
                    if (empty($errors)) {
                        echo '<div id="OK">User added successfully</div>';
                    } else {
                        echo '<div id="errors"><ol>';
                        foreach ($errors as $error) {
                            echo '<li>', $error, '</li>';
                        }
                        echo '</ol></div>';
                    }
                }
            ?>
            <div>
                <ol>
                    <li>
                        <label for="username">Username:</label>
                        <input type="text" id="username" name="username" value="" />
                    </li>
                    <li>
                        <label for="password">Password:</label>
                        <input type="password" id="password" name="password" value="" />
                    </li>
                    <li>
                        <input type="submit" value="Add" />
                    </li>
                </ol>
            </div>
        </form>
    </body>
</html>

If you're seeing the pattern here, doing anything from this point on should be relatively easy. The harder part is the UI, session maintenance, and other common difficulties with any more complex PHP application.


perfect, what is your email for details of this sequence of instructions?

ja add to your ICQ ok
add Facebook: https://www.facebook.com/sandro.diniz.16?ref=tn_tnmn
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Structure system for beginners in web API with PHP

Wed Apr 17, 2013 8:16 pm

boen [dot] robot [at] gmail [dot] com

I rarely use my ICQ nowadays... but this forum doesn't let me specify Skype (which is the same as my nickname here, FYI).
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
mktNewbie
just joined
Posts: 8
Joined: Tue May 14, 2013 1:11 am

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 1:42 am

So i'm new to this ordeal here and i just happen to find this thread. My issues is probably the same..however i have done all the steps and tested the file but my php file keeps timing out....

I have enabled the API on the routerBoard and downloaded the .phar file which is in the same folder/directory as the file that i'm working on.

One thing i notice is that when i remove the 1st line on your test code "use PEAR2\NET\RouterOS" the script runs but i get the an error in which it says that it can't find the .phar file.

What else could i do? or did i miss something?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 2:08 am

@mktNewbie

Are you sure you have PHP 5.3.0 or later? The "use" line is not parsable in earlier versions, and thus causes a parsing error.

If you're not sure, run
<?php phpinfo(); 
and check the version number at the top.



Either way, I'm kind'a surprised by the PHAR not found error... unless it's more like "unable to parse PHAR file", which would be due to the same reason.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
mktNewbie
just joined
Posts: 8
Joined: Tue May 14, 2013 1:11 am

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 2:28 am

Well i did check it via the shell command line on the server and it shows as php 5.3.3
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 2:30 am

Try to run the PHAR file from the command line, and see what it says.

(it should output some diagnostics relating to version, extensions and socket functionality)

Also, you should try to run phpinfo() from HTTP, not the command line - sometimes, servers could use one PHP for the command line, and another one for HTTP.


Assuming the PHAR extension is there and PHP 5.3.3 is also at the HTTP front... a message about the PHAR not being found sounds really out of place... I really can't see what would cause it, assuming the file names are all right.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
mktNewbie
just joined
Posts: 8
Joined: Tue May 14, 2013 1:11 am

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 4:35 pm

I executed the .phar file from the command line and got the following:

PEAR2_Net_RouterOS-1.0.0b3.phar: line 1: ?php: No such file or directory

PEAR2_Net_RouterOS-1.0.0b3.phar line 2: syntax error near unexpected token 'get_included_files'
PEAR2_Net_RouterOS-1.0.0b3.phar line 2: 'if (count(get_included_files()) > 1) {'

I do have the .phar file on the directory and I've adjusted the permissions to make sure its executable
 
mktNewbie
just joined
Posts: 8
Joined: Tue May 14, 2013 1:11 am

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 4:36 pm

Also, i did run phpinfo() from the web as well and the php version is 5.3.3
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 4:43 pm

I meant execute the PHAR file using PHP... not as a stand alone executable, i.e.
/path/to/php -f PEAR2_Net_RouterOS-1.0.0b3.phar
What exactly is the error message you get when you run the example file from HTTP with the "use" line being in the file?

If there's nothing on screen, adjust your php.ini to either display errors or log them somewhere, so that you can see.
Last edited by boen_robot on Tue May 14, 2013 4:43 pm, edited 1 time in total.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
mktNewbie
just joined
Posts: 8
Joined: Tue May 14, 2013 1:11 am

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 4:43 pm

I also got the following:

stream_socket_client() function is enabled, SHA-1 HASH (string)

It also gave me a few tips on troubleshooting so i'll look into those to make sure i can reach the router from winbox or ping

Could it be the web server?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 4:46 pm

The troubleshooting tips assume that you're successfully including the PHAR file, but that things go south after that, when you try to actually connect. In other words, they apply when the output of the test file is
Parsing is OK if you are seeing this...File included!...Connection FAIL! Details:
so that's probably not your problem at this point.

(BTW, if you have Skype, I'm online right now and for the next few hours since this post's time... for this issue, it sounds like we'll go back&forth a few more times, so rather than pollute the forum, I think it will be best to discuss it on Skype, and just post the solution after we're done)
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
mktNewbie
just joined
Posts: 8
Joined: Tue May 14, 2013 1:11 am

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 5:59 pm

Okay..i think i might have found the issues. I changed the permissions on the file to 777 (not what i want to keep them at but for testing purposes this will do) and that seem to finally work. I do however have a few questions regarding the API so that would be great if you wouldn't mind helping me out with those (nothing major and i promise i won't take advantage :) )
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Structure system for beginners in web API with PHP

Tue May 14, 2013 6:09 pm

Sure. Go ahead.

(And I'm still at Skype; just sayin')
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
mktNewbie
just joined
Posts: 8
Joined: Tue May 14, 2013 1:11 am

Re: Structure system for beginners in web API with PHP

Wed May 15, 2013 11:56 pm

So i'm still unable to connect to this, i did what you suggested and while i am able to ping the router now i just can't access it via the API yet, still getting the same error and i've got the router directly connected to my modem. Could it b my ISP?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Structure system for beginners in web API with PHP

Fri May 17, 2013 4:37 pm

You mean you can ping the router from the web server (not from a testing PC!), and yet the API doesn't work?

Are you sure the port is 443 at both the router and the script as we talked on Skype? I ask because it's easy to misspell 443 as 433.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
mktNewbie
just joined
Posts: 8
Joined: Tue May 14, 2013 1:11 am

Re: Structure system for beginners in web API with PHP

Wed May 29, 2013 1:46 am

My apologies for the delay response, i had attempted to update my router's firmware and i messed it up so i had to reinstall the firmware back..anyways after doing i retested and it finally worked!!!

I tried it both port 443 and 8728 and both worked, so i just returned to port 8728.

One of the things i wanted to know is, is it possible to update the SSID password? and change the channel of the router via the API?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Structure system for beginners in web API with PHP

Wed May 29, 2013 1:47 am

How do you do it from terminal? (if you can't tell, I don't have a WLAN on my router)

Should be in the same fashion.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)

Who is online

Users browsing this forum: No registered users and 8 guests