PHP code example of bitdreamit / laravel-mikopbx

1. Go to this page and download the library: Download bitdreamit/laravel-mikopbx library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bitdreamit / laravel-mikopbx example snippets


// via tinker, a seeder, or your own admin UI
$user = App\Models\User::find(1);
$user->update([
    'pbx_extension'    => '121',              // the plain extension number in MikoPBX
    'pbx_sip_password' => 'sip-password-here', // set in MikoPBX Admin → Extensions → edit
]);

use BitDreamIT\MikoPBX\Traits\HasMikoPBXExtension;

class User extends Authenticatable
{
    use HasMikoPBXExtension;

    protected $fillable = [..., 'pbx_extension', 'pbx_sip_password'];
    protected $hidden   = [..., 'pbx_sip_password'];
}

$user->update([
    'pbx_extension'    => '121',
    'pbx_sip_password' => 'the-sip-password-from-mikopbx',
]);

use BitDreamIT\MikoPBX\Facades\MikoPBX;

// ── Active calls (REST API v3) ─────────────────────────────────────────────
$response = MikoPBX::api()->getActiveCalls();
// $response = ['result' => true, 'data' => [...active calls...]]

// ── Call control (AMI — REST v3 has no call control endpoints) ─────────────
MikoPBX::originate('101', '01711000000');         // Extension 101 → customer
MikoPBX::transfer('PJSIP/101-00000001', '102');   // Transfer to ext 102
MikoPBX::hangup('PJSIP/101-00000001');            // End the call

// Or via ami() service directly:
MikoPBX::ami()->connect();
MikoPBX::ami()->originate('101', '01711000000');
MikoPBX::ami()->disconnect();

// ── CDR records (REST API v3) ──────────────────────────────────────────────
$response = MikoPBX::api()->getCDR('2026-06-01 00:00:00', '2026-06-30 23:59:59', [
    'src_num'     => '01711000000',  // filter by caller
    'limit'       => 50,
    'offset'      => 0,
]);
// See "CDR Field Names & Nested Structure" below — the real response is
// nested by call (linkedid), not a flat list of records.

// ── Extensions (REST API v3) ───────────────────────────────────────────────
$exts = MikoPBX::api()->getExtensions(); // GET /v3/extensions:getForSelect
// Returns: [{"value": "101", "text": "101 John Smith"}, ...]

$statuses = MikoPBX::api()->getExtensionStatuses(); // GET /v3/sip:getPeersStatuses
// Each item: { id: "101", state: "OK|REGISTERED|UNREACHABLE|...", ipaddress, port }

// ── SIP trunk status ───────────────────────────────────────────────────────
$trunks = MikoPBX::api()->getTrunkStatus(); // GET /v3/sip-providers:getStatuses
$isUp   = collect($trunks['data'] ?? [])->contains(fn($t) => $t['state'] === 'REGISTERED');

// ── Campaigns ─────────────────────────────────────────────────────────────
$campaign = MikoPBX::campaign()->create(
    ['name' => 'June Promo', 'type' => 'voice_broadcast', 'max_channels' => 5],
    ['01711000001', '01711000002', '01711000003']
);
MikoPBX::campaign()->start($campaign);
MikoPBX::campaign()->pause($campaign);
MikoPBX::campaign()->stop($campaign);

// ── Agents ────────────────────────────────────────────────────────────────
$agents = MikoPBX::agent()->all();          // With live SIP status merged
$count  = MikoPBX::agent()->sync();         // Pull from MikoPBX → local DB

// ── Blacklist ─────────────────────────────────────────────────────────────
MikoPBX::blacklist()->add('01711999999', 'Spam caller', 'both');
$blocked = MikoPBX::blacklist()->isBlocked('01711999999');
MikoPBX::blacklist()->remove('01711999999');

// ── Callbacks ─────────────────────────────────────────────────────────────
MikoPBX::callback()->schedule('01711000000', [
    'name'     => 'Customer Name',
    'priority' => 'urgent',        // low | normal | high | urgent
    'note'     => 'Called about order #1234',
]);

// ── Analytics ─────────────────────────────────────────────────────────────
$summary = MikoPBX::analytics()->summary('2026-06-01', '2026-06-30');
// Returns: total_calls, answered, missed, failed, asr%, avg_duration, inbound, outbound

// ── Health ────────────────────────────────────────────────────────────────
$health = MikoPBX::health()->check();
// Returns: ['status' => 'healthy', 'amiOk' => true, 'ariOk' => true, 'sipOk' => true, 'calls' => 3]

// ── IVR ───────────────────────────────────────────────────────────────────
$menus = MikoPBX::api()->getIVRMenus();  // GET /v3/ivr-menu
MikoPBX::api()->saveIVRMenu(['name' => 'Main Menu', 'nodes' => [...]]);

// ── Sound files ────────────────────────────────────────────────────────────
MikoPBX::api()->uploadAudio('/path/to/greeting.wav');  // POST /v3/sound-files:uploadFile

// ── Using the trait on your User model ─────────────────────────────────────
$user->callNumber('01711000000');      // originate from this user's own extension
$user->callLogs();                     // this user's call history (

Schedule::command('mikopbx:cdr-sync')->hourly();
Schedule::command('mikopbx:campaign-run --sync')->everyFiveMinutes();
Schedule::command('mikopbx:health')->everyFiveMinutes();

use BitDreamIT\MikoPBX\Testing\MikoPBXFake;

class CallTest extends TestCase
{
    private MikoPBXFake $fake;

    protected function setUp(): void
    {
        parent::setUp();
        $this->fake = MikoPBXFake::make($this->app);
    }

    protected function tearDown(): void
    {
        $this->fake->reset();
        parent::tearDown();
    }

    public function test_placing_order_triggers_call(): void
    {
        $this->post('/orders', ['phone' => '01711000000']);

        $this->fake->assertOriginated('101', '01711000000');
    }

    public function test_no_call_when_blacklisted(): void
    {
        Blacklist::create(['number' => '01711999999', 'direction' => 'both']);

        $this->post('/orders', ['phone' => '01711999999']);

        $this->fake->assertNothingOriginated();
    }
}
bash
php artisan mikopbx:install
bash
php artisan vendor:publish --tag=mikopbx-config
php artisan vendor:publish --tag=mikopbx-migrations
php artisan vendor:publish --tag=mikopbx-public
php artisan migrate
bash
php artisan mikopbx:sync-extensions

php artisan mikopbx:listen
bash
php artisan mikopbx:cdr-sync --days=7