Using XML technology to construct remote services in PHP (below) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Use XML technology in PHP to construct remote services (below ). 4. XML_RPC-based Web services use XML_RPC to construct and use services conveniently. Enterprises deploy XML_RPC servers for various services they provide. users, customer software, and customer businesses deploy XML_RPC-based Web services.
Using XML_RPC to construct and use services is convenient. Enterprises deploy XML_RPC servers for their own services. users, customer software, and customers can use these services to construct high-end services or end-user applications. This competition to provide more effective, inexpensive, and high-quality services will greatly improve the quality of application services.

However, there are still some problems to be solved, such as how to catalog, index, and search Web services? UDDI tries to solve this problem, but this standard is not simple, and the industry's response to it is still unclear. However, the application of XML_RPC in an enterprise not only improves code reusability, but also brings a new distributed computing mode, in the next few years, it will surely become an important intellectual wealth. The development of XML_RPC started from solving the distributed computing problem and becoming the basic layer of Service Web, and thus gained a very good start. it will be followed by people's enthusiasm for this standard. Now, let's take a look at the actual application of XML_RPC!

4.1 apply XML_RPC in PHP

PHP is an ideal language for providing Web services. We only need to write the PHP code, but place it in a proper place, and immediately have a service that can be called through the URL. XML_RPC implementation in PHP may be complicated or simple, but we have many options. Here we use the XML_RPC implementation from Useful Information Company. its code and documentation can be downloaded from http://xmlrpc.usefulinc.com.

The basic class implemented by XML_RPC involves two files:

Xmlrpc. inc: class required by the php client that contains XML_RPC

Xmlrpcs. inc: class required by the php server that contains XML_RPC

4.2 client

Compiling the XML_RPC client means:

1. create an XML_RPC request message

2. set the XML_RPC parameter

3. create an XML_RPC message

4. send messages

5. get a response

6. explain the response

See the following example:

<? Php
$ F = new xmlrpcmsg ('examples. getStateName ', array (new xmlrpcval (14, "int ")));
$ C = new xmlrpc_client ("/RPC2", "betty.userland.com", 80 );
$ R = $ c-> send ($ f );
$ V = $ r-> value ();
If (! $ R-> faultCode ()){
Print "status code". $ HTTP_POST_VARS ["stateno"]. "Yes ".
$ V-> scalarval (). "<BR> ";
Print "<HR> This is the server response <BR> <PRE> ".
Htmlentities ($ r-> serialize (). "</PRE> <HR> n ";
} Else {
Print "error :";
Print "code:". $ r-> faultCode ().
"Cause: '". $ r-> faultString (). "' <BR> ";
}
?>


In this example, we first create an XML_RPC message that calls the "examples. getStateName" method, and pass an integer parameter of the type "int" value of 14. Then, we created a customer that describes the URL to be called (path, domain, and port. Next, we sent the message, received the response object, and checked for errors. If no error exists, the result is displayed.

The main functions used to write RPC client programs are as follows:

Customer creation:

$ Client = new xmlrpc_client ($ server_path, $ server_hostname, $ server_port );

The message sending method is as follows:

$ Response = $ client-> send ($ xmlrpc_message );

It returns an instance of xmlrpcresp. The message we pass is an xmlrpcmsg instance, which is created using the following method:

$ Msg = new xmlrpcmsg ($ methodName, $ parameterArray );

MethodName is the name of the method (process) to be called, and parameterArray is the php array of the xmlrpcval object. For example:

$ Msg = new xmlrpcmsg ("examples. getStateName", array (new xmlrpcval (23, "int ")));

The xmlrpcval object can be created as follows:

<? Php
$ MyVal = new xmlrpcval ($ stringVal );
$ MyVal = new xmlrpcval ($ scalarVal, "int" | "boolean" | "string" | "double" | "dateTime. iso8601" | "base64 ");
$ MyVal = new xmlrpcval ($ arrayVal, "array" | "struct ");
?>


The first form creates the xmlrpc string value. The second form creates the descriptive value and type value. The third form creates complex objects by combining other xmlrpc values in arrays and other structures, such:

<? Php
$ MyArray = new xmlrpcval (array (new xmlrpcval ("Tom"), new xmlrpcval ("Dick"), new xmlrpcval ("Harry"), "array ");
$ MyStruct = new xmlrpcval (array (
"Name" => new xmlrpcval ("Tom "),
"Age" => new xmlrpcval (34, "int "),
"Geek" => new xmlrpcval (1, "boolean"), "struct ");
?>


The response object is of the xmlrpcresp type and is obtained by calling the send method of the customer object. On the server side, we can create objects of the xmlrpcresp type as follows:

$ Resp = new xmlrpcresp ($ xmlrpcval );

On the client side, use the following method to obtain xmlrpcval from the response:

$ XmlrpcVal = $ resp-> value ();

Next, we can use the following method to obtain the PHP variable that describes the response result:

$ ScalarVal = $ val-> scalarval ();

Two functions are very useful for complex data types, both of which are in xmlrpc. inc:

$ Arr = xmlrpc_decode ($ xmlrpc_val );

This function returns a PHP array containing the data in the xmlrpcval variable $ xmlrpc_val, which has been converted to the variable type of PHP.

$ Xmlrpc_val = xmlrpc_encode ($ phpval );

This function returns a value of the xmlrpcval type, which contains the PHP Data described by $ phpval. This method can perform recursive analysis on arrays and structures. Note that there is no support for non-basic data types (such as base-64 data or date-time data.

4.3 Server

Using the classes provided by xmlrpcs. inc to write services is very simple. To create a service, create an xmlrpc_server instance as follows:

<? Php
$ S = new xmlrpc_server (array ("examples. myFunc" =>
Array ("function" => "foo ")));
?>

The consortium array passed to the xmlrpc_server constructor is a consortium array. The process "examples. myFunc" calls the "foo" function. For this reason, foo is called a method handle.

Writing method handles is simple. The following is the skeleton of a method handle:

<? Php
Function foo ($ params ){
Global $ xmlrpcerruser; // introduce the user error code value
// $ Params is an array of xmlrpcval objects
If ($ err ){
// Error conditions
Return new xmlrpcresp (0, $ xmlrpcerruser + 1, // User Error 1
"Error! ");
} Else {
// Succeeded
Return new xmlrpcresp (new xmlrpcval ("Fine! "," String "));
}
}
?>


As you can see, the program has checked the error. If an error exists, an error is returned (starting from $ xmlrpcerruser + 1). otherwise, xmlrpcresp indicating successful operations is returned if everything is normal.

V. application instances
In the following example, we construct a service. For the given value n, the service returns n * 2. The client uses this service to calculate the value of 5x2.

The server code is as follows:

<? Php
Include ("xmlrpc. inc ");
Include ("xmlrpcs. inc ");
Function foo ($ params)
{
Global $ xmlrpcerruser; // introduce the user error code value
// $ Params is an array of the xmlrpcval object
$ Vala = $ params-> params [0];
$ Sval = $ vala-> scalarval ();
$ Ret = $ sval * 2;
Return new xmlrpcresp (new xmlrpcval ($ ret, "int "));
}
$ S = new xmlrpc_server (array ("product" =>
Array ("function" => "foo ")));
?>


The client code is as follows:

<? Php
Include ("xmlrpc. inc ");
If ($ HTTP_POST_VARS ["number"]! = ""){
$ F = new xmlrpcmsg ('product', array (new xmlrpcval ($ HTTP_POST_VARS ["number"], "int ")));
$ C = new xmlrpc_client ("/xmlrpc/servfoo. php", "luigi.melpomenia.com. ar", 80 );
$ C-> setDebug (0 );
$ R = $ c-> send ($ f );
$ V = $ r-> value ();
If (! $ R-> faultCode ()){
Print "Number". $ HTTP_POST_VARS ["number"]. "is ".
$ V-> scalarval (). "<BR> ";
Print "<HR> result from server! <BR> <PRE> ".
Htmlentities ($ r-> serialize (). "</PRE> <HR> n ";
} Else {
Print "operation failed :";
Print "code:". $ r-> faultCode ().
"Cause: '". $ r-> faultString (). "' <BR> ";
}
}
Print "<form method =" POST ">
<Input name = "number" VALUE = "$ {number}">
<Input type = "submit" value = "go" name = "submit"> </FORM> <P>
Enter a value ";
?>


Conclusion: the operation of the XML_RPC service also involves many other infrastructure and basic work, such as the cataloguing and indexing mechanism of distributed processes, and the better interface for processing XML_RPC in programming languages. There are many reports on XML_RPC and service Web. let's keep a close eye on their development!

Using XML_RPC to construct and use services is convenient. Enterprises deploy XML_RPC servers for various services they provide. users, customer software, and customer enterprises will...

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.