Tutorials to .com

Tutorials to .com » Php » Xml » The use of XML in PHP remote service technology structure (Lower)

The use of XML in PHP remote service technology structure (Lower)

Print View , by: iSee ,Total views: 3 ,Word Count: 2426 ,Date: Thu, 16 Apr 2009 Time: 1:32 AM

IV XML_RPC the Web-based services <br> use XML_RPC construction and use of services is very convenient. Enterprises for their own deployment of the various services available to XML_RPC servers, users, client software and client companies can use this service or the structure of high-end services for end-user applications. This provide more effective, cheap and quality services to competition will greatly improve the quality of application services.

But here there are still some issues to be resolved, such as how to catalog, index, search services on the Web? UDDI attempts to solve this problem, but this standard is not simple, but the industry's response to it still do not understand. However, in Enterprise applications XML_RPC not only to improve the reusability of code and which will create a new model of distributed computing, the next few years it will become an important wealth of knowledge. XML_RPC the development of distributed computing to solve the problem as well as a Web service to start the basic level in order to obtain a very good start, then people will bear with the enthusiasm of the standards. That being the case, let us now take a look at the practical application of XML_RPC it!

4.1 in the php application XML_RPC

To provide Web services, PHP is a very good language. We just need to prepare a good PHP code, however put it a suitable location, it can immediately have a URL "call" services. PHP in XML_RPC complex may also be possible to achieve simple, but we have many options. Here, we selected from the Useful Information Company to achieve XML_RPC, its code and documentation can be downloaded from the http://xmlrpc.usefulinc.com/.

XML_RPC to achieve this involves two basic categories of documents:

xmlrpc.inc: the php include XML_RPC client need category

xmlrpcs.inc: contains the php server XML_RPC required category

4.2 Client

XML_RPC the preparation of the client means:

1. XML_RPC to create a request message

2. XML_RPC parameter settings

3. XML_RPC to create a message

4. Send a message

5. To obtain response

6. To explain response

See the following examples:

<? 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's response <BR> <PRE>".
htmlentities ($ r-> serialize ()). "</ PRE> <HR> \ n";
) Else (
print "Error:";
print "code:". $ r-> faultCode ().
"The reason: '". $ R-> faultString ()."'< BR> ";
)
?>

In this example, we created a call to "examples.getStateName" news XML_RPC methods and delivery of a type "int" integer value of 14 parameters. Then, we have created a description to be called URL (path, domain and port) customers. Next, we sent a message to receive the object and check for error response. If there is no error, we will show results.

RPC client when preparing to use the main function is as follows:

Create customer:

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

Way is to send a message:

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

It is xmlrpcresp return of a case in point. Our news is delivered xmlrpcmsg examples of ways to create it as follows:

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

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

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

xmlrpcval the form of objects 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 is to create the string value xmlrpc. The second form is created to describe the value and type of value. The third category in the structure of an array of other xmlrpc value in the portfolio to create complex objects, such as:

<? 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");
?>

Xmlrpcresp response is the type of object by calling the send method of customer access. The server side, we can create xmlrpcresp through the following types of objects:

$ resp = new xmlrpcresp ($ xmlrpcval);

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

$ xmlrpcVal = $ resp-> value ();

Then we can obtain in this way with the following response to the results described in the PHP variables:

$ scalarVal = $ val-> scalarval ();

For complex data types, there are two very useful functions, these two functions are xmlrpc.inc within:

$ arr = xmlrpc_decode ($ xmlrpc_val);

The function returns a PHP array, which contains $ xmlrpc_val variables xmlrpcval of the data has been converted into PHP variables with the type itself.

$ xmlrpc_val = xmlrpc_encode ($ phpval);

Xmlrpcval the function returns a value type, which includes a $ phpval the PHP data description. And structure of the array, this method can be recursive analysis. Note that this does not exist for non-basic data types (such as base-64 data, or the date - time data) support.

4.3 server-side

Xmlrpcs.inc use services provided by type of preparation is very simple. To create a service, we create xmlrpc_server in accordance with the following examples:

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

Xmlrpc_server passed to the constructor is a joint co-array of array. The process of "examples.myFunc" call "foo" function, for this reason called the method foo handle.

Handle the preparation method is very simple. Is one way to handle the following skeleton:

<? Php
function foo ($ params) (
global $ xmlrpcerruser; / / the introduction of user error code value
/ / $ Params is an array object xmlrpcval
if ($ err) (
/ / Error conditions
return new xmlrpcresp (0, $ xmlrpcerruser +1, / / user error 1
"Error!");
) Else (
/ / Success
return new xmlrpcresp (new xmlrpcval ( "Fine!", "string"));
)
)
?>

Can see that the procedure to check the error, if error there is an error is returned (from the beginning of $ xmlrpcerruser +1); Otherwise, if everything is normal, then return to describe the operation of a successful information xmlrpcresp. [AutoPage]

V. Application <br> this example below we will construct a service. For a given value n, service return n * 2. Use of the services the client computing the value of 5 * 2.

Server-side code is as follows:

<? Php
include ( "xmlrpc.inc");
include ( "xmlrpcs.inc");
function foo ($ params)
(
global $ xmlrpcerruser; / / the introduction of user error code value
/ / $ Params is an array object xmlrpcval
$ 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 ")));
?>

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> the results from the server! <BR> <PRE>".
htmlentities ($ r-> serialize ()). "</ PRE> <HR> \ n";
) Else (
print "failed:";
print "code:". $ r-> faultCode ().
"The reason: '". $ 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: XML_RPC services also involves many other infrastructure and basic work, such as distributed process of cataloging and indexing mechanisms, and as dealt with in the programming language interface XML_RPC better. XML_RPC and service on the Web are many reports, let us pay close attention to their development it!


Php XML Application Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.