Use AMI to send a calls to extension

I am using Asterisk 16.28.0
Current PBX Version:
Current System Version:
I am trying to send calls to an ext from a different host to the freePBX via php

<? $socket = fsockopen("","5038", $errno, $errstr, 30); fputs($socket, "Action: Login\r\n"); fputs($socket, "UserName: test2\r\n"); fputs($socket, "Secret:*******\r\n\r\n"); fputs($socket, "Action: Originate\r\n"); fputs($socket, "Channel: PJSIP/1203\r\n"); fputs($socket, "Exten: 00357*********\r\n"); fputs($socket, "Priority: 1\r\n"); fputs($socket, "Timeout: 5000\r\n" ); fputs($socket, "Context: from-internal\r\n"); fputs($socket, "Callerid: Pick Me Up <1203>\r\n\r\n"); fputs($socket, "Action: Logoff\r\n\r\n"); fclose($socket); ?>

On the voip server i have /etc/asterisk/manager.conf
enabled = yes
port = 5038
bindaddr =
displayconnects=no ;only effects 1.6+

secret = ********
read = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate,message
write = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate,message
writetimeout = 5000

The problem that I have, is that it works after I run the script (from a different host ) multiple times . It might work from 3rd run or on the 10th run at random time. Every time I run the script it sends a signal to the voip server . But it triggers the extension at randomly and not every time I run the script.

Many Thanks,

Do you want more info on this ? If yes what info you would like to have ?

The script laid out so as to be readable.

The results from a supported version of Asterisk (18 or 20).

Asterisk full log at at least verbosity 5.

Capture of the contents of the TCP stream.

The values of errno and errstr.

The permissions for test2

However, I don’t see how this could work at all, as there is no mechanism by which the GUI definition could be integrated with the manager.conf you have, assuming that you have provided the whole file. There would need to be #include lines for it to work.

Note that it is bad practice to not read and check the responses, although the effect is more likely to be the lack of diagnostic information, rather than actually a malfunction, with such a small script or you have unnecessary read permissions and a lot of events arrive before processing of the Originate starts.

Hi david ,
Thanks for the reply ,
I did a test with the script to provide the info .
I have set the debug “manager set debug on” . I am attaching the logfile full .

I have run the script 18 times from the host and only on 18th run it was a success . You can see it on 2022-11-23 10:23:46 in the log. So on the 18th run I got a ring on the extension 1203 .
I am also providing tcpdump from the voip server “tcpdump host -s 65535 -w /root/outtosee.pcap and port 5038” .

When I run the script has no errors . I am aslo providing the script (php) in the attachment

Below are the permissions of test2 :
debug.tgz (11.4 KB)
debug.tgz (11.4 KB)

voip1*CLI> manager show user test2

      username: test2
        secret: <Set>
           ACL: yes
     read perm: system,call,log,verbose,command,agent,user,config,dtmf,reporting,cdr,dialplan,originate
    write perm: system,call,log,verbose,command,agent,user,config,dtmf,reporting,cdr,dialplan,originate

displayconnects: no
allowmultiplelogin: yes
ACL: (unnamed)

0: deny -
1: allow -
2: allow -

Below are the settings
voip1*CLI> manager show settings

Global Settings:

Manager (AMI): Yes
Web Manager (AMI/HTTP): No
TCP Bindaddress:
HTTP Timeout (seconds): 60
TLS Enable: No
TLS Bindaddress: Disabled
TLS Certfile: asterisk.pem
TLS Privatekey:
TLS Cipher:
Allow multiple login: Yes
Display connects: No
Timestamp events: No
Channel vars:
Disabled events:
Debug: Yes

I’d try it with no read permissions, unless you need them for other AMI operations using that user.

Your full log seems to have nothing but AMI events, not what I would expect.

Hi david55,

I tried it without read permissions and it is still the same . I have set also the debug level to 10

core set debug 10

On the log at 2022-11-23 14:24:52 line 334 is the success and it was on the 5th time . The past 4 runs can be seen that it got the signal but it was not issue the ring on extension 1203.

[2022-11-23 14:24:52] DEBUG[17898] iostream.c: TCP socket error writing: Broken pipe

That’s another reason why you need to read the responses!

(The dirty solution would be to delay before closing the socket, but the right one is to read all the responses promptly (and preferably check them).)

Hi David55
You mean to read the responses from the script ? Can you give a small example or something to work on it ?


There may be some set up involved, e.g. to control buffering. Please don’t ask me for that level of detail, as I’d have to research it, but you are the one that would benefit, so I expect you to do the research. You probably want to put the stream into non-blocking mode. You may have to use fread, as fgets may block. You might also want to investigate stream_set_timeout.

Most people would use a class library which will handle this level of processing, although you may still need to ensure that there is somewhere for input to go.

The dirty approach is sleep(......

Thanks david !
Yes this is works, tested it with sleep . I will change it by reading back so there is a control and remove the sleep .
Many Thanks!

This might be useful .

$socket = fsockopen("","5038", $errno, $errstr, 30);
$credentials = "Action: Login\r\nUserName: test2\r\nSecret:********\r\n\r\n";

#stream_set_timeout($socket, 3); //May be is good to have it for safety                                                                                                                                        

$call_data .= "$credentials";
$call_data .= "Action: Originate\r\nChannel: PJSIP/1203\r\n";
$call_data .= "Exten: 00357*******\r\n";
$call_data .= "Priority: 1\r\nAsync: yes\r\nTimeout: 1000\r\nContext: from-internal\r\n";
$call_data .= "Callerid: Pick Me Up <1203>\r\n\r\nAction: Logoff\r\n\r\n";

$error = send_get_responses_from_voip( $socket, $call_data );
if ( $error ) //do something if error                                                                                                                                                                          
    print "$error \n";


function send_get_responses_from_voip ( $socket, $data ){
    fputs( $socket, $data );
    //read the response                                                                                                                                                                                        
    while ($count = strlen($c = fread($socket, 1024)) > 0)
        $response_txt .= $c;

    if (preg_match_all('/error/i',$response_txt, $check)) {

        return $response_txt;
    } else {

        return null ;


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