PHP Example

Hi,

I’m restricted at the moment to demo retrieving AirVantage data using PHP. Does anybody have examples?

I’ve successfully used the av shell script to interact with AirVantage but converting the curl commands to PHP and JSON is proving slower than I’d like, not having done PHP before. I have managed to retrieve the access_token but subsequent curl queries are failing.

E.g. This request gets an error:

https://eu.airvantage.net/api/v1/systems?access_token=77b32940-8624-457a-b9a7-936fa251cca2

Error:

getAV({"error":"unauthorized","error_description":"Full authentication is required to access this resource"})

My curl function:

function getAV($url) {
        error_log("getAV(".$url.")\n",3,"/tmp/av.log");
        $curl = curl_init($url);   
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');

        $postData = "";

        //This is needed to properly form post the credentials object
        foreach($params as $k => $v)
        {  
           $postData .= $k . '='.urlencode($v).'&';
                echo $postData;
        }

        $postData = rtrim($postData, '&');

        curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);

        $data = curl_exec($curl);

        error_log("getAV(".$data.")\n",3,"/tmp/av.log");
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

// evaluate for success response
        if ($status != 200) {
          throw new Exception("Error: call to URL $endpoint failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl) . "\n");
        }
        curl_close($curl);  
        $json = json_decode($data, true);

        return $json;
}

Browser output:

access_token => 77b32940-8624-457a-b9a7-936fa251cca2
token_type => Bearer
refresh_token => 03841f37-4e9a-486f-913a-d8913853b6ab
expires_in => 12785
access_token=77b32940-8624-457a-b9a7-936fa251cca2&

Thanks,
Steve

Hi Steve,

First, yo ucan find more information about how to use API in this tutorial: doc.airvantage.net/av/howto/clo … arted_api/

Another tutorial focused on PH is available here: source.sierrawireless.com/resour … hp_sample/

Second, you may use postman or any Rest client to test your API call like described in this tutorial, it will be easier.

Your request is correct. Do you use the correct HTTP verb (GET) ?
This two lines seems root cause:

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');

You have to change to something like

curl_setopt($curl, CURLOPT_GET, true);
curl_setopt($curl, CURLOPT_HEADER,'Content-Type: application/json');

Regards,
Robert

Hi,

I’l still getting the “unauthorized” even with the json mod. I will review that area of the code more. My existing code did retrieve the access_token ok but subsequent calls were failing.

I did register with the AWS service but the link mentioned in the tutorial is no longer valid, so when I tried:

s3-eu-west-1.amazonaws.com/elas … e.template

I got error:

<Error>
<Code>AllAccessDisabled</Code>
<Message>All access to this object has been disabled</Message>
<RequestId>A5AAAB13ADCCEF07</RequestId>
<HostId>
cyM2xRFS8yi7WbrW+uQlKkkCahFnY5yaYkVipxjfpnxUWvCNVKeig50BC2S5py6SKVMLaLIEM8o=
</HostId>
</Error>

Regards,
Steve

Hi Steve,

https://s3-eu-west-1.amazonaws.com/elasticbeanstalk-eu-west-1-281626357614/Linux_Apache_PHP_AV4_Sample.template

is just an example. You have to use the url which go on your AWS account and the stack you created using the previous step of this tutorial.
elasticbeanstalk-eu-west-1-281626357614: last number are different
Linux_Apache_PHP_AV4_Sample: if you followed the tutorial, you may have the same stack name.

If you get Unauthorized, get the url link is correct (http verb, no / at this end, typo, …).

Let me know if you have still an issue.

Regards,
Robert

Hi Robert,

I eventually used file_get_contents(). Sierra Wireless also sent me the PHP code from the AWS server for reference but I’d already solved my initial issue. I tried various PHP curl techniques but none worked for me.

My proof of concept code lists some device properties:

<?php

$HOST = "https://eu.airvantage.net";
$endpoint = $HOST."/api/oauth/token";

$json = file_get_contents('https://eu.airvantage.net/api/oauth/token?grant_type=password&username=ABC@DDDDD.CCCC&password=PPPPPP&client_id=IIIIIIIIIIIIIIIIIIIIIIIIIIIII&client_secret=SSSSSSSSSSSSSSSSSSSSSSSS');
$obj = json_decode($json);

$url='https://eu.airvantage.net/api/v1/systems?access_token='.$obj->access_token;
//echo $url;
$json = file_get_contents($url);
$o = json_decode($json);
//echo $json;
echo "Your Devices (name, UID & first label):<br>";

echo "<table width=60% border=1><tr><th>Device</th><th>Uid</th><th>Owner</th></tr>";
for ($i = 0; $i < $o->count; $i++) {
        echo "<tr>";
        echo "<td>". $o->items[$i]->type . "</td><td>" . $o->items[$i]->uid . "</td><td>". $o->items[$i]->labels[0] ."</td>";
        echo "</tr>";
}
echo "</table>";
?>

Thanks,
Steve

Hi Steve,

I reworked the PHP Sample code to set it cleaner. I will update the doc.airvantage.net/av/howto/cloud/api_examples/ page in November.

I send you the php file which works for me (after enabled session mgt in php.ini file) in private.

Robert