art-of-wifi / unifi-api-browser Goto Github PK
View Code? Open in Web Editor NEWTool to browse data exposed by Ubiquiti's UniFi Controller API
License: MIT License
Tool to browse data exposed by Ubiquiti's UniFi Controller API
License: MIT License
Hello,
Thanks for your API Browser.
Its working on localhost.
But not working on server.
Please Any Idea...
EDITS: Last error_log:
[10-Jul-2017 03:17:03 UTC] cURL error: Failed to connect to 127.0.0.1 port 8443: Connection refused
[10-Jul-2017 03:17:03 UTC] PHP Catchable fatal error: Object of class UnifiApi could not be converted to string in /home/mikael1142/public_html/guest/s/default/auth.php on line 34
Hello guys,
first of all, thank you very much for the api, I am sure it will be very useful to me when rightly used!
However, and as you can imagine, I have troubles in using the php class.
Can you please guide me on what I am doing wrong here? (I know it's not stackoverflow, but I sense that you can help me in a substantially more efficient way...)
if($prenom && $gender) {
//Minutes to authorize, change to suit your needs
$minutes = 240;
$login = new UnifiApi();
$login->login();
$authorize_guest = new UnifiApi();
$authorize_guest->authorize_guest($mac, $minutes);
$logout = new UnifiApi();
$logout->logout();
if($authorize_guest === true) {
header("Location: https://www.google.ch");
}
else {
$error_log = new UnifiApi();
$error_log->get_last_error_message();
echo $error_log;
}
}
I would understand that you don't want to get into this piece of code or explaining me why my php is wrong. If that's the case, could you redirect me to good material in order for me to correct my beginner mystakes?
Thanks!
Hi,
Could you make an option to search for a Mac Address and show the site that its in.
Now we need to go to every site.
Would be very nice to have an option to search all sites at once and display the mac Address and the Site that the "AP / Mac" is in.
Hello, im having this issue....
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\unifi\index.php on line 452
i enabled debug mode, and this is what i get...
`Array
(
[url] => https://XXXXXXXXXX:8443/api/login
[content_type] => application/json;charset=UTF-8
[http_code] => 200
[header_size] => 332
[request_size] => 217
[filetime] => -1
[ssl_verify_result] => 18
[redirect_count] => 0
[total_time] => 0.656
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0.031
[size_upload] => 42
[size_download] => 41
[speed_download] => 62
[speed_upload] => 64
[download_content_length] => 41
[upload_content_length] => 42
[starttransfer_time] => 0.656
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => XXXXXXXXXXX
[certinfo] => Array
(
)
[primary_port] => 8443
[local_ip] => 172.16.10.166
[local_port] => 53698
)
-----RESPONSE----------------
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-FRAME-OPTIONS: SAMEORIGIN
Set-Cookie: unifises=XXXXXXXXXXXX; Path=/; Secure; HttpOnly
Set-Cookie: csrf_token=XXXXXXXXXXXXXX; Path=/; Secure
Content-Type: application/json;charset=UTF-8
Content-Length: 41
Date: Wed, 06 Jul 2016 03:25:47 GMT
{ "data" : [ ] , "meta" : { "rc" : "ok"}}
-----------------------------`
Then i have a logout and seems to be ok aswell... what is going on? im trying unifi 5.1.1 Beta at the moment..
Thank you in advance...
Good day, I am struggling to get the unifi-api-browser going. I have follow your instructions to but getting now where.
Unifi Controller runs as follow:
OS: Ubuntu server 16.04
Unifi controller ver 5.7.30
Errror Message:
No sites available
This is probably caused by incorrect access rights in the UniFi controller for the credentials provided in config.php, or else check your web server error logs. After updating your credentials, please restart your browser or use the Reset PHP session function in the dropdown menu on the right, before attempting to use the API browser tool again."
Here are the config.php file. I just used an simple username and password to test. Also remove the last part of the ip address.
config.php:
If you could please assist I would really appreciated.
Thank you
Gerrit
I built this for personal use, but maybe you will find it useful
https://hub.docker.com/r/bender77/unifi-api-browser/
dear friend,
already i asked question about how to limit multi use voucher. now that have developed in unifi controller 5.5.11 beta version. so please check that and quickly update api browser tool.
Hi,
There is already the set_wlansettings($wlan_id, $name, $x_passphrase)
function. But it would be nice to have also an set_psk
function if you only want to change the psk and cannot provide the wlan's name without accessing the unifi api to get the corresponding wlan name.
I have created an adoption function that you could add to you API.
if (!$this->is_loggedin) return FALSE;
$mac = strtolower($mac);
$return = FALSE;
$json = json_encode(array('mac' => $mac, 'cmd' => 'adopt'));
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/cmd/devmgr','json='.$json));
if (isset($content_decoded->meta->rc)) {
if ($content_decoded->meta->rc == 'ok') {
$return = TRUE;
}
}
return $return;
}
When cloning the recent version, the about tool always reports an update is available. The latest commit looks like the version const was never updated if I'm correct?
Thanks
I've detected an issue when disabling guest WLAN.
All works fine when I have just 1 single AP.
But when adding a 2nd AP, disabling the guest WLAN only disables 3 of them instead of the 4 of them
(2,4GHz and 5Ghz for the AP 1 and AP 2).
The guest WLAN on 1 of the AP stays enabled after running the disable command. If I run the command a second time, it does nothing (I think it detects no changes so, so does not trigger the commit.
I'm using this 2 AP models:
I have this setup with a valid wildcard SSL certificate, but when accessing on HTTPS the only return I get is:
false
However I have just noticed that list sites on this controller does actually return information.
Hi,
I followed your instructions however I am getting a 500 error when I try and access index.php, I have copied info.php to the /var/www/html/UniFi-API-browser directory ant that works fine, can you point me in any directions?
Regards
Sean
A year ago I've implemented on my side a feature in this project to turn wifi off. I'd like to contribute this feature to the project.
Turning WiFi on and off can be done by changing the wlan_group of an access point, see here
I've created the following function:
/**
* Assign device device to another WLAN group (e.g to turn wifi off)
* -----------------------------------
* return TRUE on success
* required parameter <wlantype_id> = WLAN type, can be either 'ng' (for WLANs 2G (11n/b/g)) or 'na' (WLANs 5G (11n/a/ac))
* required parameter <device_id> = id of the device to be modified
* required parameter <group_id> = id of the WLAN group to assign device to
*/
public function set_devicewlangroup($wlantype_id, $device_id, $group_id) {
if (!$this->is_loggedin) return FALSE;
$return = FALSE;
$json = json_encode(array('wlan_overrides' => array(),'wlangroup_id_'.$wlantype_id => $group_id ));
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/device/'.$device_id,'json='.$json));
if (isset($content_decoded->meta->rc)) {
if ($content_decoded->meta->rc == 'ok') {
$return = TRUE;
}
}
return $return;
}
You have to call that function like this:
$data = $unifidata->set_devicewlangroup('ng', '<deviceId>','<wlanGroupID>');
$data = $unifidata->set_devicewlangroup('na', '<deviceId>','<wlanGroupID>');
$deviceId = $_GET['deviceId']
$wlanGroupID = $_GET['wlanGroupID']
within the switch/case block?
need to create vouchers and print vouchers from api
Whenever you observe an error like this in your web server's error log, it is often an indication of a connectivity issue between the server running the API Browser tool and your controller.
curl error: Failed to connect to <hostname> port 8443: Connection timed out
PHP Warning: Invalid argument supplied for foreach() in path/to/api_browser_tool/index.php on line 415
Next steps:
How to actually check this depends on the firewall that is controlling the connections in/out but running this command from the API Browser tool server's command line, will probably provide some useful errors:
wget https://<hostname or ip>:8443
This error typically occurs in shared hosting environments where commercial web hosting providers often apply strict firewall rules by default.
The file config.php is not readable or does not exist.' . '
code:
require('config.php');
require('phpapi/class.unifi.php');
$unifidata=new unifiapi($controlleruser, $controllerpassword, $controllerurl, $controllerversion);
$unifidata->debug=TRUE;
$loginresults=$unifidata->login();
$clients=$unifidata->list_clients(3600);
var_dump($clients);
debug output:
-------URL & PAYLOAD---------
https://localhost:8443/api/s/5.2.9/stat/sta
---------RESPONSE------------
{ "data" : [ ] , "meta" : { "msg" : "api.err.NoSiteContext" , "rc" : "error"}}
-----------------------------
array(0) { }
---------cURL INFO-----------
Array
(
[url] => https://localhost:8443/logout
[content_type] =>
[http_code] => 302
[header_size] => 308
[request_size] => 154
[filetime] => -1
[ssl_verify_result] => 18
[redirect_count] => 0
[total_time] => 0.028923
[namelookup_time] => 3.2E-5
[connect_time] => 0.000126
[pretransfer_time] => 0.028362
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => -1
[starttransfer_time] => 0.028904
[redirect_time] => 0
[redirect_url] => https://localhost:8443/manage
[primary_ip] => 127.0.0.1
[certinfo] => Array
(
)
[primary_port] => 8443
[local_ip] => 127.0.0.1
[local_port] => 58202
)
Hello,
I just wanted to let you know, that I uploaded a first version of an alternative UniFi HotSpot Manager
It's based on your great UniFi PHP API and works for some weeks in one testing environment now.
When you have some suggestion, just let me know.
PS: You can close this issue or leave it open. It's up to you ;)
In old versions Unifi, unifi_sh_api has funcions update-sta i.e.
<code="php">
unifi_update_sta() {
if [ $# -lt 1 ] ; then
echo "Usage: $0 "
exit -1
fi
mac=$1
${curl_cmd} --data "json={'cmd':'update-sta', 'mac':'${mac}', 'name':'$2', 'email':'$3'}" $baseurl/api/cmd/stamgr
}
Where is this functions in new version? It was deprecated?
this is save my life.
now, im able to create hundreds site thorugh your awesome API. thanks! ๐
By the way, any recoomendation where did you find the argument for each command on your API ?
i mean command like : /stat/guest , /stat/sta, /stat/user etc.
im quite strugle to find it.
once again, thanks!
cheers,
henry
am getting the following error trying to use this:
DEBUG api - /api/login finished (647 handling, 1 rendering)
[2017-07-23 22:45:36,782] INFO api - [api] api.err.NoSiteContext: /api/s/Default/cmd/stamgr
[2017-07-23 22:45:36,784] DEBUG api - /api/s/Default/cmd/stamgr finished (1 handling, 0 rendering)
my site is definetly called 'Default'
What is the difference between listing online clients, guests and users? Is one used with an open SSID and the other used for when there is a password required?
The WebUI allows listing of known clients (in the Insights-Section).
I would like to be able, to get a list of these clients. Background: We block unknown clients on a regular basis. The count of blocked devices is limited, so my plan is to remove the oldest entry in this list.
Is there any chance to do this with the available API?
Hello,
I love you work, great job
Recently however the set_ap_wlangroup function has stopped working, it worked for us in the past. A few months ago that particular function just stopped working and now my script will still create the wlan group and the wlan but wont set it to the ap
Screenshots will be added here.
hi this is my config file:
$controlleruser = 'xx'; // the user name for access to the UniFi Controller
$controllerpassword = 'xx'; // the password for access to the UniFi Controller
$controllerurl = 'unifi.ubnt.com'; // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443'
$controllerversion = '5.2.9'; // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0)
but i cannot select a site in the browser api
Hi, can we get the info about which user is connected to which access point?
After selecting the data you would like to see (and what gets shown in the window), have an option to export a "curl" (preferred) or "wget" command line to get the exact same output in the command line.
This would help me greatly in what I'm trying to accomplish with device presence and home automation.
Hey man, it's me again.
This is not related to your repo directly - I just can't find the answer anywhere else:
What IP do I call to reach the controller from outside the LAN? For example, if my hotspot script is on Amazon AWS and the controller is behind an Edge Router, how would I determine the right IP to call to authorize the device on the controller?
You're my only hope!
I am interested in trying this UniFi API tool, BUT, it appears that the latest version (5.10.12) breaks the API apparently...
I've tried with a simple bash script:
username="admin"
password="MyPassword"
baseurl="https://unifi.domain.com:8443"
curl --data "{\"username\":\"$username\", \"password\":\"$password\"}" $baseurl/api/login
And this results in:
{"meta":{"rc":"error","msg":"api.err.Invalid"},"data":[]}
The actual error in the API Browser is:
HTTP response status: 400
This is probably caused by a UniFi controller login failure. Please check your credentials and try again.
Problem in guest authorization in auth_guest_basic.php
How can i authenticate user using the voucher i have just created..?
If you see such an error in your PHP error logs you don't have enough memory allocated to PHP. You can refer to the About modal in the latest version, to see how much memory is allocated to PHP, and what the peak memory consumption was for the latest page load.
If this happens with large data collections such as "events", the recommended fix is to increase the limit in your php.ini file, if you have sufficient RAM. Here's just one of the many how to's:
http://tutorials.hostucan.net/how-to-increase-php-memory-limit/
helm my troubles
refer to the https://community.ubnt.com/t5/UniFi-Wireless/How-to-Display-connected-Clients-in-Grafana/m-p/2139761/highlight/false#M262208 page
curl -m 30 http://192.168.0.1/UniFi-API-browser-master/?action=list_clients > clientslist data=$(cat clientslist | grep objects_count | grep -Eo [0-9]+)
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 78820 0 78820 0 0 116k 0 --:--:-- --:--:-- --:--:-- 116k
curl: (6) Could not resolve host: data=40
curl -i -X POST "http://192.168.0.1:8086/write?db=wificlients&precision=m" --data-binary 'clients;wifi=all value='$data''
HTTP/1.1 400 Bad Request
Content-Type: application/json
Request-Id: ca973028-2c3b-11e9-9367-54bef7386780
X-Influxdb-Build: OSS
X-Influxdb-Error: unable to parse 'clients;wifi=all value=': missing field value
X-Influxdb-Version: 1.7.3
X-Request-Id: ca973028-2c3b-11e9-9367-54bef7386780
Date: Sat, 09 Feb 2019 07:24:52 GMT
Content-Length: 75
{"error":"unable to parse 'clients;wifi=all value=': missing field value"}
First of all, let me congratulate you for the excellent library. Thanks!
I have created an external captive portal and successfully authorized devices.
However, when my PHP code sends the authorize_guest($mac, $duration) command, the WiFi for the mobile device in question is actually disconnected.
This happens on both Android and iOS as far as I can tell.
Here is the code I am using:
$controlleruser = $CFG->unifi_user;
$controllerpassword = $CFG->unifi_pass;
$controllerurl = 'https://'.$server.':'.$CFG->unifi_port;
$controllerversion = '5.4.11';
$cookietimeout = '3600';
$theme = 'bootstrap';
$debug = false;
$duration = $CFG->unifi_minutes;
$note = '';
$site_id = 'default';
require_once('class.unifi.php');
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifidata->set_debug($debug);
$loginresults = $unifidata->login();
/**
* To add note to a new device we need to do the following before authorizing the device:
* - first block the device to get an entry in the user collection
* - get the device id from the user collection
* - add note to the device
* - then unblock the device again
*/
$block_result = $unifidata->block_sta($mac);
$getid_result = $unifidata->stat_client($mac);
$user_id = $getid_result[0]->_id;
$note_result = $unifidata->set_sta_note($user_id, $note);
$unblock_result = $unifidata->unblock_sta($mac);
/**
* then we authorize the device for the requested duration
*/
$auth_result = $unifidata->authorize_guest($mac, $duration);
/**
* provide feedback in json format
*/
return $auth_result;
hi i need to filter blocked user from unifi api browser. in controller i can brows them by
htpps://localhost:8443/manage/site/default/insights/knownclients/1/50/0/configured. i want to get this result from api. please help
You become your beer!!!!
Hi
I've just come across this, and wondered before I start playing with it, can we configure clients or more specifically enable and disable clients?
Reason... it would be simple to create am HTML scheduler for clients to enable and disable them if you have the basic functionality in the code here!
Has anyone installed this on a cloudkey? What would be the best way to install it in order to, for example, survive upgrades?
I am having issues with the restart_ap() function. It does not return any output or errors to the error log when I run that function and the AP does not reboot. Is there a specific way it has to be passed (no colons, etc.)? Controller version 5.4.11. Thanks
is there a way to list voucher details (up/down limits, quota, notes) of single use vouchers that are used already but not yet expired?
Hello guys, i cloned the project to /var/www/html, created the config.php with credentials and when im accessing it from web. i get this error:
Parse error: syntax error, unexpected '[' in /var/www/html/UniFi-API-browser/index.php on line 48
Hello,
thanks for your great API Browser. I am searching for a way to change the port status from a switch. specific the poe status. Is there a way to do it over the API Browser?
Regards, Valle
Hi malle-pietje,
First, thank you for that great api ! :D
I am currently trying your class.unifi.php but got a problem with the voucher creation, when I am creating a voucher my page is hanging just after the voucher creation
I am using the following code for a simple voucher creation :
$unifidata->create_voucher("1440", "1");
echo "test";
My voucher is goodly created in the controler, but page is hangins there and so test is never displayed, as the rest of my code is not executed too :(
(Just for info : got no problem to list vouchers, add site, list site, etc ... Just stuck on that voucher creation :( What did I miss ? )
I enabled debug mode and got this thing if it can help :
-----cURL INFO---------------
Array
(
[url] => https://192.168.70.128:8443/api/s/j2nu4f1a/cmd/hotspot
[content_type] => application/json;charset=UTF-8
[http_code] => 200
[header_size] => 178
[request_size] => 302
[filetime] => -1
[ssl_verify_result] => 18
[redirect_count] => 0
[total_time] => 0.023527
[namelookup_time] => 3.3E-5
[connect_time] => 0.000414
[pretransfer_time] => 0.020651
[size_upload] => 53
[size_download] => 70
[speed_download] => 2975
[speed_upload] => 2252
[download_content_length] => 70
[upload_content_length] => 53
[starttransfer_time] => 0.023495
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 192.168.70.128
[certinfo] => Array
(
)
[primary_port] => 8443
[local_ip] => 192.168.70.129
[local_port] => 48874
)
-----URL & PAYLOAD-----------
https://192.168.70.128:8443/api/s/j2nu4f1a/cmd/hotspot
json={"cmd":"create-voucher","expire":"1440","n":"1"}
-----RESPONSE----------------
Thanks in advance
Hi, i'm using change_wlan_password.php with debug on and this is the output:
-----------LOGIN-------------
[snip]
----------RESPONSE-----------
HTTP/1.1 200
vary: Origin
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: Access-Control-Allow-Origin,Access-Control-Allow-Credentials
Set-Cookie: unifises=63cXxTrlKXLA8BdpPwIvyhzAmcEYNmVu; Path=/; Secure; HttpOnly
Set-Cookie: csrf_token=XOt2sh4Vx3wfapaMVYKcp417hybbgzj9; Path=/; Secure
X-Frame-Options: DENY
Content-Type: application/json;charset=UTF-8
Content-Length: 41
Date: Mon, 11 Feb 2019 11:15:50 GMT
so login works, then:
---------cURL INFO-----------
Array
(
[url] => https://10.255.255.186:8443/api/s/default/rest/wlanconf/guest
[content_type] => application/json;charset=UTF-8
[http_code] => 400
[header_size] => 373
[request_size] => 285
[filetime] => -1
[ssl_verify_result] => 18
[redirect_count] => 0
[total_time] => 0.559868
[namelookup_time] => 1.7E-5
[connect_time] => 0.000545
[pretransfer_time] => 0.536978
[size_upload] => 42
[size_download] => 74
[speed_download] => 132
[speed_upload] => 75
[download_content_length] => 74
[upload_content_length] => 42
[starttransfer_time] => 0.559847
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 10.255.255.186
[certinfo] => Array
(
)
[primary_port] => 8443
[local_ip] => 10.255.255.125
[local_port] => 54288
[request_header] => PUT /api/s/default/rest/wlanconf/guest HTTP/1.1
Host: 10.255.255.186:8443
Accept: /
Cookie: unifises=63cXxTrlKXLA8BdpPwIvyhzAmcEYNmVu;csrf_token=XOt2sh4Vx3wfapaMVYKcp417hybbgzj9
Content-Type: application/json
Content-Length: 42
What is wrong?
Thanks
Where can i find an overview of all endpoints from the Unifi API?
Do you know if there is an endpoint for creating users for a specific period?
May I get Wifi AP CSI from the UniFi-API-browser? Thank you.
when create multi use voucher user can use unlimited devise connect by that voucher. i want to limit that to few devices. some forums i show that can do from api.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.