function testAutoRegisteredMethod()
 {
     $func = wrap_xmlrpc_method($this->client, 'examples.getStateName');
     if ($func == '') {
         $this->fail('Registration of examples.getStateName failed');
     } else {
         $v = $func(23);
         $this->assertEquals('Michigan', $v);
     }
 }
Beispiel #2
0
<?php 
include "xmlrpc.inc";
include "xmlrpc_wrappers.inc";
$c = new xmlrpc_client("/server.php", "phpxmlrpc.sourceforge.net", 80);
$c->return_type = 'phpvals';
// let client give us back php values instead of xmlrpcvals
$r =& $c->send(new xmlrpcmsg('system.listMethods'));
if ($r->faultCode()) {
    echo "<p>Server methods list could not be retrieved: error '" . htmlspecialchars($r->faultString()) . "'</p>\n";
} else {
    $testcase = '';
    echo "<p>Server methods list retrieved, now wrapping it up...</p>\n<ul>\n";
    foreach ($r->value() as $methodname) {
        // do not wrap remote server system methods
        if (strpos($methodname, 'system.') !== 0) {
            $funcname = wrap_xmlrpc_method($c, $methodname);
            if ($funcname) {
                echo "<li>Remote server method " . htmlspecialchars($methodname) . " wrapped into php function " . $funcname . "</li>\n";
            } else {
                echo "<li>Remote server method " . htmlspecialchars($methodname) . " could not be wrapped!</li>\n";
            }
            if ($methodname == 'examples.getStateName') {
                $testcase = $funcname;
            }
        }
    }
    echo "</ul>\n";
    if ($testcase) {
        echo "Now testing function {$testcase}: remote method to convert U.S. state number into state name";
        $statenum = 25;
        $statename = $testcase($statenum, 2);
 public function testTransferOfObjectViaWrapping()
 {
     // make a 'deep client copy' as the original one might have many properties set
     $func = wrap_xmlrpc_method($this->client, 'tests.returnPhpObject', array('simple_client_copy' => true, 'decode_php_objs' => true));
     if ($func == false) {
         $this->fail('Registration of tests.returnPhpObject failed');
     } else {
         $v = $func();
         $obj = new stdClass();
         $obj->hello = 'world';
         $this->assertEquals($obj, $v);
     }
 }
/**
 * Similar to wrap_xmlrpc_method, but will generate a php class that wraps
 * all xmlrpc methods exposed by the remote server as own methods.
 * For more details see wrap_xmlrpc_method.
 * @param xmlrpc_client $client the client obj all set to query the desired server
 * @param array $extra_options list of options for wrapped code
 * @return mixed false on error, the name of the created class if all ok or an array with code, class name and comments (if the appropriatevoption is set in extra_options)
 */
function wrap_xmlrpc_server($client, $extra_options = array())
{
    $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : '';
    //$signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0;
    $timeout = isset($extra_options['timeout']) ? (int) $extra_options['timeout'] : 0;
    $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : '';
    $newclassname = isset($extra_options['new_class_name']) ? $extra_options['new_class_name'] : '';
    $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool) $extra_options['encode_php_objs'] : false;
    $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool) $extra_options['decode_php_objs'] : false;
    $verbatim_client_copy = isset($extra_options['simple_client_copy']) ? !$extra_options['simple_client_copy'] : true;
    $buildit = isset($extra_options['return_source']) ? !$extra_options['return_source'] : true;
    $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc';
    $msgclass = $prefix . 'msg';
    //$valclass = $prefix.'val';
    $decodefunc = 'php_' . $prefix . '_decode';
    $msg = new $msgclass('system.listMethods');
    $response =& $client->send($msg, $timeout, $protocol);
    if ($response->faultCode()) {
        error_log('XML-RPC: could not retrieve method list from remote server');
        return false;
    } else {
        $mlist = $response->value();
        if ($client->return_type != 'phpvals') {
            $mlist = $decodefunc($mlist);
        }
        if (!is_array($mlist) || !count($mlist)) {
            error_log('XML-RPC: could not retrieve meaningful method list from remote server');
            return false;
        } else {
            // pick a suitable name for the new function, avoiding collisions
            if ($newclassname != '') {
                $xmlrpcclassname = $newclassname;
            } else {
                $xmlrpcclassname = $prefix . '_' . preg_replace(array('/\\./', '/[^a-zA-Z0-9_\\x7f-\\xff]/'), array('_', ''), $client->server) . '_client';
            }
            while ($buildit && class_exists($xmlrpcclassname)) {
                $xmlrpcclassname .= 'x';
            }
            /// @todo add function setdebug() to new class, to enable/disable debugging
            $source = "class {$xmlrpcclassname}\n{\nvar \$client;\n\n";
            $source .= "function {$xmlrpcclassname}()\n{\n";
            $source .= build_client_wrapper_code($client, $verbatim_client_copy, $prefix);
            $source .= "\$this->client =& \$client;\n}\n\n";
            $opts = array('simple_client_copy' => 2, 'return_source' => true, 'timeout' => $timeout, 'protocol' => $protocol, 'encode_php_objs' => $encode_php_objects, 'prefix' => $prefix, 'decode_php_objs' => $decode_php_objects);
            /// @todo build javadoc for class definition, too
            foreach ($mlist as $mname) {
                if ($methodfilter == '' || preg_match($methodfilter, $mname)) {
                    $opts['new_function_name'] = preg_replace(array('/\\./', '/[^a-zA-Z0-9_\\x7f-\\xff]/'), array('_', ''), $mname);
                    $methodwrap = wrap_xmlrpc_method($client, $mname, $opts);
                    if ($methodwrap) {
                        if (!$buildit) {
                            $source .= $methodwrap['docstring'];
                        }
                        $source .= $methodwrap['source'] . "\n";
                    } else {
                        error_log('XML-RPC: will not create class method to wrap remote method ' . $mname);
                    }
                }
            }
            $source .= "}\n";
            if ($buildit) {
                $allOK = 0;
                eval($source . '$allOK=1;');
                // alternative
                //$xmlrpcfuncname = create_function('$m', $innercode);
                if ($allOK) {
                    return $xmlrpcclassname;
                } else {
                    error_log('XML-RPC: could not create class ' . $xmlrpcclassname . ' to wrap remote server ' . $client->server);
                    return false;
                }
            } else {
                return array('class' => $xmlrpcclassname, 'code' => $source, 'docstring' => '');
            }
        }
    }
}
 function testAutoRegisteredMethod()
 {
     // make a 'deep client copy' as the original one might have many properties set
     $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 1));
     if ($func == '') {
         $this->fail('Registration of examples.getStateName failed');
     } else {
         $v = $func(23);
         // work around bug in current version of phpunit
         if (is_object($v)) {
             $v = var_export($v, true);
         }
         $this->assertEquals('Michigan', $v);
     }
 }