Python and PHP communicate through xml-rpc, xml-rpc Chinese solution-PHP source code

Source: Internet
Author: User
Ec (2); can let the software of different platforms interact with each other through the XML-RPC, we assume that the front-end (client) with PHP, Python as the service background, through the XML-RPC to call the services provided by Python. Example: 1. PHP client & nbsp; & lt ;? Phpxmlrpc_client.phpXML-RPC client demo (xmlrpc. inc); $ xmlrpc_interna script ec (2); script

We can let the software of different platforms interact with each other through the XML-RPC, we assume that using PHP as the front-end (client), Python as the service background, through the XML-RPC to call the services provided by Python. Example:

1. PHP Client

// Xmlrpc_client.php
// XML-RPC client demo
Require (''xmlrpc. inc '');
$ Xmlrpc_internalencoding = ''utf-8 '';
// Create a client object. The three parameters are path, hostname, and port.
# $ S = new xmlrpc_client (''/testxml/xmlrpc_server.php'', ''localhost'', 80 );
$ S = new xmlrpc_client (''/'', ''localhost'', 8888 );

// Create xmlrpcval object, which allows the encoding of our variable
// Create the xmlrpcval object and encode our PHP variables in the XML format required by the XML-RPC
# $ InputString = new xmlrpcval (''world victory '', ''string '');
$ InputString = php_xmlrpc_encode (''victory World '');

// Create an array of parameters
// Although we only have one parameter, we still need to convert it into an array, because the second parameter of xmlrpcmsg is a parameter table.
$ Parameters = array ($ inputString );

// Create the message object
// Create XML-RPC message, parameters are remote method name and parameter table
$ Msg = new xmlrpcmsg (''echostring'', $ parameters );
// $ S-> request_charset_encoding = ''utf-8 '';
// Send the message, get the response
// Send a message. The returned value $ rsp is an xmlrpcresp object. It contains the following three methods:
// FaultCode () error code. If the code is successful, 0 is returned.
// FaultString () error message
// Value () return value, which exists in the form of xmlrpcval object. decoding is required before PHP is used.
$ S-> debug = true;
$ Rsp = $ s-> send ($ msg );

// Check for errors
If ($ rsp-> faultcode () = 0 ){
// Decode the response to a PHP type
// The xmlrpc_decode () function is used to decode the xmlrpcval object.
$ Response = php_xmlrpc_decode ($ rsp-> value ());

// Print results
Print''

'';
var_dump($response);
print ''
'';
} Else {
// Print errors
Print ''error: ''. $ rsp-> faultcode ().'', ''. $ rsp-> faultstring ().''
'';
}

// Show messages
// Check the message content.
$ Msg-> createpayload ();
Print ''request:

''. $ Msg-> payload .'' '';
Print ''response: ''. $ Rsp-> serialize ().'' '';

?>

2. Python Server

#-*-Coding: GB2312 -*-
Import SimpleXMLRPCServer

# Define your own CMS class
Class MyCMS:
Def getVersion (self

<

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.