原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://strugglelinux.blog.51cto.com/1009905/609161

最近在做公司的呼叫中心,一时心血来潮想实现自动呼叫功能,现在只做了个示例,用到的时候再扩展。

在实现自动呼叫的示例中我使用的是Asterisk manager API中的Originate方法,该方法在phpagi的中。具体定义如下:(英文我就不解释了,我英文很水

 
   
  1. /**
  2. * Originate Call
  3. *
  4. * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Originate
  5. * @param string $channel Channel name to call
  6. * @param string $exten Extension to use (requires 'Context' and 'Priority')
  7. * @param string $context Context to use (requires 'Exten' and 'Priority')
  8. * @param string $priority Priority to use (requires 'Exten' and 'Context')
  9. * @param string $application Application to use
  10. * @param string $data Data to use (requires 'Application')
  11. * @param integer $timeout How long to wait for call to be answered (in ms)
  12. * @param string $callerid Caller ID to be set on the outgoing channel
  13. * @param string $variable Channel variable to set (VAR1=value1|VAR2=value2)
  14. * @param string $account Account code
  15. * @param boolean $async true fast origination
  16. * @param string $actionid message matching variable
  17. */
  18. function Originate($channel,
  19. $exten=NULL, $context=NULL, $priority=NULL,
  20. $application=NULL, $data=NULL,
  21. $timeout=NULL, $callerid=NULL, $variable=NULL, $account=NULL, $async=NULL, $actionid=NULL)
  22. {
  23. $parameters = array('Channel'=>$channel);
  24. if($exten) $parameters['Exten'] = $exten;
  25. if($context) $parameters['Context'] = $context;
  26. if($priority) $parameters['Priority'] = $priority;
  27. if($application) $parameters['Application'] = $application;
  28. if($data) $parameters['Data'] = $data;
  29. if($timeout) $parameters['Timeout'] = $timeout;
  30. if($callerid) $parameters['CallerID'] = $callerid;
  31. if($variable) $parameters['Variable'] = $variable;
  32. if($account) $parameters['Account'] = $account;
  33. if(!is_null($async)) $parameters['Async'] = ($async) ? 'true' : 'false';
  34. if($actionid) $parameters['ActionID'] = $actionid;
  35. return $this->send_request('Originate', $parameters);
  36. }

下面是服务端的简单代码(很简单我只是实现执行该文件自动拨号,对方接听后会听到 hello world 的语音)

ami.php

 
   
  1. #!/usr/bin/php -q
  2. <?php
  3. include "phpagi-asmanager.php";
  4. $ams = new AGI_AsteriskManager();
  5. $ams->AGI_AsteriskManager("ami.conf");
  6. $result = $ams->connect();
  7. $res = $ams->Originate('sip/8001','8001','from-internal','1','Playback','hello-world',30000,'192.168.1.112');
  8. var_dump($res); //这个是查看输出信息的调试代码
  9. ?>

ami.conf文件是访问API的验证文件:内容如下:

 
   
  1. [asmanager]
  2. server=127.0.0.1 ; server to connect to
  3. port=5038 ; default manager port
  4. username=admin ; username for login
  5. secret=123456 ; password for login

该文件的内容要和asterisk内的/etc/asterisk/manager.conf 文件中的用户密码相同 ,以上两个文件我是放在 /var/spool/asterisk/outgoing/ 目录中的

(要给执行的权限)

以上编写完成之后在服务器上直接运行就可以呼叫拨号了:

执行./ami.php

各位看官可以自己扩展一下,比如通过访问网址来传递呼叫参数!或者通过某些程序自动执行该程序!我只想把这个用到服务器监控上面,不用再使用旧的短信提示,如果宕机就直接拨负责人的电话,接通之后自动拨放录音,还要循环播放,让他不知道都难!

本文出自 “我菜故我在” 博客,请务必保留此出处http://strugglelinux.blog.51cto.com/1009905/609161