Xml-rpc using notes in PHP

Source: Internet
Author: User
PHP integrates the XML-RPC and soap two kinds of Web Service Communication protocol standard, the basic idea is to use XML-based HTTP remote invocation to provide a standard mechanism, and eliminate the need to establish a new protocol. In fact, this is very useful in the actual development of applications, such as PC clients or now popular mobile phone clients need to communicate with the server, this time Xml-rpc is a good solution.

Here's a note on how to use XML-RPC notes in PHP, although this approach is not very much used.

The basic principle is that XML-RPC uses XML to communicate. The client then parses the XML to get the data it needs by constructing an XML-encapsulated request that is used by the RPC server to come out from the RPC client and return the processing results to the RPC client in XML form. The server side of the XML-RPC must have out-of-the-box functions available to client calls, and the functions and methods in the request submitted by the client must be consistent with the server side, otherwise the desired result will not be obtained.

First of all to ensure that your PHP support XML-RPC extension, if not can please install, Windows Php_xmlrpc.dll put into your PHP extension directory, Linux recompile PHP, in configure time please add –WITH-XMLRPC option , because I use Ubuntu, so direct sudo apt-get install PHP5-XMLRPC on it.

The server segment code is as follows, comments in detail:


/* server.php* @function provide the function called to the RPC client * @param   string  $method The function that the client needs to call * @param   array   $params The client needs to call the function's parameter array * return   string to  return the 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 XML-RPC 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 the client post XML data $request = $HTTP _raw_ post_data;//executes the call to the client after the XML request gets the execution result $xmlrpc_response = Xmlrpc_server_call_method ($xmlrpc _server, $request, NULL);// The output header (' Content-type:text/xml ') of the result XML after the function is processed; Echo $xmlrpc _response;//destroys the XML-RPC server-side resource Xmlrpc_server_destroy ($ Xmlrpc_server);

/* client.php* @function provide the client with a function to connect to the XML-RPC server * @param string $host the host that needs to be connected * @param string $port the port to which the host is connected * @p Aram string $rpc _server xml-rpc Server-side file * @param $request encapsulated XML Request information * Return successfully returns XML information returned by server side, failed to return false*/function RP C_client_call ($host, $port, $rpc _server, $request) {//Open the specified server-side $fp = Fsockopen ($host, $port);// Constructs the XML-RPC server-side query post request information that needs to be communicated $query = "Post $rpc _server http/1.0\nuser_agent:xml-rpc client\nhost:". $host. "\ncontent-type:text/xml\ncontent-length:". Strlen ($request). "\ n". $request. "\ n";//sends the constructed HTTP protocol to the server, failing to return Falseif (!fputs ($fp, $query, strlen ($query))) {$errstr = "Write error"; return false;} Gets all information returned from the server, including HTTP headers and XML information $contents = ""; while (!feof ($fp)) {$contents. = fgets ($fp);} Returns the obtained content after closing the connection resource fclose ($FP); return $contents;} Constructs the connection RPC server side information $host = ' localhost '; $port = n; $rpc _server = ' server.php ';//encode the XML request to be sent to XML, the method to be called is Rpc_server, The parameter is Get$request = Xmlrpc_encode_request (' rpc_server ', ' get ');//Call rpc_client_call function to send all requests to XML-RPCServer-side Access information $response = Rpc_client_call ($host, $port, $rpc _server, $request);//Parse the XML returned from the server side, removing the HTTP header information, and the XML to PHP can recognize the string $split = ""; $xml = Explode ($split, $response); $xml = $split. Array_pop ($xml); $response = Xmlrpc_decode ($xml);//output information obtained from the RPC server Print_r ($response);

This XML is encoded as a PHP string for processing through the Xmlrpc_decode function, and the entire Web service interaction completes.

Xml-rpc Function Reference:

  • xmlrpc_decode_request-decoding XML as the type of PHP itself
  • xmlrpc_decode-decoding XML as the type of PHP itself
  • xmlrpc_encode_request-generating XML for PHP values
  • xmlrpc_encode-generating XML for PHP values
  • Xmlrpc_get_type-gets the type of XMLRPC for the value of PHP
  • Xmlrpc_is_fault-determines If an array value represents an XMLRPC fault
  • xmlrpc_parse_method_descriptions-decoding XML into a list of method descriptions
  • xmlrpc_server_add_introspection_data-adding a self-describing document
  • xmlrpc_server_call_method-parsing XML Request Simultaneous call method
  • xmlrpc_server_create-Creating a XMLRPC server
  • xmlrpc_server_destroy-destroying service-side resources
  • xmlrpc_server_register_introspection_callback-registering 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 Xmlrpc type, base64, or DateTime for a PHP string value

  • The above describes the PHP xml-rpc use notes, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.