Tutorial Xml-rpc Constructing Web service_php in PHP

Source: Internet
Author: User
Tags soap phpinfo

PHP integrates access to XML-RPC and soap two protocols, all focused on xmlrpc extensions. In addition, in PHP pear, whether PHP 4 or PHP 5, has been integrated by default XML-RPC extension, and the extension is independent of xmlrpc extension, can independently implement the XML-RPC protocol interaction, if there is no xmlrpc extension, it is recommended to use pear:: XML-RPC extension.

Introduction to WEB Service

Web service is generated for the communication of heterogeneous systems, its basic idea is to use the remote invocation of XML-based HTTP to provide a standard mechanism, and eliminate 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, the occurrence of time is relatively early, soap is more complex, mainly some need to be stable, robust, secure and complex interaction when used.

We mainly use XML-RPC to describe the interactive process of Web service, part of the content from PHP manual, more detailed, recommended reference manual.

Installing the XMLRPC Extension

If you do not have a PHP extension for XMLRPC installed on your system, please install it correctly.

Under the Windows platform, first put the extension php_xmlrpc.dll under the PHP installation directory into the c:windows or c:winnt directory, (PHP4 extension in the C:phpextensions directory, PHP5 extension in C: Phpext directory), and extension=php_xmlrpc.dll the semicolon in front of the C:windowsphp.ini or C:winntphp.ini ";" Remove and then restart the Web server after viewing phpinfo () There is no XML-RPC project to be able to determine whether the XMLRPC extension is properly installed.

Under the Unix/linux platform, if the XMLRPC extension is not installed, recompile PHP, add the--WITH-XMLRPC option when configure, and check phpinfo () to see if XMLRPC is installed properly.

(Note: The following operations are based on the normal installation of XMLRPC expansion, please be sure to install correctly.) )

XML-RPC Working principle

Xml-rpc is roughly the whole process of using XML to communicate. The client then parses the XML to get the data it needs by constructing an XML-encapsulated request that is used by the RPC server to come out from the RPC client and return the processing results to the RPC client in XML form.

The server side of the XML-RPC must have out-of-the-box 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 result will not be obtained.

Here I carry out the simple code to describe the whole process.

XML-RPC Practice

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 result, which is displayed to the client in XML.

The code is as follows: rpc_server.php

/**
* Functions: Functions provided to RPC client calls
Parameters
* $method The function that the client needs to call
* $params the parameter array of 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;
}

Generate a XML-RPC server-side
$xmlrpc _server = Xmlrpc_server_create ();

Register a server-side call method Rpc_server, which is actually pointing to the Rpc_server_func function
Xmlrpc_server_register_method ($xmlrpc _server, "Rpc_server", "Rpc_server_func");

Accept the XML data that the client post comes from
$request = $HTTP _raw_post_data;

Get execution results after calling the client's XML request
$xmlrpc _response = Xmlrpc_server_call_method ($xmlrpc _server, $request, NULL);

Output the resulting XML after the function is processed
Header (Content-type:text/xml);
echo $xmlrpc _response;

Destroying XML-RPC server-side resources
Xmlrpc_server_destroy ($xmlrpc _server);
?>

The server side is constructed, and then our RPC client is constructed. The client accesses the 80 port on the XML-RPC server roughly through the socket, then encapsulates the RPC interface that needs to be called into XML, submits the post request to the RPC server side, and finally gets the server-side return result.

The code is as follows: rpc_client.php

/**
* Function: Provides a function to the client to connect the XML-RPC server side
Parameters
* $host hosts that need to be connected
* $port Port connected to the host
* $RPC _server XML-RPC server-side files
* XML Request Information $request encapsulated
* Returned: The connection successfully returned the XML information returned by the server side, failed to return false
*/
function Rpc_client_call ($host, $port, $rpc _server, $request) {

Opens the specified server-side
$fp = Fsockopen ($host, $port);

Constructs a XML-RPC server-side query post request information that needs to be communicated
$query = "POST $rpc _server http/1.0user_agent:xml-rpc clienthost:". $host. " Content-type:text/xmlcontent-length: ". strlen ($request).". $request. "";

Send the constructed HTTP protocol to the server and fail to return false
if (!fputs ($fp, $query, strlen ($query))) {
$ERRSTR = "Write error";
return false;
}

Gets all information returned from the server, including HTTP headers and XML information
$contents =;
while (!feof ($fp)) {
$contents. = Fgets ($FP);
}

Returns what gets when a connection resource is closed
Fclose ($FP);
return $contents;
}

To construct information that connects to the RPC server side
$host = localhost;
$port = 80;
$rpc _server =/~heiyeluren/rpc_server.php;

The XML request 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, 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);

Parse the XML returned from the server side, remove the HTTP header information, and convert the XML to a string that PHP can recognize
$split = ;
$xml = Explode ($split, $response);
$xml = $split. Array_pop ($xml);
$response = Xmlrpc_decode ($xml);

Output information obtained from the RPC server side
Print_r ($response);
?>

Our example above is to commit a method called Rpc_server in the past, the parameter is get, and then get the server-side return, the server side returns the XML data is:






This data by Get method



Then we can use the Xmlrpc_decode function to encode this XML as a PHP string, we will be able to handle the entire Web service interaction completed.

Conclusion

Whether it is XML-RPC or soap, as long as we can make a stable and secure remote process calls, complete our project, even if the entire Web service is successful. In addition, if you can, you can also try to use the pear in the XML-RPC to achieve the above similar operation, it might be more simple, more suitable for you to use.

The simple use of XML-RPC for Web service interaction is complete, part of the Code reference PHP manual, want to get more information suggested reference manual, if the article is incorrect, please correct me.
This article is from the source of PHP Information link: html/84/n-33884.html ">http://www.phpchina.com/html/84/n-33884.html

http://www.bkjia.com/PHPjc/508344.html www.bkjia.com true http://www.bkjia.com/PHPjc/508344.html techarticle PHP integrates access to XML-RPC and soap two protocols, all focused on xmlrpc extensions. In addition, in PHP pear, whether PHP 4 or PHP 5, has been integrated by default XML-RPC extension ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.