PHP Using XML RPC to Construct Web Service Instance Tutorial

  • 2021-07-09 07:22:15
  • OfStack

1. Overview:

At present, there are two protocol standards for Web Service communication, one is XML-RPC, and the other is SOAP. XML-RPC is relatively simple and appears earlier, while SOAP is relatively complex, which is mainly used when it needs stability, robustness, security and complex interaction.

PHP itself integrates access to the XML-RPC and SOAP protocols, both concentrated in the xmlrpc extension. In addition, in PEAR of PHP, both PHP 4 and PHP 5 have integrated XML-RPC extension by default, and this extension has nothing to do with xmlrpc extension, and can independently realize XML-RPC protocol interaction. If there is no xmlrpc extension, it is recommended to use PEAR:: XML-RPC extension.

We mainly use XML-RPC to briefly describe the interaction process of Web Service. Some contents come from PHP manual. For more detailed contents, readers can refer to the manual.

2. Install the xmlrpc extension:

If the php extension of xmlrpc is not installed on your system, install it correctly.

Under the Windows platform, first put the extension php_xmlrpc. dll in the PHP installation directory into the C:\ Windows or C:\ Winnt directory, (PHP4 extension in C:\ php\ extensions directory, PHP5 extension in C:\ php\ ext directory. The specific installation directory of dll extension files depends on your php installation directory, which is only for example), and the semicolon before extension=php_xmlrpc. dll in C:\ Windows\ php. ini or C:\ Winnt\ php. ini "; "Remove, then reboot the Web server and check whether phpinfo () has the XML-RPC project to determine whether the xmlrpc extension has been installed correctly.

On the Unix/Linux platform, if the xmlrpc extension is not installed, recompile PHP, add the--with-xmlrpc option on configure, and then check phpinfo () to see if xmlrpc is installed properly.

(Note: The following operations are based on the normal installation of xmlrpc expansion, please install it correctly. )

3. Working principle of XML-RPC:

XML-RPC is roughly the whole process is to use XML for communication. First, construct an RPC server to come out the request encapsulated by XML transmitted from RPC client, and return the processing result to RPC client in the form of XML, and the client will analyze XML to obtain the data it needs.

The server side of XML-RPC must have ready-made functions for the client to call, and the functions and methods in the request submitted by the client must be the same as those on the server side, otherwise the required results will not be obtained.

Let me describe the whole process with simple code.

4. XML-RPC Practice:

The server uses xmlrpc_server_create function to generate a server, then registers the RPC call interface that needs to be exposed, accepts XML data from POST client, and then processes it. The processing results are displayed to the client in the form of XML.

The code for the rpc_server. php file is as follows:


/**
*  Function: Provides to RPC Functions called by the client 
*  Parameters: 
* $method  Functions that the client needs to call 
* $params  Array of arguments for the function that the client needs to call 
*  Return: Returns the result of the specified call 
*/
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; 
} 
// Produce 1 A XML-RPC Server side of 
$xmlrpc_server = xmlrpc_server_create(); 

// Registration 1 Methods called on the server side rpc_server What actually points to is rpc_server_func Function 
xmlrpc_server_register_method($xmlrpc_server, "rpc_server", "rpc_server_func"); 

// Accept client POST Come over XML Data 
$request = $HTTP_RAW_POST_DATA;

// Executes the calling client's XML Get the execution result after request 
$xmlrpc_response = xmlrpc_server_call_method($xmlrpc_server, $request, null); 


// The result of processing a function XML Output 
header(''Content-Type: text/xml''); 
echo $xmlrpc_response; 

// Destruction XML-RPC Server-side resources 
xmlrpc_server_destroy($xmlrpc_server); 

At this point, the server side is constructed, so we can construct our RPC client. The client visits port 80 of XML-RPC server through Socket, then encapsulates the RPC interface that needs to be called into XML, submits the request to RPC server through POST, and finally obtains the return result from the server.

The code for the rpc_client. php file is as follows:


/**
*  Function: Provided to the client for connection XML-RPC Server-side functions 
*  Parameters: 
* $host  Hosts that need to be connected 
* $port  Connect to the port of the host 
* $rpc_server XML-RPC Server-side file 
* $request  Encapsulated XML Request information 
*  Return: Successful connection successfully returns the returned by the server side XML Message, failure returns false
*/
function rpc_client_call($host, $port, $rpc_server, $request) { 

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

// Construct a that needs to communicate XML-RPC Server-side query POST Request information 
$query = "POST $rpc_server HTTP/1.0\nUser_Agent: XML-RPC Client\nHost: ".$host."\nContent-Type: text/xml\nContent-Length: ".strlen($request)."\n\n".$request."\n"; 

// Put the constructed HTTP The protocol is sent to the server and fails to return false
if (!fputs($fp, $query, strlen($query))) 
{ 
$errstr = "Write error"; 
return false; 
} 

// Gets all the information returned from the server, including HTTP Head and XML Information 
$contents = ''''; 
while (!feof($fp))
{ 
$contents .= fgets($fp); 
} 

// Returns the obtained content after closing the connection resource 
fclose($fp); 
return $contents; 
} 

// Structural connection RPC Server-side information 
$host = ''localhost''; 
$port = 80; 
$rpc_server = ''/~heiyeluren/rpc_server.php'';

// Send what needs to be sent XML Request to be encoded into XML The method that needs to be called is rpc_server The parameter is get
$request = xmlrpc_encode_request(''rpc_server'', ''get''); 

// Call rpc_client_call Function sends all requests to the XML-RPC Obtain information after the server side 
$response = rpc_client_call($host, $port, $rpc_server, $request); 

// Parse the returned from the server side XML , remove HTTP Header information, and put XML Convert to PHP Recognizable string 
$split = '''';
$xml = explode($split, $response);
$xml = $split . array_pop($xml);
$response = xmlrpc_decode($xml);

// Output from RPC Information obtained on the server side 
print_r($response);

Roughly, our above example is to submit a method called rpc_server with the parameter get, and then get the return from the server. 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 encode this XML into the string of PHP through xmlrpc_decode function, and we can handle it at will. At this point, the whole Web Service interaction is completed.

5. Summary:

Whether it is XML-RPC or SOAP, as long as we can call the remote procedure stably and safely and complete our project, then even if the whole Web Service is successful. In addition, if possible, you can also try to use XML-RPC in PEAR to achieve similar operations above, which may be simpler and more suitable for you. Interested readers can try to finish it.


Related articles: