Skip to content
Advertisement

call php file on incoming call (asterisk)

im new in asterisk i searched like 10hour for example how trigger a php file on incoming call to get caller ID and show to user who is calling
and i found some result and tested them

codes below are Working(copy pasted from someone example) But problem is its work(trigger php file) only when i call someone from voip and NOT when someone from outside call me and that is what i need

so can you dear programmers help me what is my mistake and gimme a working example? i know the codes in extensions_custom.conf need to be change but well im not very good at voip programming and i have no clue how to fix it

extensions_custom.conf codes:

[macro-dialout-trunk-predial-hook]
exten => s,1,Verbose(Incoming call from Sip line CallerID=${CALLERID(all)})
exten => s,2,AGI(testest.php,${CALLERID(all)})

and my test php file

#!/usr/bin/php -q
<?php 
$query = $argv[1];
file_get_contents('http://192.168.1.6/crm/test.php?s=test');
$fh = fopen('test.txt','w+');
fwrite($fh,$query);
fclose($fh);
?>

Advertisement

Answer

i FOund answer i put it in here So everyone can use because there is like no example of this simple code anywhere

<?php
$socket = fsockopen("192.168.1.100","5038", $errno, $errstr,10);
if (!$socket){
    echo "$errstr ($errno)n"; 
}else{
    //include('config.php');
    //$db = db();
    fputs($socket, "Action: Loginrn");
    fputs($socket, "UserName: adminrn");
    fputs($socket, "Secret: *********rnrn");
    //fputs($socket, "Action: WaitEventrn");
    //fputs($socket, "Action: Logoffrnrn");
$event = "";
while($ret = fgets($socket)){        
    if(substr($ret,0,6) == "Event:"){
        $e = explode(':', $ret);
        $event = trim($e[1]);
    }
    
    if($event == "DeviceStateChange"){
        $data = explode(':', $ret);
        
        if($data[0] == "Timestamp"){
            $ts = floor(trim($data[1]));
        }
    
        if($data[0] == "Device" && substr(trim($data[1]),0,3) == 'SIP'){
            $d = explode('/', trim($data[1]));
            $dev = trim($d[1]);
            $device = "";
            
            if(is_numeric($dev)){
                $device = $dev;
            }
        }
    
        if($data[0] == "State" && $device != ""){
            $state = trim($data[1]);
            
            if($state == "NOT_INUSE"){
                //Clear CID fields and update presence state
                echo 'NOT_inuse: state:'.$state.'-device: '.$device;//YOUR CODE
               // $sql = "update asterisk.web_presence set state='$state',cidnum = NULL, cidname = NULL, inorout = NULL, callstart = NULL where ext='$device'";
               // mysql_query($sql);
            }else{
                //Update presence state
                echo 'else NOT_inuse: state:'.$state.'-device: '.$device;//YOUR CODE
                $sql = "update asterisk.web_presence set state='$state' where ext='$device'";
              //  mysql_query($sql);
            }
            
            $event = "";
            $device = "";
        }    
    }
    
    if($event == "DialBegin"){
        $data = explode(':', $ret);
        
        if($data[0] == "Timestamp"){
            $ts = floor(trim($data[1]));
        }
    
        if($data[0] == "Channel"){
            $c = explode('/',trim($data[1]));
            $c2 = explode('-', trim($c[1]));
            $channel = trim($c2[0]);
        }
    
        if($data[0] == "CallerIDNum"){
            $cidnum = trim($data[1]);
        }
    
        if($data[0] == "CallerIDName"){
            $cidname = trim($data[1]);
        }
    
        if($data[0] == "DialString"){
            if(substr(trim($data[1]),0,3) == 'SIP' || is_numeric(trim($data[1]))){                
                if(is_numeric(trim($data[1]))){
                    $exten = trim($data[1]);
                }else{
                    $e = explode('/', trim($data[1]));
                    $exten = trim($e[1]);
                }
                $time = time();
                //query("insert into callq(mobile,exten,time_stamp) values('$cidnum','$exten',$time)",$db);//YOUR CODE
                echo 'dialstring-cidnum:'.$cidnum.'-cidname:'.$cidname.'-ts:'.$ts.'-ext:'.$exten;
                //Update inbound presence call
               /// $sql = "update asterisk.web_presence set cidnum = '$cidnum', cidname = '$cidname', inorout='I', callstart='$ts' where ext='$exten' and cidnum is null";//YOUR CODE
               // mysql_query($sql);    
                
               // $sql = "update asterisk.web_presence set cidnum = '$exten', inorout='O', callstart='$ts' where ext='$channel' and cidnum is null";//YOUR CODE
               // mysql_query($sql);            
            }else{
                $e = explode('@', trim($data[1]));
                $dialed = trim($e[0]);
                
                if($channel != 'gateway'){                    
                    //Update outbound presence call
                    
                   // $sql = "update asterisk.web_presence set cidnum = '$dialed', inorout='O', callstart='$ts' where ext='$channel'";
                   // mysql_query($sql);    
                }                
            }

            $event = "";
            $exten = "";
        }    
    }
        
    if($event == "UnParkedCall"){
        $data = explode(':', $ret);
        
        if($data[0] == "Timestamp"){
            $ts = floor(trim($data[1]));
        }
    
        if($data[0] == "RetrieverChannel"){
            $c = explode('/',trim($data[1]));
            $c2 = explode('-', trim($c[1]));
            $channel = trim($c2[0]);
        }
        
        if($data[0] == "ParkeeCallerIDNum"){
            $cidnum = trim($data[1]);
        }
        
        if($data[0] == "ParkeeCallerIDName"){
            $cidname = trim($data[1]);
        }        
    
        if($data[0] == "ParkingSpace"){
            $dialed = trim($data[1]);
            
            $pickup = "$cidnum ($dialed)";
            echo 'parked';
            //Update outbound presence call
           // $sql = "update asterisk.web_presence set cidnum = '$pickup', cidname='$cidname', inorout='O', state='INUSE', callstart='$ts' where ext='$channel'";
            //mysql_query($sql);    
            
            $event = "";    
            $channel = "";        
        }    
    }        
}
} 

sleep(5);

exit;   
fclose($socket); ?>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement