Reply
Highlighted
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218
Accepted Solution

PHP class to access the UniFi controller API: updates and discussion

[ Edited ]

I am creating this thread just to make it easier for (new) forum members to find this PHP class to access the UniFi controller API.

 

A PHP class, which is based on the work of @domwo and @fbagnol and the API as published by UBNT, and can be found here:

https://github.com/Art-of-WiFi/UniFi-API-client

 

EDIT: The class can now also be installed using composer:

https://packagist.org/packages/art-of-wifi/unifi-api-client

 

This class currently supports the following functions to get/put/post/delete data through the UniFi controller API:

    • login()
    • logout()
    • adopt_device()
    • authorize_guest()
    • block_sta()
    • count_alarms()
    • create_hotspotop()
    • create_network()
    • create_radius_account()
    • create_site()
    • create_usergroup()
    • create_user()
    • create_voucher()
    • create_wlan()
    • delete_network()
    • delete_radius_account()
    • delete_site()
    • delete_usergroup()
    • delete_wlan()
    • disable_ap()
    • edit_client_fixedip()
    • edit_firewallgroup()
    • edit_usergroup()
    • extend_guest_validity()
    • forget_sta() (supported on controller version 5.9.X and higher)
    • led_override()
    • list_admins()
    • list_alarms()
    • list_aps() (deprecated in favor of list_device() but still available as alias)
    • list_backups()
    • list_clients()
    • list_current_channels()
    • list_dashboard()
    • list_devices()
    • list_dpi_stats()
    • list_dynamicdns()
    • list_events()
    • list_extension()
    • list_guests()
    • list_health()
    • list_hotspotop()
    • list_networkconf()
    • list_portconf()
    • list_portforward_stats()
    • list_portforwarding()
    • list_radius_accounts() (supported on controller version 5.5.19 and higher)
    • list_radius_profiles()
    • list_rogueaps()
    • list_self()
    • list_settings()
    • list_sites()
    • list_tags() (supported on controller version 5.5.19 and higher)
    • list_usergroups()
    • list_users()
    • list_wlan_groups()
    • list_wlanconf()
    • locate_ap()
    • reconnect_sta()
    • rename_ap()
    • restart_ap()
    • revoke_voucher()
    • set_ap_radiosettings()
    • set_guestlogin_settings()
    • set_ips_settings_base()  (supported with controllers 5.9.10 and higher)
    • set_locate_ap() (deprecated but still available as alias)
    • set_networksettings_base()
    • set_radius_account_base()
    • set_sta_name()
    • set_sta_note()
    • set_usergroup()
    • set_wlan_mac_filter()
    • set_wlansettings()
    • set_wlansettings_base()
    • site_leds()
    • site_ledsoff() (deprecated but still available as alias)
    • site_ledson() (deprecated but still available as alias)
    • spectrum_scan()
    • spectrum_scan_state()
    • stat_allusers()
    • stat_auths()
    • stat_client()
    • stat_5minutes_aps() (supported on controller version 5.5.X and higher)
    • stat_hourly_aps()
    • stat_daily_aps()
    • stat_5minutes_gateway() (supported on controller version 5.7.X and higher)
    • stat_hourly_gateway() (supported on controller version 5.7.X and higher)
    • stat_daily_gateway() (supported on controller version 5.7.X and higher)
    • stat_5minutes_user() (supported on controller version 5.7.X and higher)
    • stat_hourly_user() (supported on controller version 5.7.X and higher)
    • stat_daily_user() (supported on controller version 5.7.X and higher)
    • stat_5minutes_site() (supported on controller version 5.5.X and higher)
    • stat_hourly_site()
    • stat_daily_site()
    • stat_payment()
    • stat_sessions()
    • stat_sites()
    • stat_speedtest_results()
    • stat_ips_events() (supported with controllers 5.9.10 and higher)
    • stat_sta_sessions_latest()
    • stat_sysinfo()
    • stat_voucher()
    • unauthorize_guest()
    • unblock_sta()
    • unset_locate_ap() (deprecated but still available as alias)
    • upgrade_device()
    • upgrade_device_external()
    • start_rolling_upgrade()
    • cancel_rolling_upgrade()
    • invite_admin()
    • revoke_admin()

    Internal functions, getters/setters:

    • set_debug()
    • set_site()
    • set_site()
    • get_site()
    • get_cookie() (renamed from getcookie())
    • get_last_results_raw()
    • get_last_error_message()

Please refer to the source code for more details on each function/method and it's parameters.

 

For example usage of this class you can follow the instructions in the main README, and also go through the files in the example directory here:

https://github.com/Art-of-WiFi/UniFi-API-client/tree/master/examples

 

Important Disclaimer:

many of these functions are not officially supported by UBNT and as such, may not be supported in future versions of the API.

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.

Accepted Solutions
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion

[ Edited ]

Today I pushed version 1.0.1 of the API Browser and PHP API class to github:

 

- added list_usergroups to API class and to API browser

enjoy!Icon Biggrin

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.

View solution in original post


All Replies
SuperUser
Posts: 20,370
Registered: ‎09-17-2013
Kudos: 5109
Solutions: 1455

Re: PHP class to access the Unifi controller API

when I first saw this title, my immediate reaction was "paging @slooffmaster" ... and then I saw you wrote it 

 

Nice list of stuff that it does there Cheers2

Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API

Thanks!Icon Wink

 

There's still some work I think should be done to further document each API endpoint and it's associated function. If anyone is willing to help out, that will be more than welcomeIcon Biggrin

 

I'm considering setting up a seperate github repository for this PHP class where we can use the Wiki pages to set up the API documentation. Let me know whether that's a good idea.

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion

[ Edited ]

After looking at the list of functions, I decided to update the class to group some of the functions in a more logical manner. No functional changes were made.

https://github.com/malle-pietje/Unifi-API-browser/blob/master/phpapi/class.unifi.php

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
Established Member
Posts: 1,227
Registered: ‎07-17-2013
Kudos: 822
Solutions: 83
Contributions: 1

Re: PHP class to access the Unifi controller API: updates and discussion

this is awesome @slooffmaster !!! Just a small suggestion, add some version number inside the class, so people can track easily if they are updated or not.

Solutti Tecnologia Ltda - Goiânia/Goiás/Brazil
www.solutti.com.br / www.wifiquefunciona.com.br
Ubiquiti Enterprise Wireless Admin (UEWA) certified
did my answer helped you ? if yes, i would love your kudos on it Man Happy
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion

@leonardogyn That's a good suggestion. I'll do that with the next update.

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion

Today I published a minor update which is tagged as v1.0 (upon special request from @leonardogyn):

release of version 1.0
- added description to index.php
- added version numbering to index.php and class.unifi.php
- code and comments cleanup

https://github.com/malle-pietje/Unifi-API-browser/blob/master/phpapi/class.unifi.php

 

As always, enjoy!

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion

[ Edited ]

Today I pushed version 1.0.1 of the API Browser and PHP API class to github:

 

- added list_usergroups to API class and to API browser

enjoy!Icon Biggrin

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
Member
Posts: 253
Registered: ‎07-19-2015
Kudos: 8
Solutions: 2

Re: PHP class to access the Unifi controller API

@slooffmaster - good idea!

Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion

(cross posting for those only monitoring this thread)

Today I pushed an update to github for both the API browser and the controller API client class:

- added: add_site function
- fixed: when used on mobile screens, the button to display the top navbar was empty
- cleanup: removed unnecessary code and improved the comments for the functions/methods

Thanks to @patrickclover for supporting this effort.

I will be looking forward to your input and feedback. Enjoy!

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion

(cross posting for those only monitoring this thread)

I just released v1.0.2 of the API browser and the API client class:

 

version 1.0.2
- added: optional parameter <mac> to list_aps function in class.unifi.php
- fixes: minor code cleanup (enhanced comments and applied more consistent variable naming)

Thanks to @Gorbushka for reminding me of the list_aps enhancementIcon Wink

 

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
New Member
Posts: 2
Registered: ‎05-10-2016

Re: PHP class to access the Unifi controller API: updates and discussion

Thank you for the great work! Helped me a lot.

I needed a function to move a user to another group and with the help of your functions I added the following to class.unifi.php

It works fine to me and maybe you want to add it to your class.

Thx

 

   /*
   move user to another group
   return true on success
   required parameter <user_id>
   required parameter <group_id>
   */
   public function set_usergroup($user_id, $group_id) {
      if (!$this->is_loggedin) return false;
      $return           = false;
      $json             = json_encode(array('usergroup_id' => $group_id, "noted" => false));
	  $content_decoded  = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/user/'.$user_id,'json='.$json));
      if (isset($content_decoded->meta->rc)) {
         if ($content_decoded->meta->rc == 'ok') {
            $return = true;
         }
      }
      return $return;
   }
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion


AndyGSE wrote:

Thank you for the great work! Helped me a lot.

I needed a function to move a user to another group and with the help of your functions I added the following to class.unifi.php

It works fine to me and maybe you want to add it to your class.

Thx

 

   /*
   move user to another group
   return true on success
   required parameter <user_id>
   required parameter <group_id>
   */
   public function set_usergroup($user_id, $group_id) {
      if (!$this->is_loggedin) return false;
      $return           = false;
      $json             = json_encode(array('usergroup_id' => $group_id, "noted" => false));
	  $content_decoded  = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/user/'.$user_id,'json='.$json));
      if (isset($content_decoded->meta->rc)) {
         if ($content_decoded->meta->rc == 'ok') {
            $return = true;
         }
      }
      return $return;
   }

@AndyGSE Welcome to these forums. Thanks for the feedback and contribution! I will add the method/function with the next release. Icon Biggrin

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
New Member
Posts: 2
Registered: ‎05-10-2016

Re: PHP class to access the Unifi controller API: updates and discussion

@slooffmaster, great, thank you 

maybe another userfull function

 

   /*
   gets data of one specific client
   returns an array with the client information
   required parameter <mac>
   */
   public function stat_client($client_mac) {
      if (!$this->is_loggedin) return false;
      $return           = false;
	  $content_decoded  = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/user/'.$client_mac));
      if (isset($content_decoded->meta->rc)) {
         if ($content_decoded->meta->rc == 'ok') {
            if (is_array($content_decoded->data)) {
               foreach ($content_decoded->data as $client) {
                  $return[]= $client;
               }
            }
         }
      }
      return $return;
   }

of course, the same information can also be retrieved with your list_clients(), after searching the array for the specific client

I just thought it's a little easier if there isn't such a flood of data Smiley Happy

 

Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion


AndyGSE wrote:

@slooffmaster, great, thank you 

maybe another userfull function

 

   /*
   gets data of one specific client
   returns an array with the client information
   required parameter <mac>
   */
   public function stat_client($client_mac) {
      if (!$this->is_loggedin) return false;
      $return           = false;
	  $content_decoded  = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/user/'.$client_mac));
      if (isset($content_decoded->meta->rc)) {
         if ($content_decoded->meta->rc == 'ok') {
            if (is_array($content_decoded->data)) {
               foreach ($content_decoded->data as $client) {
                  $return[]= $client;
               }
            }
         }
      }
      return $return;
   }

of course, the same information can also be retrieved with your list_clients(), after searching the array for the specific client

I just thought it's a little easier if there isn't such a flood of data Smiley Happy

 


Thanks, indeed useful, especially where you have a large number of clients and picking a single device is all you need. This also helps lower the memory/CPU load of the PHP processes and reduce data transfer times. I will also add this in the next release.

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion

I thought I might as well release the updated version of the API client to github:

API client version 1.0.3: added new methods/functions and other minor updates to class.unifi.php

- added set_usergroup method/function
- bumped API client to version 1.0.3
- added stat_client method/function
  (both suggested by AndyGSE)
- added optional parameter "within" to list_guests method/function
- other minor code clean up

Thanks go to @AndyGSE for the suggestions!

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
Established Member
Posts: 1,227
Registered: ‎07-17-2013
Kudos: 822
Solutions: 83
Contributions: 1

Re: PHP class to access the Unifi controller API: updates and discussion

hi @slooffmaster ... i'm writing some scripts to automate some blocking of devices, based on their mac address, and that can be done easily with the 'block_sta' function, no problem on that.

 

however i'd like, at the blocking moment, to write something on the device 'note' field, and that's not possible through the API.

 

any chance of adding this feature, of changing some device 'note' with the API ??

 

Thanks !!

Solutti Tecnologia Ltda - Goiânia/Goiás/Brazil
www.solutti.com.br / www.wifiquefunciona.com.br
Ubiquiti Enterprise Wireless Admin (UEWA) certified
did my answer helped you ? if yes, i would love your kudos on it Man Happy
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion


leonardogyn wrote:

hi @slooffmaster ... i'm writing some scripts to automate some blocking of devices, based on their mac address, and that can be done easily with the 'block_sta' function, no problem on that.

 

however i'd like, at the blocking moment, to write something on the device 'note' field, and that's not possible through the API.

 

any chance of adding this feature, of changing some device 'note' with the API ??

 

Thanks !!


@leonardogyn adding a note to a device is only possible once it has connected to the network, so not if you are being "pro-active" in blocking a device. If this makes sense to you, I will see whether I can free up some time and add it to the API client.

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
Established Member
Posts: 1,227
Registered: ‎07-17-2013
Kudos: 822
Solutions: 83
Contributions: 1

Re: PHP class to access the Unifi controller API: updates and discussion

ouch .... no, it makes no sense at all. It seems i'll have to manually write on the MongoDB to acchieve that Man Sad

 

well ... anyway, thanks for your attention Man Happy

Solutti Tecnologia Ltda - Goiânia/Goiás/Brazil
www.solutti.com.br / www.wifiquefunciona.com.br
Ubiquiti Enterprise Wireless Admin (UEWA) certified
did my answer helped you ? if yes, i would love your kudos on it Man Happy
Veteran Member
Posts: 4,661
Registered: ‎06-13-2015
Kudos: 1263
Solutions: 218

Re: PHP class to access the Unifi controller API: updates and discussion

[ Edited ]

leonardogyn wrote:

ouch .... no, it makes no sense at all. It seems i'll have to manually write on the MongoDB to acchieve that Man Sad

 

well ... anyway, thanks for your attention Man Happy


You're welcome. Yeah, the device must be in the user collection to add a note (you need to use the user id as the key). While you can authorise/un-authorise/block devices which are not in the user collection, you cannot add a note through the API. Would be keen to know how that can easily be achieved directly in the MongoDB.

Art of WiFi
Check out our UniFi API browser tool on GitHub. The PHP API client which it uses, can be found here on GitHub.
Our UniFi Device Search tool can be found here, and our Captive Portal solutions for UniFi can be found here.
Reply