Using XML-RPC to build Web Service in PHP

Source: Internet
Author: User

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.

 

* Author: heiyeluren
* Time: 2006-03-05
* Blog:
Http://blog.csdn.net/heiyeshuwu

[Introduction to Web Service]

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 detailed 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: in the/PHP/extensions directory, PhP5 extensions are stored in the C:/PHP/EXT directory. ini or C:/winnt/PHP. 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
/**
* Function: provides a function to the client to connect to the XML-RPC server.
* Parameters:
* $ Host the host to be connected
* $ Port: port used to connect to the host
* $ Rpc_server XML-RPC server files
* $ XML request information encapsulated by request
* Return: if the connection succeeds, XML Information returned by the server is returned. If the connection fails, false is returned.
*/
Function rpc_client_call ($ host, $ port, $ rpc_server, $ request ){

// Open the specified server
$ Fp = fsockopen ($ host, $ port );

// Construct the query POST request information on the XML-RPC server that requires communication
$ 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 ";

// Send the constructed HTTP protocol to the server. If the HTTP Protocol fails, false is returned.
If (! Fputs ($ FP, $ query, strlen ($ query )))
{
$ Errstr = "write error ";
Return false;
}

// Obtain all the information returned from the server, including the HTTP header and XML Information
$ Contents = '';
While (! Feof ($ FP ))
{
$ Contents. = fgets ($ FP );
}

// After the connection resource is closed, the retrieved content is returned.
Fclose ($ FP );
Return $ contents;
}

// Construct the information of the RPC server.
$ Host = 'localhost ';
$ Port = 80;
$ Rpc_server = '/~ Heiyeluren/rpc_server.php ';

// Encode the XML request to be sent into XML. The method to be called 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 for 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 that PHP can recognize
$ Split = '<? XML version = "1.0" encoding = "iso-8859-1"?> ';
$ Xml = explode ($ split, $ response );
$ Xml = $ split. array_pop ($ XML );
$ Response = xmlrpc_decode ($ XML );

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

?>

In general, the above example is to submit a method called rpc_server. The parameter is get, and then get the server-side response. The XML data returned by the server is:

<? XML version = "1.0" encoding = "iso-8859-1"?>
<Methodresponse>
<Params>
<Param>
<Value>
<String> this data by get method </string>
</Value>
</Param>
</Params>
</Methodresponse>

Then, we can use the xmlrpc_decode function to encode the XML as a PHP string, so that we can process it at will and complete Web service interaction.

[Conclusion]

Whether it is XML-RPC or soap, as long as we can make stable and secure remote process calls, to complete our project, then even the whole Web Service is successful. In addition, if you can, you can also try to use the XML-RPC in pear to achieve the above similar operations, maybe more simple, more suitable for your use.

Simple use of XML-RPC web service interaction is complete, part of the code reference PHP manual, want to obtain detailed information recommended reference manual, if the article is incorrect, please correct.

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.