PHP integrates access to two protocols, XML-RPC and SOAP, both of which are concentrated in the xmlrpc extension. In addition, in PHP's PEAR, whether it is PHP 4 or PHP 5, the XML-RPC extension has been integrated by default, and this extension has nothing to do with the xmlrpc extension and can independently implement XML-RPC protocol interaction. If there is no xmlrpc extension, it is recommended Use the PEAR::XML-RPC extension.
Introduction to Web Service
Web Service was created for the communication of heterogeneous systems. Its basic idea is to use XML-based HTTP remote calls to provide a standard mechanism, eliminating the need to establish a new protocol. Currently, there are two protocol standards for Web Service communication, one is XML-RPC and the other is SOAP. XML-RPC is relatively simple and appeared earlier, while SOAP is more complex and is mainly used when stability, robustness, security and complex interactions are required.
Here we mainly use XML-RPC to briefly describe the interaction process of Web Service. Some of the content comes from the PHP manual. For more details, it is recommended to refer to the manual.
Install the xmlrpc extension.
If the xmlrpc php extension is not installed in your system, please 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 (the extension for PHP4 is in the C:phpextensions directory, and the extension for PHP5 is in C: phpext directory), and remove the semicolon ";" in front of extension=php_xmlrpc.dll in C:Windowsphp.ini or C:Winntphp.ini, then restart the web server and check phpinfo () Whether there is an XML-RPC project can determine whether the xmlrpc extension has been installed correctly.
Under Unix/Linux platform, if the xmlrpc extension is not installed, please recompile PHP, add the --with-xmlrpc option when configure, and then check phpinfo() to see if xmlrpc is installed normally.
(Note: The following operations are based on the normal installation of the xmlrpc expansion. Please be sure to install it correctly.)
Working Principle of XML-RPC
XML-RPC generally means that the entire process uses XML for communication. First, an RPC server is constructed to process XML-encapsulated requests passed from the RPC client, and the processing results are returned to the RPC client in the form of XML. The client then analyzes the 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 consistent with those on the server side, otherwise the required results will not be obtained.
Below I make simple code to describe the entire process.
The XML-RPC practice
server uses the xmlrpc_server_create function to generate a server, then registers the RPC calling interface that needs to be exposed, accepts the XML data POST from the RPC client, and then processes it. The processing results are displayed to the client in the form of XML. .
The code is as follows: rpc_server.php
<?php
/**
* Function: function provided to be called by the RPC client
* Parameters:
* $method is the function that the client needs to call
* $params is the parameter array of the function that the client needs to call.
* 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 an XML-RPC server side
$xmlrpc_server = xmlrpc_server_create();
//Register a method rpc_server called by the 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;
//Get the execution result after executing the XML request calling the client
$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-side resources
xmlrpc_server_destroy($xmlrpc_server);
?>
After the server side is constructed, let’s construct our RPC client. The client accesses port 80 of the XML-RPC server roughly through Socket, then encapsulates the RPC interface that needs to be called into XML, submits it to the RPC server through a POST request, and finally obtains the result returned by the server.
The code is as follows: rpc_client.php
<?php
/**
* Function: Function provided to the client to connect to the XML-RPC server
* Parameters:
* $host The host that needs to be connected
* $port port to connect to the host
* $rpc_server XML-RPC server-side file
* $request encapsulated XML request information
* Return: If the connection is successful, the XML information returned by the server will be returned. If the connection is successful, it will return false.
*/
function rpc_client_call($host, $port, $rpc_server, $request) {
//Open the specified server
$fp = fsockopen($host, $port);
//Construct the query POST request information of the XML-RPC server that needs to communicate
$query = "POST $rpc_server HTTP/1.0nUser_Agent: XML-RPC ClientnHost: ".$host."nContent-Type: text/xmlnContent-Length: ".strlen($request)."n n".$request."n";
//Send the constructed HTTP protocol to the server, and return false if it fails.
if (!fputs($fp, $query, strlen($query))) {
$errstr = "Write error";
return false;
}
//Get all information returned from the server, including HTTP headers and XML information
$contents = '''';
while (!feof($fp)){
$contents .= fgets($fp);
}
//Return the obtained content after closing the connection resource
fclose($fp);
return $contents;
}
//Construct information to connect to the RPC server
$host = ''localhost'';
$port = 80;
$rpc_server = ''/~heiyeluren/rpc_server.php'';
//Encode the XML request that needs to be sent into XML. The method that needs 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 and obtain the 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);
?>
Roughly speaking, our above example is to submit a method called rpc_server, the parameter is 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 a PHP string through the xmlrpc_decode function, and we can process it at will, and the entire Web Service interaction is completed.
Conclusion
Whether it is XML-RPC or SOAP, as long as it allows us to make remote process calls stably and safely and complete our project, then the entire Web Service will be successful. In addition, if possible, you can also try to use XML-RPC in PEAR to implement similar operations above. It may be simpler and more suitable for you.
Simply use XML-RPC for Web Service interaction. For some codes, please refer to the PHP manual. If you want to get detailed information, it is recommended to refer to the manual. If the article is incorrect, please correct me.
This article comes from the original link of PHP information: http://www.phpchina.com/html/84/n-33884.html