Using XML-RPC to construct WebService_PHP tutorial

WBOY
Release: 2016-07-13 10:59:39
Original
782 people have browsed it

[Web Service Introduction]

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.

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.

We here mainly use XML-RPC to briefly describe the interaction process of Web Service. Some of the content comes from the PHP manual. For more specific content, it is recommended to refer to the manual.

 [Install xmlrpc extension]

If the xmlrpc php extension is not installed in your system, please install it correctly.

Under Windows platform, first put the extension php_xmlrpc.dll in the PHP installation directory into the C:Windows or C:Winnt directory. (PHP4 extensions are in the C:phpextensions directory, and PHP5 extensions are in the 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 whether there is an XML-RPC project in phpinfo() to 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.)

 [XML-RPC working principle]

XML-RPC roughly 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.

I will use simple code below to describe the entire process.

 [XML-RPC Practice]

The 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

/**
* Function: Function provided to the RPC client
* Parameter:
* $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;

//Execute the XML request calling 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-side resources
xmlrpc_server_destroy($xmlrpc_server);
?>

Now that 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 gets the result returned by the server.

The code is as follows: rpc_client.php

/**
* Function: Function provided to the client to connect to the XML-RPC server
* Parameter:
* $host The host that needs to be connected
* $port The 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 it fails, it will return false
*/
function rpc_client_call($host, $port, $rpc_server, $request) {

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

file://Constructs the XML-RPC server-side query POST request information that needs to be communicated
$query = "POST $rpc_server HTTP/1.0 User_Agent: XML-RPC Client Host: ".$host." Content-Type: text/xml Content-Length: ".strlen($request)." ".$request." ";

file:// sends the constructed HTTP protocol to the server, and returns false on failure
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 = 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:






This data by get method



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. Please refer to the PHP manual for some codes. If you want to obtain specific information, it is recommended to refer to the manual. If the article is incorrect, please correct me.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631895.htmlTechArticle[Web Service Introduction] Web Service is created for the communication of heterogeneous systems. Its basic idea is to use XML-based HTTP remote calls provide a standard mechanism, eliminating the need to build...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template