Link to home
Start Free TrialLog in
Avatar of Jesper Andersen
Jesper Andersen

asked on

Encode json with cURL to an API help

Hello Guys,
I am having a little problem with a Unifi Controller that I am trying to communicate with through PHP.
The API documentation for the controller is located here: http://dl.ubnt.com/unifi/4.6.0/unifi_sh_api

And the function I'm trying to use is to authenticate a user.
# cmd/stamgr
# authorize-guest(mac, minutes, [up=kbps, down=kbps, bytes=MB])
unifi_authorize_guest() {
    if [ $# -lt 2 ] ; then
        echo "Usage: $0 <mac> <minutes> [up=kbps] [down=kbps] [bytes=MB] [ap_mac=mac]"
        return
    fi

    mac=$1
    minutes=$2
    other_payload=`named_args_to_payload "$@"`

    ${curl_cmd} --data "json={'cmd':'authorize-guest', 'mac':'${mac}', 'minutes':${minutes}${other_payload}}" $baseurl/api/s/$site/cmd/stamgr
}

I got it working with the first 3 commands, but I cant get upload, download and quota to work.
This is how I do it now:

$data = json_encode(array(
                                    'cmd'=>'authorize-guest',
                                    'mac'=>$id,
                                    'minutes'=>$minutes));

I hoped that I could just do it like this, but it doesnt work :-( Am I dont know about this kind of programmering.

$data = json_encode(array(
                              'cmd'=>'authorize-guest',
                              'mac'=>$id,
                              'minutes'=>$minutes,
                              'up'=>$upload,
                              'down'=>$download));

So I hope some of you guys can help me out here, and tell me how the string to the controller should look like to get it working.

Thanks,
Avatar of Rob
Rob
Flag of Australia image

The code you've posted seems to be a mix of shell scripting and php.  Are you trying to convert the API to php or just trying to utilise it?
ASKER CERTIFIED SOLUTION
Avatar of Jesper Andersen
Jesper Andersen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jesper Andersen
Jesper Andersen

ASKER

I found a solution to the problem myself...