API: PHP Example
Bryan Nye avatar
Written by Bryan Nye
Updated over a week ago

This is a very basic example of the MetaLocator XML/RPC service.  This uses the test method MetaLocator.add, which simply adds two numbers.  This method is good to use for testing basic communication with our service. This example makes use of the free XMLRPC library available from Sourceforge.

* Adds two numbers.  This is a test method present to assist in testing proper communication with the MetaLocator API. Param 1(int): Number 1, Param 2(int): Number 2.  Returns the two numbers added together within an XMLRPC Struct
* @param string $p1
* @param string $p2
* @param int $debug when 1 (or 2) will enable debugging of the underlying xmlrpc call (defaults to 0)
* @return array (or an xmlrpcresp obj instance if call fails)

//include our XMLRPC library
require_once "../xmlrpc-debug/lib/xmlrpc.inc";
function xmlrpc_MetaLocator_add ($p1, $p2, $debug=0) {
       $client = new xmlrpc_client('/xmlrpc', 'admin.metalocator.com', 0);
       $client->return_type = 'xmlrpcvals';
       $msg = new xmlrpcmsg('MetaLocator.add');
       $p1 = new xmlrpcval($p1, 'string');
       $p2 = new xmlrpcval($p2, 'string');
       $res =& $client->send($msg, 0, '');
       if ($res->faultcode()) return $res; else return php_xmlrpc_decode($res->value());

$result = xmlrpc_MetaLocator_add(5,7);
echo "MetaLocator responded " . print_r($result,1);

If correctly executed, the output of this script should be:

 MetaLocator responded Array ( [value1] => 5 [value2] => 7 [total] => 12 )

Happy coding!

Did this answer your question?