Run AGI to send data Our CRM (Curl)

I’m working on an Asterisk AGI script to send call data to our CRM. However, I’m redirecting the trunk to a miscellaneous destination (Cell Phone). Here’s the data I want to include:

  • Ringing Time
  • Call Duration
  • Status (ANSWERED, BUSY, NOANSWER, UNAVAILABLE)
  • Caller ID
  • Dialer ID

Could you help me optimize my script?

Custom Extensions:

[from-internal-custom]
include => send-api


[efixed-send-api]
exten => h,2,AGI(send-api)

AGI

#!/usr/bin/php -q

<?php
require('phpagi.php');

$agi = new AGI();

// Data to send to the API
$data = array(
    'agi_request' => $agi->request['agi_request'],
    'agi_channel' => $agi->request['agi_channel'],
    'agi_language' => $agi->request['agi_language'],
    'agi_type' => $agi->request['agi_type'],
    'agi_uniqueid' => $agi->request['agi_uniqueid'],
    'agi_callerid' => $agi->request['agi_callerid'],
    'agi_dnid' => $agi->request['agi_dnid'],
    'agi_rdnis' => $agi->request['agi_rdnis'],
    'agi_context' => $agi->request['agi_context'],
    'agi_extension' => $agi->request['agi_extension'],
    'agi_priority' => $agi->request['agi_priority'],
    'agi_enhanced' => $agi->request['agi_enhanced'],
    'agi_accountcode' => $agi->request['agi_accountcode'],
    'agi_network' => $agi->request['agi_network'],
    'agi_network_script' => $agi->request['agi_network_script'],
);

// Convert data to JSON
$postData = json_encode($data);

// Save data to a file
$file = fopen("laravel.txt","w");
fwrite($file, $postData);
fclose($file);

// Set up cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'TEST:URL');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if ($response === false) {
    $error = curl_error($ch);
    // Handle the error
}

// Close cURL session
curl_close($ch);