Use XML technology to construct remote services in PHP ----------------------------------------- --------------------------------------- 2003-4-10 16:10:44 Future The Web will be a service-centric Web, XML_RPC standard makes writing and application services very simple. This article describes the XML_RPC standard and its PHP implementation, and through example demonstrates how to develop XML_RPC services and client programs in PHP. I. Service Web From the Content Provider to the Future Conception of UDDI (Universal Description, Discovery and Integration), the industry has a large number of instructions and comments on "service web". For the initial stage of the Web, it is just a distribution of a document, providing only some browsable information. With the development of the web, it is increasingly attractive to run services on the web. In the future, Web will become a carrier that provides convenient services for customers and other companies. Synergies between B2B and B2C mode can be seen as a serviced web. A very important question is what services can I provide on the web? There are many services that the Web can provide, some of which are now in use, and some services will appear in the near future. To illustrate the problem, the following lists a small part of the service available through the Web: the topic vertical search engine. User's knowledge base for user finding information. Users can ask expert systems. Bank service. News and information publishing services. Digital payment related services. Graphics processing service. Health and health services. So what is the correct way to provide services to the company and organizations through the Web? This is a very important issue. Today, some services provide HTML interfaces, which provide services in the form of documents, but what is hidden behind the service interface? In the competition of the occupation of the Web, web browsers are not alone, mobile phones, handheld devices, and microwavers and other devices want to access web, query databases, conversion data, extract information, and so on. To achieve a real service web, there should be another layer under the performance layer (HTML). Second, XML_RPC standard XML may be the most important standard in the past 10 years, and the XML vocabulary provides cornerstone for the enterprise construction service environment. To build a service web, it is necessary to learn XML_RPC standards, not only because XML_RPC is useful to put the service on the web, and because XML_RPC is a standard that has been formed, it is easy to adopt. For B2B services, the standards that provide services are extremely important, and the co-compliance companies can use the services provided by other companies to achieve rapid growth. Unable to imagine that you can build a real servic Web on a variety of private service standards, and the service must have a standard that can be followed. XML_RPC is a standard for Internet distributed processing. RPC is an abbreviation for Remote Procedure Call, which is a remote call mechanism for calling processes that may reside on other machines and may be written in other languages. Remote process call is an important pillar of distributed computing. For example, in a distributed computing environment, we can find and utilize the process of performing an add-on and subtraction operations running on other machines, performing the process of performing the addition operations may be written in an APL, running on the RS6000 machine, perform subtraction operations May be written with C, run on UNIX. Other developers who want to use this distributed calculator can also use them, or he can also use additional better calculators. In the RPC, procedure is the most important component, and the server is provided by the process for client calls. The process can receive parameters and return the result.
XML_RPC implements an RPC mechanism by sending and receiving data XML vocabulary by sending and receiving data as a protocol carrier. The XML_RPC server receives the XML_RPC request and returns an XML_RPC response, and the XML_RPC client sends an XML_RPC request and receives an XML_RPC response. Servers and customers must process responses and requests in accordance with XML_RPC standards. Third, the complete XML_RPC specification of the XML_RPC protocol can be found at http://www.xmlrpc.com/spec. Here is its point of view. 3.1 XML_RPC Request XML_RPC Request should be an HTTP POST request, and its body is an XML format. The requested XML section is as follows: XML Version = "1.0"?>
Below is a structure consisting of two elements:
The basic class implemented by this XML_RPC involves two files: xmlrpc.inc: The class XMLRPCS.inc: The class XMLRPCS.inc: The class containing XML_RPC's PHP server is required to write XML_RPC client means: 1. Create an XML_RPC request message 2. Set an XML_RPC parameter 3. Create an XML_RPC message 4. Send a message 5. Get a response 6. If you answer your answer, please 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 ().
"; Print"
".htmlentities ($ r-> serialize ())." Pre>
/ n ";} else {print" error : "; Print" code: "$ r-> faultcode ()." reason: '". $ r-> faultstring ()."'
";}?> In this example, let us create A XML_RPC message called the "Examples.getStateName" method and passes an integer parameter with a type "int" value of 14. Then we created a customer who describes the URL (path, domain, and port). Next, we send a message, receive an acknowledgment object and check an error. If there is no error, we will display the results. The main functions you want to use when writing RPC clients: $ client = new xmlrpc_client ($ server_path, $ server_hostname, $ server_port); send message method is: $ response = $ client-> send ($ xmlrpc_message ); It returns an instance of XMLRPCRESP. The message we passed is an instance of XMLRPCMSG. It is created with the following method: $ msg = new xmlrpcmsg ($ MethodName, $ ParameterArray); methodname is the name of the method (process) to be called, Parameterarray is a PHP array of XMLRPCVAL objects.For example: $ msg = new xmlrpcmsg ("eXamples.getStatename", Array (New XMLRPCVAL (23, ")))))); XMLRPCVAL object can be created in the following form: Php $ myval = new Xmlrpcval ($ stringval); $ Myval = new xmlrpcval ($ SCALARVAL, "INT" | "String" | "double" | "DateTime.iso8601" | "Base64"); $ myval = new xmlrpcval ($ ArrayVal, "Array" | "Struct ");>> The first form creates an XMLRPC string value. The second form is created a value of the description value and the type. The third form creates complex objects by combining other XMLRPC values in an array, for example: Php $ myarray = new xmlrpcval (arch xmlrpcval ("Tom"), New Xmlrpcval ("Dick"), New XMLRPCVAL ("Harry"), "array"); $ mystruct = new xmlrpcval ("name" => new Xmlrpcval ("Tom"), "age" => New XMLRPCVAL (34, "int"), "GEEK" => New XMLRPCVAL (1, "boolean")), "struct");?> Answer object is the XMLRPCRESP type, which is obtained by calling the customer object. At the server, we can create an XMLRPCRESP type object: $ RESP = New XMLRPCRESP ($ xmlrpcval); and in the client, use the following method to get Xmlrpcval: $ xmlrpcval = $ resp-> value (); Next we can use the following way to obtain the PHP variables for the response results: $ SCALARVAL = $ VAL-> Scalarval (); two functions are very useful for complex data types, both functions are in XMLRPC. Incology: $ arr = xmlrpc_decode ($ xmlrpc_val); This function returns a PHP array containing data within XMLRPCVAL variables $ XMLRPC_VAL, which has been converted into a variable type having a PHP itself. $ XMLRPC_VAL = XMLRPC_ENCODE ($ PHPVAL); This function returns an XMLRPCVAL type value, which contains the PHP data described in PHPVal. For arrays and structures, this method can perform recursive analysis. Note that there is no support for non-basic data types such as Base-64 data, or date-time data. 4.3 The server-side written service provided by XMLRPCS.Ic is very simple.
To create a service, we create an instance of XMLRPC_Server as follows: Php $ s = new xmlrpc_server ("examples.myfunc => array (" function "=>" foo ")));?> Pass The XMLRPC_Server constructor is a joint array of joint arrays. Procedure "Examples.MYFUNC" calls "foo" function, because this reason foo is called method handles. Writing method handles is simple. Here is a skeleton of a method handle: PhpFunction foo ($ params) {global $ xmlrpcerruser; // Introduction User Error Code Value // $ Params is an array of XMLRPCVAL objects IF ($ ERR) {// Error Condition Return New XMLRPCRESP (0, $ xmlrpcerruser 1, // User Error 1 "Error!");} else {// Success Return New XMLRPCRESP (New Xmlrpcval ("Fine!", "String"));}}?> When the program checks the error, if there is an error, the error is returned (starting from the $ XMLRPcerRuser 1); otherwise, if everything is normal, return XMLRPCRESP describing the operation success information. V. Application Examples In the following example we will construct a service. For a given value n, the service returns N * 2. The client uses the service to calculate the value of 5 * 2.