How can I get the extension information in php

I am tinkering with a php script that is using this environment.

 Load FreePBX environment
require_once('/etc/freepbx.conf');
$FreePBX = FreePBX::Create();

For the moment, I just want to get the extension’s display name, but may expand to other fields if it proves useful.

Is there a list of the functions anyplace for what I can pull in? Or do I need to poke in the code to find it?

Nothing documented outside of the code. I usually start by guessing which module has the functions I want and grep for “public function”. Most are named with get in the name:

<?php
include '/etc/freepbx.conf';
$FreePBX = FreePBX::Create();

$devices=$FreePBX->Core->getAllDevicesByType();
$device=$FreePBX->Core->getDevice("6008");
$user=$FreePBX->Core->getUser("6008");

Well it did not like something.
FreePBX 15 or 16

<?php
  // test.php
  // Load FreePBX bootstrap environment
  require_once('/etc/freepbx.conf');
  $FreePBX = FreePBX::Create();

  $user=$FreePBX->Core->getUser("213");
  echo "<br />BEGIN var_dump <br />\r\n<pre>\r\n";
  var_dump($user);
  echo "\r\n</pre><br />\r\nEND var_dump\r\n";

?>

I’m testing using the CLI on 16, not via browser. I was just reusing code from an old script of mine on github.

My PHP skills are not good. This one will get a list of all the extension numbers and names in a JSON Array

<?php

include '/etc/freepbx.conf';
global $db;

$sql = "select extension,name from users";

echo json_encode($db->getAll($sql, DB_FETCHMODE_ASSOC), JSON_PRETTY_PRINT);

?>

I do that elsewhere in a different script. I was trying to get in to using the built in functions.

With that simple PHP script you’re not bringing in your authenticated session. Add at the top before you include /etc/freepbx.conf:

// this is from config.php
if (!isset($_SESSION)) {
        //start a session if we need one
        $ss = @session_start();
        if(!$ss){
                session_regenerate_id(true); // replace the Session ID
                session_start();
        }
}

Or bypass auth altogether with:

$bootstrap_settings['freepbx_auth'] = false;

before requiring /etc/freepbx.conf. But of course, disclaimer: bypass security at your own peril.

I wasn’t looking for security yet. I will tweak and let you know.

  1. Never use include_once. It is bad practice and frankly makes doves cry.
  2. When possible always cheat :slight_smile:
<?php
include '/etc/freepbx.conf';

$core = FreePBX::Core();

/** Here we cheat **/
$extensions = $core->bulkhandlerExport('extensions');

foreach($extensions as $extension => $data){
      echo $data['name'] . PHP_EOL;
}
  1. In a pure php file never use a closing ?>, like include_once it will cause weirdness and debugging issues…

I’m not a PHP dev. I copy/edit/modify. Thanks for the tip.

In this case, I am already in a loop and know the exact extension I want. Are you saying I should pull this once and then jsut go down the object then?
maybe:

$extensions = $core->bulkhandlerExport('extensions');
echo  $extensions[$extvalue_from_my_loop]->['name']; 

or something like that?

We all start with examples and php makes bad habits really easy. I will always do my best to give good code examples. Note my old code may also have some of these bad habits. We all learn and grow hopefully.

If you can afford the data structure memory wise it is always useful to put it in memory. Without a full scope of your end goal I couldn’t say. Premature optimization is the devil so ideally make it work first then clean up.

If you want to hold a smaller data structure you can use something like array_walk to reduce the size of the data structure only keeping the keys you want

Dropped that in my test.php and that is a LOT of data for each extension. I think I will stick with this:

<?php

  $bootstrap_settings['freepbx_auth'] = false;
  // Load FreePBX bootstrap environment
  include '/etc/freepbx.conf';
  $fcore = FreePBX::Core();

  $user=$fcore->getUser("213");	// $data['AOR'] in actual loop
  echo '<p>Name: ' . $user['name'] . '</p>' . PHP_EOL;

Seeing this reminded me that I had tried this before and it never worked. It has been on my “I should figure out why not” list for a while.

I added it to the top of the test.php and it does not work as expected.

Instead, I simply bypassed the auth as you suggested. Which according to my notes from before is what I had to do then also.