Cant create SIP Extension from php script

Hello there, i have a problem. I want to create a new extension from my own php project. i created a service that will work with freepbx. My problem is the extension type. I have to create it as sip, but it keeps created as virtual…

My Script

$user = [
“extension” =>$_POST[“extension”],
“password” =>$_POST[“password”],
“name” =>$_POST[“name”],
];

    try {
        $bootstrap_settings = array();
        $bootstrap_settings['freepbx_auth'] = false;
        if ([email protected]_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) { 
            include_once('/etc/asterisk/freepbx.conf'); 
        }
        core_users_add($user);
        $ex = (int)$user["extension"];
        $userName = $user["name"];
        core_devices_add($ex,"sip","SIP/$userName","fixed",$ex ,$userName);
    }
    catch (Exception $e ){
        if($e->getMessage()=="Fields are empty::"){
            $status = 200;
            $message = "Extension Ekleme İşlemi Başarıyla Sonuçlandı...";
        }
        else{
            $status = 0;
            $message = $e->getMessage();
        }
        
    }

My Version

Framework: 14.0.13.4

Well it would help to create the user (extension) with all the proper fields like what tech the extension is. No tech means virtual.

Hi Tom;

Then where should I add the tech parameter and how ? Is this correct ?

$user = [
“extension” =>$_POST[“extension”],
“password” =>$_POST[“password”],
“name” =>$_POST[“name”],
“tech” =>“sip”
];

    try {
        $bootstrap_settings = array();
        $bootstrap_settings['freepbx_auth'] = false;
        if ([email protected]_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) { 
            include_once('/etc/asterisk/freepbx.conf'); 
        }
        core_users_add($user);
        $ex = (int)$user["extension"];
        $userName = $user["name"];
        core_devices_add($ex,"sip","SIP/$userName","fixed",$ex ,$userName);

So a couple things I didn’t catch on this earlier. Assume extension = 100 and the name = Test

$user[‘extension’] is already an integer why are you doing (int)$user['extension']?

How do you expect Asterisk to dial SIP/Test when it should be SIP/100?

Yes you’re right, it should change “SIP/$userName” but name always same extension in my project. I wanted to make sure extension is type of int because $_POST always returns string. Sorry this is not my solution.

This is PHP code, so No, PHP returns a value that can be interpreted in one of several ways. Variable type in PHP is fluid and converts automatically to whatever the lvalue is required to be. To be further pedantic, $_POST always returns an associative array. Sorry - CIS teacher mode popped up there.

Either way - have you looked at the code for “Bulk Handler”? It does all of this and is open source. You should be able to snag that code and reinterpret it to manage your code.

Also, with FreePBX 15, there’s going to be an API, so that’s going to deprecate this code.

first of all thank you for correcting me on this. I’m a junior developer who has switched to php for the last 1 month.

I looked but did not understand much. do I need to create and import csv file in my service code every time. And after creating sip extension, I need to add it to the current “paging and intercom”. Is this possible?

I couldn’t find enough information about rest api in wiki

I’m not suggesting you drop your current project or code. The source code for the Bulk Handler module is available from GitHub.com (in the FreePBX Git Repo) so you can easily snag the code that already does this for the current system and “extend” it to do everything you need to do.

If all you needed to do was create extensions, the easy way is to create a CSV file with the fields filled in and feed it to the Bulk Handler module. Since you need to do more than that, your approach (assuming you lift the code from GitHub) will probably work just fine.

The REST API (which is available now in several versions of FreePBX) is different than the system API that’s coming out with the release of FreePBX 15. It’s not surprising that you can’t find a lot of documentation yet - I get the impression that everyone at Sangoma is playing “Rock - Paper - Scissors” to see who is going to get stuck documenting this cool new feature. :slight_smile:

It’s already well documented.

https://wiki.freepbx.org/display/FPG/API

https://wiki.freepbx.org/display/FOP/PBX+API

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.