Construct a WebService using a XML-RPC. [WebService introduction] WebService is generated for the communication of heterogeneous systems. its basic idea is to use XML-based HTTP remote call to provide a standard mechanism, without the need to create
[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 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
/**
* 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
/**
* 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 ){
File: // open the specified server
$ Fp = fsockopen ($ host, $ port );
File: // Construct the POST request information on the XML-RPC server 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: // 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 = 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:
This data by get method
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 operation, maybe it will be simpler, more suitable for your use.
Simple use of XML-RPC Web Service interaction is completed, part of the code reference PHP manual, to obtain specific information recommended reference manual, if the article is incorrect, please correct.
Http://www.bkjia.com/PHPjc/631895.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631895.htmlTechArticle[ Web Service introduction] Web Service is generated for the communication of heterogeneous systems. its basic idea is to provide a standard mechanism for remote calls using XML-based HTTP, saving the trouble of creating...