Using XML-RPC constructs WebService

Source: Internet
Author: User
Tags soap pear phpinfo strlen

[Web Service Introduction]

Web Service is created for the communication of heterogeneous systems, and its basic idea is to use an xml-based HTTP remote invocation to provide a standard mechanism that eliminates the need to establish a new protocol. There are two protocol standards for Web service communication, one is XML-RPC, the other is soap. XML-RPC is simpler, occurs earlier, and soap is more complex, and is mainly used when there is a need for stability, robustness, security, and complex interactions.

PHP integrates XML-RPC and SOAP Two protocol access, is concentrated in the xmlrpc extension. Also, in PHP's pear, PHP 4 or PHP 5, the XML-RPC extension has been integrated by default, and the extension is independent of the XMLRPC extension to enable XML-RPC protocol interaction, and if there is no xmlrpc extension, it is recommended to use pear:: XML-RPC extensions.

We mainly use XML-RPC to briefly describe the interactive process of Web service, part of the content from the PHP manual, more specific content, suggested reference manual.

[Install XMLRPC extension]

If you do not have a xmlrpc PHP extension installed on your system, install it correctly.

Under Windows platform, the extended php_xmlrpc.dll in the PHP installation directory is first placed under the C:windows or c:winnt directory (PHP4 extension is in the C:phpextensions directory, and PHP5 is extended in C: Phpext directory), at the same time C:windowsphp.ini or c:winntphp.ini the semicolon in front of Extension=php_xmlrpc.dll ";" Remove and then reboot the Web server to see if Phpinfo () has an XML-RPC project to determine if the XMLRPC extension has been properly installed.

Under the Unix/linux platform, if the XMLRPC extension is not installed, please recompile PHP, add the--WITH-XMLRPC option when configure, and then View Phpinfo () to see if the XMLRPC is installed normally.

(Attention: The following operations are based on the XMLRPC expansion of the normal installation premise, please be sure to install correctly.) )

  [XML-RPC working principle]

XML-RPC is basically the whole process of communicating by using the xml-based. The client then analyzes XML to obtain the data it needs by constructing an RPC server-side request to use XML encapsulation from the RPC client and returning the processing result to the RPC client in the form of XML.

The server side of XML-RPC must have ready-made 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 results will not be obtained.

Let me do a simple code to describe the whole process.

  [XML-RPC practice]

The server side uses the Xmlrpc_server_create function to generate a server-side, then registers the RPC calling interface that needs to be exposed, accepts the XML data that the RPC client post, and then processes the results, which are displayed to the client in XML form.

The code is as follows: rpc_server.php

* Functions: Functions supplied to RPC client calls
* $method functions that the client needs to call
* $params parameter array of functions that the client needs to call
* Returns: Returns the specified call result
function Rpc_server_func ($method, $params) {
$parameter = $params [0];
if ($parameter = = "Get")
$return = \ ' This data by Get method\ ';
$return = \ ' Not specify method or params\ ';
return $return;

Generate an XML-RPC server side
$xmlrpc _server = Xmlrpc_server_create ();

Registers a server-side called method Rpc_server, which actually points to the Rpc_server_func function
Xmlrpc_server_register_method ($xmlrpc _server, "Rpc_server", "Rpc_server_func");

Accept the XML data that the client post.
$request = $HTTP _raw_post_data;

Gets execution results after executing an XML request that invokes the client
$xmlrpc _response = Xmlrpc_server_call_method ($xmlrpc _server, $request, NULL);

Output the result XML after the function is processed
Header (\ ' content-type:text/xml\ ');
echo $xmlrpc _response;

Destroying XML-RPC server-side resources
Xmlrpc_server_destroy ($xmlrpc _server);

The server-side constructs, then constructs our RPC client. The client accesses the 80 port on the XML-RPC server side roughly through the socket, and then encapsulates the RPC interface that needs to be invoked into XML, submits it to the RPC server side through a POST request, and finally gets the server-side return result.

The code is as follows: rpc_client.php

* Functions: Functions provided to clients to connect XML-RPC server-side
* $host hosts that need to be connected
* $port Port connecting the host
* $RPC _server XML-RPC server-side files
* $request encapsulated XML Request information
* Return: The connection successfully returned the XML information returned by the server side and failed to return false
function Rpc_client_call ($host, $port, $rpc _server, $request) {

FILE://Open the specified server side
$fp = Fsockopen ($host, $port);

file://constructs XML-RPC server-side query Post request information that requires communication
$query = "POST $rpc _server http/1.0 user_agent:xml-rpc Client Host:". $host. " Content-type:text/xml content-length: ". strlen ($request)." ". $request." ";

FILE://sends the constructed HTTP protocol to the server and fails back to false
if (!fputs ($fp, $query, strlen ($query))
$ERRSTR = "Write error";
return false;

Gets all the information returned from the server side, including HTTP headers and XML information
$contents = \ ';
while (!feof ($FP))
$contents. = Fgets ($FP);

Returns the acquired content when the connection resource is closed
Fclose ($FP);
return $contents;

To construct the information connected to the RPC server side
$host = \ ' localhost\ ';
$port = 80;
$rpc _server = \ '/~heiyeluren/rpc_server.php\ ';

To encode XML requests that need to be sent into XML, the method to call is Rpc_server, and 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 to get information
$response = Rpc_client_call ($host, $port, $rpc _server, $request);

Analyze XML returned from server side, remove HTTP header information, and convert XML to a string that is recognized by PHP

$split = \ <?xml version= "1.0" encoding= "iso-8859-1" >\ ";
$xml = Explode ($split, $response);
$xml = $split. Array_pop ($xml);
$response = Xmlrpc_decode ($xml);

Output information obtained from the RPC server side
Print_r ($response);


The above example is to submit a method called Rpc_server in the past, the parameter is get, and then obtain the server-side return, the server-side returned XML data is:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<string>this data by Get method</string>

So we can then encode this XML as a PHP string via the Xmlrpc_decode function, and we'll be able to do it at will, with the entire Web service interacting.


Whether it's XML-RPC or SOAP, the entire Web service is successful as long as it allows us to make a stable and secure call to the remote process and complete our project. In addition, if you can, you can try using XML-RPC in pear to achieve similar operations above, it might be simpler and more suitable for you to use.

Simple use of XML-RPC for Web service interaction is complete, some code reference PHP manual, want to get specific information suggestions reference manual, if the article is incorrect, please correct me.

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