PHP 인터페이스 개발 에 관 한 프 리 스위치

문서 주소: FreeSWITCH 호출 인터페이스 감청
class CallSystem extends Model{


    public $url = 'http://192.168.0.251:8085/api/';
    public $username = 'admin'; //  token     
    public $password = 'admin'; //  token       

    //


    /**   2、3、4       
     * 2、      
     * 3、      ,      
     * 4、        
     * @param $Extension      (   )
     * @param $Value       
     * @param $type  array(2, 3, 4)
     * @return array
     */
    public function set_extension($Extension, $type, $Value) {

        $config = array(
            2 => array(
                'url' => 'extension_seatstatus',
                'val' => array(
                    0 => 'idle',  //  
                    1 => 'busy'   //  
                )
            ),
            3 => array(
                'url' => 'extension_seatstate',
                'val' => array(
                    0 => 'up',    //  
                    1 => 'down'   //  
                )
            ),
            4 => array(
                'url' => 'extension_callstate',
                'val' => array(
                    0 => 'ring',    //   
                    1 => 'talking', //   
                    2 => 'ivr',     //     
                    3 => 'ready'    //    
                )
            ),
        );

        if (isset($config[$type]['val'][$Value])) {
            $url = $this->url . $config[$type]['url'];
            $params = array(
                'Extension' => $Extension,
                'Value' => $config[$type]['val'][$Value]
            );
            $res = $this->curl($url, json_encode($params), true);

            return json_decode($res, true);;
        }

    }



    /**5、token  ,      token
     * @return mixed|string
     */
    public function Token() {

        $username = $this->username;
        $password = $this->password;

        $token = '';
        $cache = \Yii::$app->cache;
        //$token = $cache->delete($username . '_token');
        if ($cache->get($username . '_token')) {
            $token = $cache->get($username . '_token');

        } else {
            $params = array(
                'username' => $username,
                'password' => $password
            );
            $url = $this->url . 'login';

            $res = $this->curl($url, json_encode($params), true, false);
            $res_arr = json_decode($res,true);

            if (isset($res_arr['token'])) {
                $token = $res_arr['token'];
                $cache->add($username . '_token', $res_arr['token']);
            }

        }

        return $token;

    }


    /**6、  uuid
     * @return mixed|string
     */
    public function get_uuid() {

        $username = $this->username;

        $uuid = '';
        $cache = \Yii::$app->cache;
        if ($cache->get($username . '_uuid')) {
            $uuid = $cache->get($username . '_uuid');

        } else {
            $url = $this->url . 'get_uuid';
            $res = $this->curl($url);
            $res_arr = json_decode($res, true);

            if (isset($res_arr['uuid'])) {
                $uuid = $res_arr['uuid'];
                $cache->add($username . '_uuid', $res_arr['uuid']);
            }

        }

        return $uuid;

    }


    /**   1、7、8、9、10      
     * 1、      
     * 7、             uuid
     * 8、          
     * 9、hold    
     * 10、unhold    
     * 14、park           
     * 15、                  
     * 16、     
     * 20 preanswer
     * 22、      
     * @param $username      (   )
     * @param $type     
     * @return mixed
     */
    public function sent($username, $type) {

        $config = array(
            1 => 'extension_info/',
            7 => 'get_phone_uuid/',
            8 => 'hangup/',
            9 => 'hold/',
            10 => 'unhold/',
            14 => 'park/',
            15 => 'intercept/',
            16 => 'answer/',
            20 => 'preanswer/',
            22 => 'break/',

        );

        if (isset($config[$type])) {
            $url = $this->url . 'get_phone_uuid/' . $username;
            $res = $this->curl($url);

            return json_decode($res, true);
        }

    }




    /**11、      
     * @param $args
     * @return mixed
     */
    public function originate($args) {

        $url = $this->url . 'originate';
        $params = array(
            "Job_uuid" => $this->get_uuid(),                                              //uuid
            "Callername" => $args['Callername'],                                          //     
            "Callernum" => $args['Callernum'],                                            //    
            "Calleenum" => $args['Calleenum'],                                            //    
            "A_gatewayname" => $args['A_gatewayname'],                                    //  a   ,    a     
            "B_gatewayname" => $args['B_gatewayname'],                                    //  b   ,    b     
            "A_effective_caller_id_num" => $args['A_effective_caller_id_num'],            //        
            "B_effective_caller_id_num" => $args['B_effective_caller_id_num'],            //        
            "A_external" => $args['A_external'],                                          //  a     
            "B_external" => $args['B_external']                                           //  b     
        );

        $res = $this->curl($url, json_encode($params), true);

        return json_decode($res,true);

    }


    /**11b           1、           2、          3、      
     * @param $args
     * @return mixed
     */
    public function originate_playring($args) {

        $url = $this->url . 'originate_playring';

        $params = array(
            "Job_uuid" => $this->get_uuid(),                                    //uuid
            "Callername" => $args['Callername'],                                //     
            "Callernum" => $args['Callernum'],                                  //    
            "Calleenum" => $args['Calleenum'],                                  //    
            "A_gatewayname" => $args['A_gatewayname'],                          //  a   ,    a     
            "B_gatewayname" => $args['B_gatewayname'],                          //  b   ,    b     
            "A_effective_caller_id_num" => $args['A_effective_caller_id_num'],  //        
            "B_effective_caller_id_num" => $args['B_effective_caller_id_num'],  //        
            "A_external" => $args['A_external'],                                //  a     
            "B_external" => $args['B_external'],                                //  b     
            "Ring_id" => $args['Ring_id']
        );


        $res = $this->curl($url, json_encode($params), true);

        return json_decode($res,true);
    }


    /**11c               cdr post URL
     * @return mixed
     */
    public function originate_yn() {

        $url = $this->url . 'originate_yn';
        $params = array(
            "Job_uuid" => "1234",
            "Callername" => "1002",
            "Callernum" => "1002",
            "Calleenum" => "1000",
            "A_gatewayname" => "",
            "B_gatewayname" => "",
            "A_effective_caller_id_num" => "1002",
            "B_effective_caller_id_num" => "1000",
            "A_external" => "false",
            "B_external" => "false",
            "Ring_id" => "35",
            "Cdr_url" => "http://123.1.2.2/cdr/",
            "Event_url" => "http://123.1.2.2/event/"
        );
        $res = $this->curl($url, json_encode($params), true);

        return json_decode($res,true);;
    }



    /**11d   fsgui        
     * @return mixed
     */
    public function originate_ext() {

        $url = $this->url . 'originate_ext';
        $params = array(
            "Job_uuid" => "1234",
            "Callername" => "1002",
            "Callernum" => "1002",
            "Calleenum" => "1000",
            "A_effective_caller_id_num" => "1002",
            "GatewayGroupId" => "1",
            "B_effective_caller_id_num" => "1000",
            "A_external" => "false",
            "B_external" => "false",
            "Ring_id" => "35",
            "Cdr_url" => "http://123.1.2.2/cdr/",
            "Event_url" => "http://123.1.2.2/event/"

        );
        $res = $this->curl($url, json_encode($params), true);

        $res_arr = json_decode($res,true);
        return $res_arr;
    }







    /**12、 uuid           13、           
     * @param $args
     * @param $type 0、 uuid          1、           
     * @return mixed
     */
    public function bridge($args, $type) {

        $config = array(
            0 => array(
                "A_uuid" => $this->sent($args['A_uuid'], 7),
                "B_uuid" => $this->sent($args['B_uuid'], 7)
            ),
            1 => array(
                "A_phone" => $args['phone1'],
                "B_phone" => $args['phone2']
            )
        );

        if ($type==0) {
            $url = $this->url . 'bridge';
        } else {
            $url = $this->url . 'bridge_phone';
        }
        $params = $config[$type];

        $res = $this->curl($url, json_encode($params), true);

        return json_decode($res, true);
    }


    /**17                、       、           ,       
     * 18                      、                  、        ,       
     * 19                  、     、       ,       
     * 26                  、      、       ,       
     * @param $args
     * @param $type
     * @return array
     */
    public function number($args, $type) {

        $args = array(
            "EvdedPhone" => $args['EvdedPhone'],
            "EvdPhone" => $args['EvdPhone'],
            "EvdGateway" => $args['EvdGateway']
        );

        $flag = '';
        switch ($type) {
            case 17 :
                $flag = 'eavesdrop';
                break;
            case 18 :
                $flag = 'threeway';
                break;
            case 19 :
                $flag = 'threeway';
                break;
            case 26 :
                $flag = 'transfer';
                break;
            default:
                break;
        }
        $url = $this->url . $flag;
        $res = $this->curl($url, json_encode($args), true);

        return json_decode($res, true);;
    }






    /**21、  answer     
     * @param $PhoneNumber        
     * @param $FilePath         
     * @param $Loopplay      
     * @return mixed
     */
    public function playback($PhoneNumber, $FilePath, $Loopplay) {

        $url = $this->url . 'playback';
        $params = array(
            "PhoneNumber" => $PhoneNumber,
            "FilePath" => $FilePath,
            "Loopplay" => $Loopplay
        );

        $res = $this->curl($url, json_encode($params), true);

        $res_arr = json_decode($res, true);
        return $res_arr;
    }


    /**23、  
     * @param $PhoneNumber        
     * @param $Filepath       
     * @param $Flag      
     * @param $Limit 0           ,   5,       5    
     * @return mixed
     */
    public function record($PhoneNumber, $Filepath, $Flag, $Limit) {

        $url = $this->url . 'record';
        $params = array(
            "PhoneNumber" => $PhoneNumber,
            "Filepath" => $Filepath,
            "Flag" => $Flag,
            "Limit" => $Limit
        );
        $res = $this->curl($url, json_encode($params), true);

        $res_arr = json_decode($res, true);
        return $res_arr;

    }



    /**24   dtmf
     * @param $PhoneNumber     dtmf   
     * @param $Dtmf        dtmf
     * @return mixed
     */
    public function senddtmf($PhoneNumber, $Dtmf) {

        $url = $this->url . 'senddtmf';
        $params = array(
            "PhoneNumber" => $PhoneNumber,
            "Dtmf" => $Dtmf
        );
        $res = $this->curl($url, json_encode($params), true);

        $res_arr = json_decode($res, true);
        return $res_arr;
    }


    /**25     
     * @return mixed
     */
    public function send_fax() {

        $url = $this->url . 'send_fax';
        $params = array(
            "PhoneNumber" => "02112345678",
            "GatewayName" => "fax",
            "FaxFile" => "/tmp/av.tiff"
        );
        $res = $this->curl($url, json_encode($params), true);

        $res_arr = json_decode($res, true);
        return $res_arr;
    }













    public function curl($url, $data = [], $post=false, $flag=true){
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTP_VERSION, '1.0');//  HTTP 1.0  
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if($post){
            curl_setopt($curl, CURLOPT_POST, TRUE);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FRESH_CONNECT ,true); //      
        //      ----------------------------------
        $ip = $_SERVER['REMOTE_ADDR'];
        $ipArray = explode('.', $ip);
        $ip = $ipArray[0].'.'.$ipArray[1].'.'.rand(2,254).'.'.rand(2,254);
        $header[] = "Accept: ".$_SERVER['HTTP_ACCEPT'];
        $header[] = 'X-FORWARDED-FOR: '.$ip;
        $header[] = 'CLIENT-IP: '.$ip;
        $header[] = "Cache-Control: no-cache";
        $header[] = "Connection: keep-alive";
        $header[] = "Keep-Alive: 300";
        $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[] = "Accept-Language: zh-CN,zh;q=0.8,en;q=0.6";
        $header[] = "User-Agent: ".$_SERVER['HTTP_USER_AGENT']; // browsers keep this blank.
        $header[] = "Content-Type:application/json";

        if ($flag) {
            $header[] = "Authorization:Nway " . $this->Token();
        }

        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }



}

좋은 웹페이지 즐겨찾기