First, overview:
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 itself integrates access to both XML-RPC and SOAP two protocols, concentrating on XMLRPC extensions. 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, readers can refer to the manual.
Second, install XMLRPC expansion:
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 The \ext directory. The specific installation directory of the DLL extension file depends on your PHP installation directory, which is illustrated here as an example, and the semicolon in front of the Extension=php_xmlrpc.dll in C:\Windows\php.ini or C:\Winnt\php.ini. 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 .) )
Three, the working principle of XML-RPC:
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.
Iv. the practice of XML-RPC:
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 rpc_server.php file code is as follows:
/**
* Functions: Functions supplied to RPC clients
* Parameters:
* $method client needs to call function
* $params The parameter array of the function 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;
}
Generates 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 client POST XML data
$request = $HTTP _raw_post_data;
Gets execution results
$xmlrpc _response = Xmlrpc_server_call_method ($xmlrpc _server, $request, NULL) after executing an XML request to invoke the client;
Output
header (' Content-type:text/xml ') of the result XML after the function is processed;
echo $xmlrpc _response;
Destroy XML-RPC server
-side resource Xmlrpc_server_destroy ($xmlrpc _server);
This is constructed on the server side and then constructs our RPC client. The client accesses the 80 port on the XML-RPC server side roughly through the socket, and then encapsulates the RPC interface that needs to be invoked into XML, submits it to the RPC server side through a POST request, and finally gets the server-side return result.
The
rpc_client.php file code is as follows:
/** * Functions: Provided to the client to connect XML-RPC Server-side function * Parameters: * $host need to connect the host * $port connect the host of the port * $RPC _server XML-RPC Server-side file * $request Encapsulation of the information of the xml-based request * Return: The connection successfully returns the XML information returned by the server side, failure returns false/function Rpc_client_call ($host, $port, $rpc _server, $request) {//Open the specified server side $FP
= Fsockopen ($host, $port); Constructs an XML-RPC server-side query Post request information 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 ";
The constructed HTTP protocol is sent to the server, and the failure returns False if (!fputs ($fp, $query, strlen ($query)) {$errstr = "Write error";
return false;
//Get all the information returned from the server side, including HTTP headers and XML information $contents = ' ";
while (!feof ($fp)) {$contents. = fgets ($FP);
////Close the connection resource to return the content obtained fclose ($FP);
return $contents;
}//Constructs the information that connects the RPC server side $host = ' localhost ';
$port = 80;
$rpc _server = '/~heiyeluren/rpc_server.php ';
The XML request that needs to be sent is encoded into XML, the method that needs to be called is Rpc_server, the parameter is get $request = xmlrpc_encode_request (' rpc_server ', ' "'); Call the Rpc_client_call function to send all requests to the XML-RPC server sideAfter obtaining information $response = Rpc_client_call ($host, $port, $rpc _server, $request);
Analyzes the XML returned from the server side, removes the HTTP header information, and turns the XML into a string that is recognized by PHP $split = ' ";
$xml = Explode ($split, $response); $xml = $split.
Array_pop ($xml);
$response = Xmlrpc_decode ($xml);
Outputs the information obtained from the RPC server Print_r ($response);
The above example is to submit a method called Rpc_server in the past, the parameter is get, and then obtain the server-side return, the server-side returned XML data 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 this XML as a PHP string, so that the entire Web service interacts with it.
V. Summary:
Whether it's XML-RPC or SOAP, the entire Web service is successful as long as it allows us to make a stable and secure call to the remote process and complete our project. Also, if you can, try using XML-RPC in pear to do the same thing above, and it might be simpler and more appropriate for you to use. Interested readers can try to finish.