login with balena-cli and php

Hi,

i’ve made the balena-cli masterclass, installed balena-cli on my server and test commands into shell with success.

I’m trying to control balena-cli with a php script.

 $output = shell_exec('balena devices');
 echo "<pre>$output</pre>";

the ouput is currently empty. i assume it’s because php have not the right authentication (i was using api key method with shell).

I’m searching for examples of balena-cli authentication, but i didn’t manage to find any. How can i authenticate to balena-cli with php ?

thank you,

FX

Hi,

I think your best bet would be to use balena login with an API key.
You can generate your API keys on the preferences page of balena cloud.

Try something like this:

<?php
   $token="<api key you generated in balena cloud preferences page>";
   
   // start a new session with our token
   shell_exec("balena login --token $token");

   // check devices
   $output = shell_exec("balena devices");
   echo "<pre>$output</pre>";
?>
1 Like

Hi, thanks for your return.

I tried that, but the page prints the same as before.
<pre></pre>

i would like to have a more verbose mode to know that’s issuing :'D

Hi,

I think this may be more an issue of setting up php on your server vs getting balena-cli to work.

For testing the balena-cli part, it’s easier to first run your scripts on the command line (ex. running php test.php).
This will show you the actual output of your commands.

When you have that part working, you can look into setting up your server and how to include the scripts in your pages.

on the server shell, the balena devices prints all devices as awaited. (balena login has been executed some days ago).
what do you mean by setting up the server ?

thank you !

Hi,

I am assuming your issue relates to your webserver (which hosts the file) not processing the PHP properly when requesting the file.
If you simply open the file in your browser without actually running a server, this will also be the case.

I can recommend reading through the PHP getting started guide if this is new.
If you want a simple setup for developing PHP sites, you may want to try XAMPP.

It’s difficult to help without more details about the setup in which you’re trying this.

i’ve built a tests php page to test several dev cases, with a switch of actions in the query string.
the page is run by the server, i assume.

but i see your point.

i’ve tried this code with success :

php -r '$_GET["action"]="balena"; require_once("tests.php");'

the direct access to the page url answers 200 too.
https://lab.cleandrop.fr/tests.php?action=balena

so i don’t understand why it is executed by command line and not executed through the php page rendering.