Freeswitch XML-RPC

From FreeSWITCH Wiki

Jump to: navigation, search

Test program for connecting to the Freeswitch XML-RPC interface.


Contents

Ruby Example

#!/usr/bin/env ruby

require 'xmlrpc/client'
require 'xmlrpc/marshal'

exit unless ARGV[0]

port = 8080
server = '192.168.190.248'
directory = '/RPC2'

def fixup(value)
      value.gsub(/&lt;/, "<").
            gsub(/&gt;/, ">")
end

server = XMLRPC::Client.new(server, directory, port, nil, nil, 'freeswitch', 'works', nil, 10)
marshal = XMLRPC::Marshal.new()

puts "Executing command #{ARGV.join(' ')}"
result = server.call("freeswitch.api", "#{ARGV.shift}", "#{ARGV.join(' ')}")

puts fixup(marshal.dump_response(result))

PHP Example

Uses the xmlrpc-2.1 library from http://phpxmlrpc.sourceforge.net/

<?php

//
// Kludged together by Ken Rice - SwK on #freeswitch/freenode
//

include("xmlrpc.inc");

$username = "YOURUSERNAME";
$password = "YOURPASSWORD";

// Play nice to PHP 5 installations with REGISTER_LONG_ARRAYS off

$stateno=(integer)$HTTP_POST_VARS["stateno"];
$f=new xmlrpcmsg('freeswitch.api',
        array(new xmlrpcval("show", "string"),new xmlrpcval("channels", "string"))
);
$c=new xmlrpc_client("/RPC2", "localhost", 8080);
$c->setCredentials($username,$password,NULL);


// $c->setDebug(2); // Uncomment a Value of 1 Outputs Received XML, 
                    // Gets you Both Sent and Returned XML
$r=&$c->send($f);
echo "<PRE>";
if(!$r->faultCode())
{       
        $v=$r->value();
        $foo = explode("\n" , $v->scalarval());
        
        $x=0;
        foreach ($foo as $bar){
                $foobar = explode(",", $bar);
                if (preg_match("/created/", $foobar[1])) {
                        $nope =1 ; // dont show the first line
                } elseif (sizeof($foobar) < 2) {
                        $nope =1 ; // dont lines at the end
                } else {
                        $calls[$x] = $foobar; // grab the exploded lines into an array
                        $x++;
                }
        }
        print_r($calls);
} else {
        print "An error occurred: ";
        print "Code: " . htmlspecialchars($r->faultCode())
                . " Reason: '" . htmlspecialchars($r->faultString());
}
echo "</PRE>";
?>


Perl Example

#!/usr/bin/perl                                                                                                                                                 
# by Brian West (bkw_) or #freeswitch                                                                                                                                                    
use RPC::XML::Client;                                                                                                                                           
use Data::Dumper;                                                                                                                                               
                                                                                                                                                                
my $client = new RPC::XML::Client('http://localhost:8080/RPC2');                                                                                                
                                                                                                                                                                
my $req = RPC::XML::request->new('freeswitch.api',                                                                                                              
                                 'show',                                                                                                                        
                                 'channels');                                                                                                                   
                                                                                                                                                                
$client->credentials ("freeswitch" , "freeswitch", "works");                                                                                              
                                                                                                                                                                
                                                                                                                                                                
$res = $client->send_request($req);                                                                                                                             
                                                                                                                                                                
my $value = $res->value;                                                                                                                                        
chomp($value);                                                                                                                                                  
                                                                                                                                                                
print Dumper $req;                                                                                                                                              
                                                                                                                                                                
print Dumper $value;   


Python Example


#!/usr/bin/python

from xmlrpclib import ServerProxy

host = 'localhost'
username = 'test'
password = 'test'
port = '8080'

server = ServerProxy("http://%s:%s@%s:%s" % (username, password, host, port))
print server.freeswitch.api("show","channels")


Java Example

Using apache XmlRpcClient

        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        XmlRpcClient client = new XmlRpcClient();
        try {

            config.setServerURL(new URL("http://localhost:8080/RPC2"));
            config.setBasicUserName("freeswitch");
            config.setBasicPassword("works");
            client.setConfig(config); 
           
            // gw1 is the gateway you setup. and 1234 is the number you want to call
            client.execute("freeswitch.api", new Object[]{"originate", "sofia/gateway/gw1/1234 &park()"});

        } catch (Exception ex) {ex.printStackTrace();}
Personal tools