Construct WebService with XML-RPC

Source: Internet
Author: User
WebService is generated for the communication of heterogeneous systems. its basic idea is to use XML-based HTTP remote calls to provide a standard mechanism, this eliminates the need to establish a new protocol. Currently, two protocol standards are available for WebService communication.

Web Service introduction:Web Service is generated for the communication of heterogeneous systems. its basic idea is to use XML-based HTTP remote calls to provide a standard mechanism, this eliminates the need to establish a new protocol. 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 integrates access to both XML-RPC and SOAP protocols, both of which are concentrated in xmlrpc extensions. 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 mainly use XML-RPC to briefly describe the Web Service interaction process, part of the content from the PHP Manual, more specific content, it is recommended to refer to the manual.

Install xmlrpc extension:If your system does not have the php extension of xmlrpc installed, install it correctly. on Windows, first put the extension php_xmlrpc.dll in the PHP installation directory to C: Windows or C: under the Winnt Directory (PHP4 extensions are in the C: phpextensions Directory, PHP5 extensions are in the C: phpext directory), and C: Windowsphp. ini or C: Winntphp. 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, please be sure to install correctly.

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.

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 is as follows: rpc_server.php

  1. /**
  2. * Function: the function provided to the RPC client.
  3. * Parameters:
  4. * $ Function to be called by the method client
  5. * $ Params parameter array of the function to be called by the client
  6. * Return: return the specified call result.
  7. */
  8. Function rpc_server_func ($ method, $ params ){
  9. $ Parameter = $ params [0];
  10. If ($ parameter = "get ")
  11. {
  12. $ Return = \ 'This data by get method \';
  13. }
  14. Else
  15. {
  16. $ Return = \ 'not specify method or params \';
  17. }
  18. Return $ return;
  19. }
  20. // Generate a XML-RPC on the server side
  21. $ Xmlrpc_server = xmlrpc_server_create ();
  22. // Register a method called by the server, rpc_server, which actually points to the rpc_server_func function
  23. Xmlrpc_server_register_method ($ xmlrpc_server, "rpc_server", "rpc_server_func ");
  24. // Accept XML data POST from the client
  25. $ Request = $ HTTP_RAW_POST_DATA;
  26. // Execute the XML request to call the client and obtain the execution result
  27. $ Xmlrpc_response = xmlrpc_server_call_method ($ xmlrpc_server, $ request, null );
  28. // Output the result XML after function processing
  29. Header (\ 'content-Type: text/xml \');
  30. Echo $ xmlrpc_response;
  31. // Destroy XML-RPC server resources
  32. Xmlrpc_server_destroy ($ xmlrpc_server );
  33. ?>

After the server is constructed, construct our RPC client. The client accesses port 80 of the XML-RPC server through Socket, then encapsulate the RPC Interface to be called into XML, submit the request to the RPC server through POST, and finally obtain the returned results from the server, the code is as follows: rpc_client.php

  1. /**
  2. * Function: provides a function to the client to connect to the XML-RPC server.
  3. * Parameters:
  4. * $ Host the host to be connected
  5. * $ Port: port used to connect to the host
  6. * $ Rpc_server XML-RPC server files
  7. * $ Xml request information encapsulated by request
  8. * Return: If the connection succeeds, XML information returned by the server is returned. if the connection fails, false is returned.
  9. */
  10. Function rpc_client_call ($ host, $ port, $ rpc_server, $ request ){
  11. File: // open the specified server
  12. $ Fp = fsockopen ($ host, $ port );
  13. File: // Construct the POST request information on the XML-RPC server that requires communication
  14. $ Query = "POST $ rpc_server HTTP/1.0 User_Agent: XML-RPC Client Host :". $ host. "Content-Type: text/xml Content-Length :". strlen ($ request ). "". $ request. "";
  15. File: // send the constructed HTTP protocol to the server. if the HTTP protocol fails, false is returned.
  16. If (! Fputs ($ fp, $ query, strlen ($ query )))
  17. {
  18. $ Errstr = "Write error ";
  19. Return false;
  20. }
  21.  
  22. // Obtain all the information returned from the server, including the HTTP header and XML Information
  23. $ Contents = \'\';
  24. While (! Feof ($ fp ))
  25. {
  26. $ Contents. = fgets ($ fp );
  27. }
  28. // After the connection resource is closed, the retrieved content is returned.
  29. Fclose ($ fp );
  30. Return $ contents;
  31. }
  32. // Construct the information of the RPC server.
  33. $ Host = \ 'localhost \';
  34. $ Port = 80;
  35. $ Rpc_server = \'/~ Heiyeluren/rpc_server.php \';
  36. // Encode the XML request to be sent into XML. the method to be called is rpc_server and the parameter is get.
  37. $ Request = xmlrpc_encode_request (\ 'rpc _ server \ ', \ 'get \');
  38. // Call the rpc_client_call function to send all requests to the XML-RPC server for information
  39. $ Response = rpc_client_call ($ host, $ port, $ rpc_server, $ request );
  40. // Analyze the XML returned from the server, remove the HTTP header information, and convert the XML into a string that PHP can recognize
  41. $ Split = \' \';
  42. $ Xml = explode ($ split, $ response );
  43. $ Xml = $ split. array_pop ($ xml );
  44. $ Response = xmlrpc_decode ($ xml );
  45.  
  46. // Output the information obtained from the RPC Server
  47. Print_r ($ response );
  48. ?>

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:

  1.  
  2.  
  3. This data by get method  
  4.  
  5.  
  6.  
  7.  

Then, we can use the xmlrpc_decode function to encode the XML as a PHP string, so that we can process it at will and complete Web Service interaction.

Conclusion

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 operation, maybe it will be simpler, more suitable for your use.

Simple use of XML-RPC Web Service interaction is completed, part of the code reference PHP manual, to obtain specific information recommended reference manual, if the article is incorrect, please correct.

Related Article

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.