Simple introduction to using XML-RPC to construct Web service in PHP

Source: Internet
Author: User
Tags manual ini soap pear phpinfo return client
Web|xml [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 detailed 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:\php\extensions directory, PHP5 is extended in C:\php \ext directory), at the same time C:\Windows\php.ini or C:\Winnt\php.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 properly.

(Note: The following operations are based on the XMLRPC expansion of normal installation, 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

<?php
/**
* Functions: Functions supplied to RPC client calls
Parameters
* $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 ';
}
Else
{
$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);
?>

[1] [2] Next page



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.