Using XML-RPC to build WebService in PHP

Source: Internet
Author: User
Tags manual ini soap pear phpinfo

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 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: phpextensions Directory, PHP5 extension in the C: phpext directory), and in the 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. Be sure to install them 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

<? Php
/**
* Function: the function provided to the RPC client.
* Parameters:
* $ Function to be called by the method client
* $ Params parameter array of the function to be called by the client
* Return: return 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 on the server side
$ 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 POST from the client
$ 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
Header ('content-Type: text/XML ');
Echo $ xmlrpc_response;

// Destroy XML-RPC server resources
Xmlrpc_server_destroy ($ xmlrpc_server );
?>

After the server is constructed, construct our RPC client. 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 is as follows: rpc_client.php

<? Php
/**

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.