PHP WebService instance construction using XML-RPC tutorial

Source: Internet
Author: User
This article mainly introduces the use of PHP XML-RPC to construct WebService, the need of friends can refer to the next I. Overview:

At present, there are two protocol standards for Web Service communication, one is XML-RPC, the other is SOAP. XML-RPC is relatively simple, the appearance of time is relatively early, SOAP is more complex, mainly in some need of stability, robustness, security and complex interaction when using.

PHP itself integrates XML-RPC and SOAP protocol access, are concentrated in xmlrpc extension. In addition, in php pear, whether PHP 4 or PHP 5, has been integrated with the XML-RPC extension by default, and the extension has nothing to do with xmlrpc extension, can independently implement XML-RPC protocol interaction, if there is no xmlrpc extension, we recommend using the PEAR: XML-RPC extension.

Here we are mainly to XML-RPC to briefly describe the Web Service interaction process, part of the content from the PHP Manual, more detailed content, readers can refer to the manual.

II. install xmlrpc extension:

If the php extension of xmlrpc is not installed in your system, install it correctly.

On the Windows platform, first put the extension php_xmlrpc.dll under the PHP installation directory under the C: \ Windows or C: \ Winnt Directory (the extension of PHP4 is in the C: in the \ php \ extensions directory, PHP5 extensions are in the C: \ php \ ext directory. The specific installation directory of the dll extension file depends on your php installation directory. here is only an example description. ini or C: \ Winnt \ php. remove the semicolon ";" in front of extension = php_xmlrpc.dll in ini, and restart the Web server and check if phpinfo () has a XML-RPC project to determine if the xmlrpc extension has been correctly installed.

On Unix/Linux platforms, if the xmlrpc extension is not installed, re-compile PHP and add the -- with-xmlrpc option to configure, and view phpinfo () check whether xmlrpc is installed properly.

(Note: The following operations are based on the normal installation of xmlrpc expansion. Be sure to install them correctly .)

Three, XML-RPC working principle:

XML-RPC is roughly the whole process is to use XML for communication. First, construct an RPC server for sending XML-encapsulated requests from the RPC client, and return the processing results to the RPC client in XML format, the client analyzes XML to obtain the data it needs.

The server side of the XML-RPC must have a ready-made function provided to the client to call, and the function and method in the request submitted by the client must be consistent with the server side, otherwise the desired result cannot be obtained.

The following is a simple code to describe the entire process.

4. XML-RPC practice:

The server uses the xmlrpc_server_create function to generate a server, register the RPC calling interface to be exposed, accept the XML data POST from the RPC client, and process it, the processing result is displayed to the client in XML format.

The code of the rpc_server.php file is as follows:

/*** Function: the function * parameter provided to the RPC client. * $ method: array of parameters of the function to be called by the client * $ params: returns the specified call result */function rpc_server_func ($ method, $ params) {$ parameter = $ params [0]; if ($ parameter = "get ") {$ return = ''This data by get method '';} else {$ return = ''not specify method or params'';} return $ return ;} // Generate a XML-RPC of the server side $ xmlrpc_server = xmlrpc_server_create (); // register a server side call method rpc_server, actually pointing to the rpc_server_func function xmlrpc_server_register_method ($ xmlrpc_server, "rpc_server", "rpc_server_func"); // accept XML data from the client POST $ request = $ HTTP_RAW_POST_DATA; // execute the XML request to call the client and obtain the execution result $ xmlrpc_response = xmlrpc_server_call_method ($ xmlrpc_server, $ request, null ); // output the result XML after function processing (''content-Type: text/xml''); echo $ xmlrpc_response; // destroy the XML-RPC server-side resource xmlrpc_server_destroy ($ xmlrpc_server );

Now the server has been constructed, and then our RPC client is constructed. The client accesses port 80 of the XML-RPC server through Socket, and then encapsulates the RPC Interface to be called into XML, submits the request to the RPC server through POST, and finally obtains the returned result from the server.

The code of the rpc_client.php file is as follows:

/*** Function: provided to the client to connect to the XML-RPC server * parameter: * $ host the host to be connected * $ port the port to connect to the host * $ rpc_server XML-RPC server-side file * $ XML request information encapsulated by request * returned: if the connection succeeds, XML information returned by the server is returned. if the connection fails, false */function rpc_client_call ($ host, $ port, $ rpc_server, $ request) is returned) {// open the specified server $ fp = fsockopen ($ host, $ port ); // Construct the query POST request information for the XML-RPC server that requires communication $ query = "POST $ rpc_server HTTP/1.0 \ nUser_Agent: XML-RPC Client \ nHost :". $ host. "\ nContent-Type: text/xml \ nCon Tent-Length :". strlen ($ request ). "\ n ". $ request. "\ n"; // send the constructed HTTP protocol to the server, and falseif (! Fputs ($ fp, $ query, strlen ($ query) {$ errstr = "Write error"; return false ;}// gets all the information returned from the server, including HTTP header and XML information $ contents = '''; while (! Feof ($ fp) {$ contents. = fgets ($ fp);} // after the connection resource is closed, the obtained content fclose ($ fp) is returned; return $ contents ;} // Construct the information of the RPC Server. $ host = ''localhost''; $ port = 80; $ rpc_server = ''/~ Heiyeluren/rpc_server.php ''; // Encode the XML request to be sent into XML. the method to be called is rpc_server, the parameter is get $ request = xmlrpc_encode_request (''rpc _ server'', ''get ''); // call the rpc_client_call function to send all requests to the XML-RPC server side and get the information $ response = rpc_client_call ($ host, $ port, $ rpc_server, $ request ); // analyze the XML returned from the server, remove the HTTP header information, and convert the XML into a string recognized by PHP. $ split = '''; $ xml = explode ($ split, $ response); $ xml = $ split. array_pop ($ xml); $ response = xmlrpc_decode ($ xml); // outputs the information print_r ($ response) obtained from the RPC server );

In general, the above example is to submit a method called rpc_server. the parameter is get, and then get the server-side response. the XML data returned by the server is:

<?xml version="1.0" encoding="iso-8859-1"?>
 
  
   
   
    
     This data by get method
    
   
  
 

Then, we can use the xmlrpc_decode function to encode the XML as a PHP string, and then we can process it at will. at this point, the whole Web Service interaction is complete.

V. Summary:

Whether it is XML-RPC or SOAP, as long as we can make stable and secure remote process calls, to complete our project, then even the whole Web Service is successful. In addition, if you can, you can also try to use the XML-RPC in PEAR to achieve the above similar operations, maybe more simple, more suitable for your use. Interested readers can try to do it.

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.