Notes for using XML-RPC in PHP

Source: Internet
Author: User
: This article mainly introduces the use of PHP XML-RPC notes, for PHP tutorials interested in students can refer. PHP integrates two protocol standards: XML-RPC and SOAP for Web Service communication. The basic idea is to use XML-based HTTP remote call to provide a standard mechanism, this eliminates the need to establish a new protocol. In fact, this is very practical in the actual development and application, such as PC client or now popular mobile client needs to communicate with the server, this time XML-RPC is a good solution.

Here let's take notes on how to use XML-RPC in PHP, though this method is not used much.

The basic principle is that the XML-RPC uses 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.

First make sure that your PHP supports XML-RPC extension, if not, please install, windows php_xmlrpc.dll put in your PHP extension directory, Linux re-compile PHP, in configure, add the-with-xmlrpc option, because I am using Ubuntu, so just run the sudo apt-get install php5-xmlrpc.

The server segment code is as follows, with detailed comments:


/* Server. php * @ function the function * @ param string $ method the function that the client needs to call * @ param array $ array of parameters of the function to be called by the params client * return string 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;} // the server that generates the XML-RPC $ xmlrpc_server = xmlrpc_server_create (); // register a method called by the server, rpc_server, which actually points 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; // Obtain the execution result after executing the XML request of the client call $ 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 );

/* Client. php * @ function provides the client with a function to connect to the XML-RPC server * @ param string $ host to be connected * @ param string $ port to connect to the host * @ param string $ rpc_server XML-RPC the XML request information encapsulated by the server file * @ param $ request * return returns the XML information returned by the server, false */function rpc_client_call ($ host, $ port, $ rpc_server, $ request) is returned for failure {// open the specified server side $ 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: X ML-RPC Client \ nHost :". $ host. "\ nContent-Type: text/xml \ nContent-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 = 'server. php '; // Encode the XML request to be sent into XML. the method to call 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 );

The XML is encoded as a PHP string through the xmlrpc_decode function for processing, and the Web Service interaction is complete.

XML-RPC function reference:

  • Xmlrpc_decode_request-decodes XML into the PHP type
  • Xmlrpc_decode-decodes XML into the PHP type
  • Xmlrpc_encode_request-generate XML for PHP values
  • Xmlrpc_encode-generate XML for PHP values
  • Xmlrpc_get_type-obtain the xmlrpc type for the PHP value
  • Xmlrpc_is_fault-Determines if an array value represents an XMLRPC fault
  • Xmlrpc_parse_method_descriptions-decodes XML into a list of Method descriptions
  • Xmlrpc_server_add_introspection_data-add self-description document
  • Xmlrpc_server_call_method-call methods when parsing XML requests
  • Xmlrpc_server_create-create an xmlrpc server
  • Xmlrpc_server_destroy-destroy server resources
  • Xmlrpc_server_register_introspection_callback-register a PHP function to generate a document
  • Xmlrpc_server_register_method-register a PHP function to match the xmlrpc method name.
  • Xmlrpc_set_type-set the xmlrpc type, base64, or datetime for a PHP string value

  • The above introduces the use of PHP XML-RPC notes, including the content of the aspect, hope to be interested in PHP Tutorial friends help.

    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.