Xmlrpc-epi PHP Package (1)

Source: Internet
Author: User
Tags definition functions lowercase mul php script client
xml| Package

In PHP itself with a C-language implementation of the XMLRPC extension, called Xmlrpc-epi. Because it is implemented in C, it is much faster than the XMLRPC extensions that are implemented in PHP. But in practical applications it is found that very few people use this extension, most of them are implemented with PHP script XMLRPC extensions. The reason may have the following two points: first, this extension needs to be opened on the server, and if there is no server operation permissions, it is impractical to use this extension. The second reason is that the extensions are too small in the PHP manual, and the functions provided are very basic and cumbersome to use. For the second question, I encapsulated the extension, encapsulating it into 3 classes: Xmlrpc_error, Xmlrpc_client, and Xmlrpc_server. Of course, the most important is the latter two categories, namely Xmlrpc_client and Xmlrpc_server. These two classes greatly simplify the steps for creating XMLRPC clients and servers.

Here are some examples of how easy it is to create XMLRPC servers and clients now.

Server-side code
Download: server.php
<?php
Require_once (' class_xmlrpc.php ');

function Add ($method, $params) {
return $params [0] + $params [1];
}
function Sub ($method, $params) {
return $params [0]-$params [1];
}
function Mul ($method, $params) {
return $params [0] * $params [1];
}
function Div ($method, $params) {
return $params [0]/$params [1];
}

$xmlrpc _server = new Xmlrpc_server ();
$xmlrpc _server->register_method ("Math.add", "add");
$xmlrpc _server->register_method ("Math.sub", "sub");
$xmlrpc _server->register_method ("Math.mul", "Mul");
$xmlrpc _server->register_method ("Math.div", "Div");
$xmlrpc _server->call_method ();
?>
Client code
Download: client.php
<?php
Require_once (' class_xmlrpc.php ');

$xmlrpc _client = new Xmlrpc_client (' server.php ', ' Math ');
$a = 100;
$b = 20;
echo "\ $a = $a; \ $b = $b <br/> ";
Echo ' $a + $b = '. $xmlrpc _client->add ($a, $b). ' <br/> ';
Echo ' $a-$b = '. $xmlrpc _client->sub ($a, $b). ' <br/> ';
Echo ' $a * $b = '. $xmlrpc _client->call (' Mul ', $a, $b). ' <br/> ';
echo ' $a/$b = '. $xmlrpc _client->invoke (' Math.div ', $a, $b). ' <br/> ';
?>
Although the above code is very simple, but there are two points to be aware of the place.

First, the Xmlrpc method is to support the name space (namespace), in order to simplify the call--omitting the name space before the method, we give the parameter "Math" of a namespace when we initialize the $xmlrpc _client, This allows you to omit the namespace prefix if you invoke the method directly by using the method name or by calling. If you want to change the name space, just give $xmlrpc _client->namespace to assign a value on it. If this is a temporary change, it can also be invoked by invoking the full name (that is, a method name with a namespace).

Second, the method of Xmlrpc and PHP5 is case-sensitive, and the PHP4 method is case-insensitive, and in PHP4, all functions or method names are stored in lowercase, so either add or Sub or the last method they call is lowercase. So PHP5 can directly invoke the method with uppercase letters in the definition, but not in the PHP4. That is, if you want to access the Xmlrpc method directly through the method name in the PHP4, you must ensure that the Xmlrpc method name is lowercase when defined, or it will produce an error that cannot find the corresponding method. What if you want to call a Xmlrpc method in PHP4 that has uppercase letters in the definition? In fact, it is very simple, call method calls on it, the first parameter is the name of the method to call the string, this string is case-sensitive. Also can invoke method to call, the different place is if the name space, need to write out clearly.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.