Application of XML-RPC structure Web & nbsp; Service simple entry in PHP

Source: Internet
Author: User
[WebService prefix] WebService is generated for the communication of heterogeneous systems. its basic idea is to provide a scale mechanism for the application of XML-based HTTP remote calls, this saves the need to establish a new protocol. Currently, [Web Service prefix] is used for WebService communication.

Web Service is generated for the communication of heterogeneous systems. its basic idea is to provide a scale mechanism for the application of XML-based HTTP remote calls, this saves the need to establish a new protocol. Currently, there are two protocol standards for Web Service communication: XML-RPC and SOAP. XML-RPC is relatively simple, present time is relatively early, SOAP is more complex, important is some need to be stable, tough, secure and complex interaction application.

PHP integrated XML-RPC and SOAP two protocols to visit, are concentrated in the xmlrpc expansion. In addition, in php pear, whether PHP 4 or PHP 5, has been integrated with the XML-RPC expansion by default, and the expansion has nothing to do with xmlrpc expansion, can independently implement XML-RPC protocol interaction, if there is no xmlrpc expansion, it is recommended to apply PEAR: XML-RPC expansion.

Here we are an important XML-RPC to briefly describe the Web Service interaction process, part of the content from the PHP Manual, more specific content, it is recommended to refer to the manual.


[Install xmlrpc extension]

If your system does not have the php extension with xmlrpc installed, install it accurately.

On the Windows platform, first put the expanded php_xmlrpc.dll under the PHP installation directory to the C: \ Windows or C: \ Winnt directory: in the \ php \ extensions directory, PHP5 is expanded in the C: \ php \ ext directory), and C: \ Windows \ php. ini or C: \ Winnt \ php. ini put extension = php_xmlrpc.dll in front of the semicolon ';', and then restart the Web server to check whether phpinfo () has a XML-RPC project can determine whether the xmlrpc expansion has been accurately installed.

On Unix/Linux platforms, if xmlrpc expansion is not installed, re-compile PHP. when configure is installed, add the -- with-xmlrpc option and view phpinfo () check whether xmlrpc is installed properly.

(Note: The following controls are built on the xmlrpc expansion normal installation conditions, please be sure to install accurately .)


[XML-RPC working principle]

XML-RPC is generally the whole process is the application of XML for communication. First, an RPC server is used to construct an application XML encapsulation request passed from the RPC client, and return the processing result to the RPC client through the XML format, the client obtains the data you need from the analysis XML.

The server end of the XML-RPC must have a ready-made function supply should be called by the client, and the client to submit the function and method in the request must be consistent with the server, otherwise it will not be able to obtain the expected results.

Below I will briefly describe the entire process.


[XML-RPC practice]

The server uses the xmlrpc_server_create function to generate a server, and then registers the RPC calling interface that requires *** exposure to receive XML data from the POST of the RPC client, which is processed by lines, the processing result is displayed to the client in XML format.

The code is as follows: rpc_server.php

/**
* Function: provides the functions called by the RPC client.
* Parameters:
* $ Function to be called by the method client
* $ Params parameter array of the function to be called by the client
* 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 a XML-RPC on the server side
$ Xmlrpc_server = xmlrpc_server_create ();

// Register a method called by the server, rpc_server, which actually points to the rpc_server_func function
Xmlrpc_server_register_method ($ xmlrpc_server, 'rpc _ server', 'rpc _ server_func ');

// Receives XML data from the client POST
$ Request = $ HTTP_RAW_POST_DATA;

// Obtain the fulfillment result after the client makes an XML request
$ 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;

// Burn XML-RPC server resources
Xmlrpc_server_destroy ($ xmlrpc_server );
?>

The structure of the server is good, and then the RPC client is structured. The client roughly through Socket visit XML-RPC server really port 80, and then encapsulate the RPC Interface to be called in XML, submit to the RPC server through the POST request, and finally get the results returned by the server.

The code is as follows: rpc_client.php

/**
* Function: provide the client to connect to the XML-RPC server.
* Parameters:
* $ Host the host to be connected
* $ Port: port used to connect to the host
* $ Rpc_server XML-RPC server files
* $ XML request information encapsulated by request
* Return: If the connection succeeds, XML information returned by the server is returned. if the connection fails, false is returned.
*/
Function rpc_client_call ($ host, $ port, $ rpc_server, $ request ){

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

// The structure of the XML-RPC server that requires communication is really querying the 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 ';

// Send the structured HTTP protocol to the server. if the HTTP protocol fails, false is returned.
If (! Fputs ($ fp, $ query, strlen ($ query )))
{
$ Errstr = 'write error ';
Return false;
}

// Obtain all the information returned from the server, including the HTTP header and XML Information
$ Contents = '''';
While (! Feof ($ fp ))
{
$ Contents. = fgets ($ fp );
}

// The obtained content is returned after the connection resource is closed.
Fclose ($ fp );
Return $ contents;
}

// Structure the information of the RPC Server
$ Host = ''localhost '';
$ Port = 80;
$ Rpc_server = ''/~ Heiyeluren/rpc_server.php '';

// Encode the XML request to be sent into XML. the method 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 for information
$ Response = rpc_client_call ($ host, $ port, $ rpc_server, $ request );

// Analyze the XML returned from the server, drop the HTTP header information, and convert the XML into a string that can be identified by PHP.
$ 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 );

?>


In general, the above example is to submit a method called rpc_server in the past. the parameter is get, and then the server returns a true result. the XML data returned by the server is:






This data by get method





Then we can use the xmlrpc_decode function to encode the XML as a PHP string, so that we can handle it at will and complete all Web Service interactions.


[Conclusion]

Whether it is XML-RPC or SOAP, as long as we can make a solid and secure remote process call, to complete our project, then even if all Web services are successful. In addition, if you can, you can also try to apply the XML-RPC in PEAR to achieve the above like control, maybe it will be more simple, more suitable for your application.

Simple application of XML-RPC Web Service interaction is completed, part of the code reference PHP manual, to obtain specific information recommended reference manual, if the article is not accurate, please correct.

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.