Reply
Highlighted
New Member
Posts: 9
Registered: ‎10-01-2013
Kudos: 11

little php class for unifi api

[ Edited ]

Hello,

here is a little php class for use in a custom portal based on the unifi api for use with version 3.x

it can <un>authorzie clients , create/list vouchers , list guests , block/unblock client

usage excample to authorize a mac for 60 minutes

$unifi=new unifiapi("username","password","https://<ip_of_unifi>:84443");

$unifi->login();

$unifi->authorize_guest("mac adress",60)

 

 

Attachment
Regular Member
Posts: 740
Registered: ‎10-23-2013
Kudos: 324
Solutions: 7

Re: little php class for unifi api

Thanks was looking for such a class Man Wink

Regular Member
Posts: 360
Registered: ‎06-21-2010
Kudos: 21
Solutions: 2

Re: little php class for unifi api

Thanks a lot for this. Really usefull.

 

Member
Posts: 165
Registered: ‎06-06-2013
Kudos: 8
Solutions: 8

Re: little php class for unifi api

Not to sound dumb or anything, could you explain a little more what this php file does?

 

Thanks,

Regular Member
Posts: 431
Registered: ‎09-23-2013
Kudos: 339
Solutions: 9

Re: little php class for unifi api

It looks like this is for it you want to use your php site for authorization with unifi.

Pretty cool , thanks for posting Man Happy

 

Regular Member
Posts: 438
Registered: ‎05-29-2014
Kudos: 87
Solutions: 7

Re: little php class for unifi api

This is fantastic, helps a lot.  I was thinking of using the same against the 2.4.6 and trying to gather what the differences would be.  Is there anything different in the calls besides the 'Site' ?

New Member
Posts: 40
Registered: ‎08-19-2014
Kudos: 11
Solutions: 1

Re: little php class for unifi api

Thanks for this, going to be excellent for our latest development Man Happy

Emerging Member
Posts: 102
Registered: ‎03-20-2014
Kudos: 156
Solutions: 2

Re: little php class for unifi api

[ Edited ]

The login function in the unifiapi class need a tweak for the new 4.2.0 controller.

 

Approx Lines 33&34:

from

curl_setopt($ch, CURLOPT_URL, $this->baseurl."/login");
curl_setopt($ch, CURLOPT_POSTFIELDS,"login=login&username=".$this->user."&password=".$this->password);

to

curl_setopt($ch, CURLOPT_URL, $this->baseurl."/api/login");

curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode(array("username" => $this->user, "password" => $this->password)).":");

 

and 

 

Approx line 45:

from

if (strlen($body) < 10) {

to

if (!empty($body)) {

Regular Member
Posts: 360
Registered: ‎06-21-2010
Kudos: 21
Solutions: 2

Re: little php class for unifi api

[ Edited ]

Upgrade to 3.2.7 from 3.2.5 broke the PHP API.

 

I did the changes mentioned in previous post but it doesn´t work

Emerging Member
Posts: 102
Registered: ‎03-20-2014
Kudos: 156
Solutions: 2

Re: little php class for unifi api


lisandrogalup wrote:

Upgrade to 3.2.7 from 3.2.5 broke the PHP API.

 

I did the changes mentioned in previous post but it doesn´t work


Please note: "The login function in the unifiapi class need a tweak for the new 4.2.0 controller." not 3.2.7!

 

I have not used 3.2.7 and I can only presume that its the samebase as 3.2.5 which worked with the original API.

Regular Member
Posts: 360
Registered: ‎06-21-2010
Kudos: 21
Solutions: 2

Re: little php class for unifi api

The only difference between 3.2.5 and 3.2.7 API, as I can see here http://dl.ubnt.com/unifi/3.2.7/unifi_sh_api and here http://dl.ubnt.com/unifi/3.2.5/unifi_sh_api is this line:

 

3.2.5:

curl_cmd="curl --sslv3 --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure "

 

3.2.7:

curl_cmd="curl --tlsv1 --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure "

 

I tried adding this but still doesn´t work

 

 

curl_setopt($ch, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_TLSv1'); //also tested with TLSv1_1 and 1_2

 

 

Regular Member
Posts: 360
Registered: ‎06-21-2010
Kudos: 21
Solutions: 2

Re: little php class for unifi api

[SOLVED]

 

I was adding this line, in the wrong function:

curl_setopt($ch, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_TLSv1'); 

 

The only you have to do is change 

 

private function get_curl_obj() {
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_POST, TRUE);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 //curl_setopt($ch, CURLOPT_SSLVERSION, 3); //YOU CAN DELETE THIS
 curl_setopt($ch, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_TLSv1'); //ADD THIS	   
 curl_setopt($ch , CURLOPT_RETURNTRANSFER, true);
 if ($this->debug == true) {
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);    
 }
 if ($this->cookies != "") {
    curl_setopt($ch, CURLOPT_COOKIE,  $this->cookies);
 }
 return $ch;
}   

 

Voila! Works again

Emerging Member
Posts: 102
Registered: ‎03-20-2014
Kudos: 156
Solutions: 2

Re: little php class for unifi api


lisandrogalup wrote:

[SOLVED]

 

I was adding this line, in the wrong function:

curl_setopt($ch, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_TLSv1'); 

 

The only you have to do is change 

 

private function get_curl_obj() {
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_POST, TRUE);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 //curl_setopt($ch, CURLOPT_SSLVERSION, 3); //YOU CAN DELETE THIS
 curl_setopt($ch, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_TLSv1'); //ADD THIS	   
 curl_setopt($ch , CURLOPT_RETURNTRANSFER, true);
 if ($this->debug == true) {
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);    
 }
 if ($this->cookies != "") {
    curl_setopt($ch, CURLOPT_COOKIE,  $this->cookies);
 }
 return $ch;
}   

 

Voila! Works again


In this function

 

simply change the line from

curl_setopt($ch, CURLOPT_SSLVERSION, 3);

to

curl_setopt($ch, CURLOPT_SSLVERSION, 6);

As SSLv3 has been replace with TLS1.2

 

I'll redownload the original files and update them and post them here.

Emerging Member
Posts: 102
Registered: ‎03-20-2014
Kudos: 156
Solutions: 2

Re: little php class for unifi api

[ Edited ]

This new php code has been slightly changed to acomodate controller differences.

 

I haven't tested it on a V3.x.x controller as I don't run one but it should work; it's also compatible with the previous copy, so you should just be able to drop it in over the top and get the same results.

 

There's an extra option in the construct, where by you just add your controller version at the end, it'll then change the login curl and ssl version. i.e.

 

$unifi=new unifiapi("username","password","https://0.0.0.0:8443","default","4.6.0");
$unifi->login();
$unifi->authorize_guest("mac adress",60)
Attachment
New Member
Posts: 16
Registered: ‎03-05-2015

Re: little php class for unifi api

Thank you very much for the code, but I got a little problem with the authorization.

I'm using controller version 3.2.10, and my portal is on an external webserver.

Till today I used a code I found somewhere else on the internet and it always worked quite well but even I can say that it's not a very clean code. I tried your class and it runs without an error but my guests don't get authorized when I'm running the code. 

 

$unifi=new unifiapi();
$unifi->login();
$unifi->authorize_guest($mac,$minutes);

 

In your classes you defined all the public variables so I don't have to give them the parameters (right?).

Can somebody tell me what I'm doing wrong?

 

 

Here's the Code which works:

 

function sendAuthorization($id, $minutes, $url) {
global $unifiServer;
global $unifiUser;
global $unifiPass;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
$unifiServer = "https://x.x.x.x:8443";
$unifiUser = "user";
$unifiPass = "pw";
///////////////////////////////////////////////////////////////////////////////////////////////////////////

// Start Curl for login
$ch = curl_init();
// We are posting data
curl_setopt($ch, CURLOPT_POST, TRUE);
// Set up cookies
$cookie_file = "/tmp/unifi_cookie";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
// Allow Self Signed Certs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// Force SSL3 only
//curl_setopt($ch, CURLOPT_SSLVERSION, 3);
// Login to the UniFi controller
curl_setopt($ch, CURLOPT_URL, "$unifiServer/login");
curl_setopt($ch, CURLOPT_POSTFIELDS,"login=login&username=$unifiUser&password=$unifiPass");
curl_exec ($ch);
curl_close ($ch);

// Send user to authorize and the time allowed
$data = json_encode(array(
'cmd'=>'authorize-guest',
'mac'=>$id,
'minutes'=>$minutes));
$ch = curl_init();
// We are posting data
curl_setopt($ch, CURLOPT_POST, TRUE);
// Set up cookies
$cookie_file = "/tmp/unifi_cookie";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
// Allow Self Signed Certs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// Force SSL3 only
//curl_setopt($ch, CURLOPT_SSLVERSION, 3);
// Make the API Call
curl_setopt($ch, CURLOPT_URL, $unifiServer.'/api/s/site/cmd/stamgr');

curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.$data);
curl_exec ($ch);
curl_close ($ch);

// Logout of the connection
$ch = curl_init();
// We are posting data
curl_setopt($ch, CURLOPT_POST, TRUE);
// Set up cookies
$cookie_file = "/tmp/unifi_cookie";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
// Allow Self Signed Certs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// Force SSL3 only
//curl_setopt($ch, CURLOPT_SSLVERSION, 1);
// Make the API Call
curl_setopt($ch, CURLOPT_URL, $unifiServer.'/logout');
curl_exec ($ch);
curl_close ($ch);
//sleep(8); // Small sleep to allow controller time to authorize
exit(header('Location: ' . $url));//$_SESSION['url']);
}

 

Emerging Member
Posts: 102
Registered: ‎03-20-2014
Kudos: 156
Solutions: 2

Re: little php class for unifi api


Kisum wrote:

Thank you very much for the code, but I got a little problem with the authorization.

I'm using controller version 3.2.10, and my portal is on an external webserver.

Till today I used a code I found somewhere else on the internet and it always worked quite well but even I can say that it's not a very clean code. I tried your class and it runs without an error but my guests don't get authorized when I'm running the code. 

 

$unifi=new unifiapi();
$unifi->login();
$unifi->authorize_guest($mac,$minutes);

 

In your classes you defined all the public variables so I don't have to give them the parameters (right?).

Can somebody tell me what I'm doing wrong?

 


I'm not amazing at OOP so I may be wrong but IIRC Public only means the variables are editable/accessible form outside of the class so I believe yes you need to include the parameters.

 

I've only edited the original source that the OP posted to try and make it work with Controller V4.

If things don't work as expected, try making the class think you're using controller v4 as some changes from v4 maybe have been merged into v3 and I don't run v3 to try it with.

New Member
Posts: 16
Registered: ‎03-05-2015

Re: little php class for unifi api

But in the class he defines the parameters. You CAN include the parameters, but I don't think its necessary. If you include the parameters it would just override the default values of the variables. 

 

Emerging Member
Posts: 102
Registered: ‎03-20-2014
Kudos: 156
Solutions: 2

Re: little php class for unifi api

[ Edited ]

A quick update, I've come accross an issue with PHP5.6 not negotiating the SSL correctly.

 

Presumably newer versions of SSL/TLS can negotiate a secure conenction better so it's safe to remove lines 366-370

 

if($this->controller >= 4) {
		curl_setopt($ch, CURLOPT_SSLVERSION, 6);
} else {
		curl_setopt($ch, CURLOPT_SSLVERSION, 3);
}

If you have issues after that, try editing line 367

from

curl_setopt($ch, CURLOPT_SSLVERSION, 6);

to

curl_setopt($ch, CURLOPT_SSLVERSION, 1);

This is assuming you're using the version I've updated - here 

New Member
Posts: 6
Registered: ‎08-15-2012
Kudos: 2

Re: little php class for unifi api

Hi, 

 

Very nice class.

 

I add 2 methods : 

  • list_aps => list all APs
  • restart_ap => reboot an AP (give it mac address in parameter)

Can I put this class on github ?

 

It will be better for the next changes.

 

Regards.

FredB

Attachment
New Member
Posts: 6
Registered: ‎08-15-2012
Kudos: 2

Re: little php class for unifi api

No answer, I put on github.

 

https://github.com/fbagnol/class.unifi.php

Reply