Call Redirecting using channel and Extension number

Hi all, I’m new to asterisk. I have this basic code for transferring calls using AMI to an extension from another extension but it requires the channel and the extension. The problem is i dont know where should i get the channel to use in the code. I have tried getting the channel from the asterisk -rx ‘core show channels’ command manually and use it in the code, it works fine, but the channels are inconsistent and only shows when theres a call ongoing. How can i do that programmatically? Where should i get the channel? Can i use extension number as a channel?

this is the code

<?php $ami_host = '127.0.0.1'; $ami_port = '5038'; $ami_username = 'user'; $ami_password = 'pass'; $channel_to_transfer = ' '; $destination_extension = '2001'; $socket = fsockopen($ami_host, $ami_port, $errno, $errstr, 10); if (!$socket) { die("Unable to connect to Asterisk Manager Interface: $errstr ($errno)\n"); } loginToAMI($socket, $ami_username, $ami_password); transferCallToExtension($socket, $channel_to_transfer, $destination_extension); logoutFromAMI($socket); fclose($socket); function loginToAMI($socket, $username, $password) { sendAMIData($socket, "Action: Login\r\nUsername: $username\r\nSecret: $password\r\n\r\n"); readAMIResponse($socket); } function logoutFromAMI($socket) { sendAMIData($socket, "Action: Logoff\r\n\r\n"); readAMIResponse($socket); } function transferCallToExtension($socket, $channel, $destination_extension) { sendAMIData($socket, "Action: Redirect\r\nChannel: $channel\r\nExten: $destination_extension\r\nContext: your_context_name\r\nPriority: 1\r\n\r\n"); readAMIResponse($socket); } function sendAMIData($socket, $data) { fwrite($socket, $data); } function readAMIResponse($socket) { $response = ''; while (!feof($socket)) { $response .= fgets($socket, 4096); if (strpos($response, "\r\n\r\n") !== false) { break; } } echo $response; } ?>

Sorry if the question is a bit blurry, I am really new to the asterisk, anything will help. Thank you.

Please mark up code (logs, etc.) as pre-formatted text.

You are confusing extensions with endpoints.

That’s fairly obvious as the channel only exists when there is a call leg on it. Typically you would look for the NEWCHANNEL event or have to the dialplan generate a cujstom event.

There’s already an application that can blind transfer a call to an extension using the server. Should i create another event? or just find the context that is being used?

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