Use XML technology to construct remote services in PHP (data transmission)

xiaoxiao2021-03-06  80

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: Examples.getStateName 41 Specifies where the URL sent to the data is not specified here. If the server is dedicated to RPC processing, it may be "/". The payload in the above XML document is a "MethodCall" structure. MethodCall must contain a "methodname" sub-element, "methodname" sub-element contains a string describing the method to be called. How to explain the content of "MethodName" is completely determined by the server, for example it can be an executable file name, which can be the name recorded in the database, or anything else. If the process receives parameters, "MethodCall" can include a "params" element and several "param" sub-elements. Each "param" element contains a value with a type descriptor. Type descriptors are shown in the following table: Tag Description or four-byte symbol integer, such as 12 0 (false) , Or 1 (True) string, such as "Hello World" Double precision band symbol floating point, such as -12.214 Date / time, such as 19980717T14: 08: 55 base64 encoding Binary data, such as EW91IGBID0IHJLQGDGHPCYE? 3.1.1 Structure value can be a structure, the structure is described in the element. Each contains multiple , each contains a and one .

Below is a structure consisting of two elements: name member1 Member2 19 can nested, any can include or any other type, including . 3.1.2 Array Values ​​can be an array type, an array with element description. Each element contains a element, elements can contain any multiple elements. The following is an example of array elements: 0 9 Hello elements have no name. As shown in the previous example, the value of the element can be various types. element can nested, any can include or other type, as described above . 3.2 XML_RPC Answer XML_RPC Answer is an HTTP response, and the content type is Text / XML. The format of the response text is as follows: Abcdefg may include a structure, or may contain a structure, which is determined by the process call.

Structure, like XML request, element syntax is as follows: faultcode 4 faultstring error! Fourth, XML_RPC-based Web services use XML_RPC constructors and use services. Enterprises deploy XML_RPC servers, users, customer software, and customer enterprises to use this service to construct high-end services or end users. This competition that provides more effective, cheap and quality services will greatly improve the quality of application services. But there are still some problems to be resolved, such as how to catalog, index, search for services on the web? UDDI tries to solve this problem, but this standard is not simple, and the industry has not yet known to its reaction. However, the application of XML_RPC within the enterprise can not only improve the reusability of code, but also brings a new distributed computing mode, which will become an important knowledge wealth in the years later. The development of XML_RPC starts with a solution of distributed computing problems and the basic levels that become a servic Web, which will have followed people to follow the criteria. In this case, let's take a look at the actual application of XML_RPC! 4.1 Applying XML_RPC in PHP For web services, PHP is a very ideal language. We only need to write a good PHP code, however, put it in a suitable location, and immediately has a service that can be "called" through the URL. The XML_RPC implementation in PHP may be complicated or simple, but we have many options. Here we choose the XML_RPC implementation from the useful information company, its code and documentation can be downloaded from http://xmlrpc.usefulinc.com/.

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: send ($ f); $ V = $ r-> value (); if (! $ r-> faultcode ()) {print "Status Code". $ http_post_vars ["stateno"]. "Yes". $ V-> scalarval ().
"; Print"


This is the server's response
 ".htmlentities ($ r-> serialize ())."  
/ 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: > 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: 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: 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: 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.

转载请注明原文地址:https://www.9cbs.com/read-87767.html

New Post(0)