Python, PHP communication through XML-RPC, the solution of XML-RPC Chinese

Source: Internet
Author: User
Tags error code

You can use XML-RPC to allow different platform software to interact with each other, we assume that using PHP as the foreground (client), Python as a service backstage, between XML-RPC invoke the services provided by Python. Examples are as follows:

1. PHP Client

<?php
xmlrpc_client.php
XML-RPC Client demo program
Require (' xmlrpc.inc ');
$xmlrpc _internalencoding= ' UTF-8 ';
Create a client object with three arguments in the path, hostname, 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 a Xmlrpcval object that encodes our PHP variables into XML-RPC-required forms
# $inputString =new Xmlrpcval (' World Triumph ', ' string ');
$inputString = Php_xmlrpc_encode (' Victory World ');

Create an array of parameters
Although we have only one argument, we still want to convert the form of an array, because the second parameter of xmlrpcmsg is a parameter table
$parameters =array ($inputString);

Create the Message Object
Create an XML-RPC message with parameters for the remote method name and parameter table
$msg =new xmlrpcmsg (' echostring ', $parameters);
$s->request_charset_encoding = ' utf-8 ';
Send the message and get the response
Sends a message, the return value $RSP to a Xmlrpcresp object, which contains the following three methods:
FaultCode () error code, if successful will return 0
FaultString () error message
Value () returns the Xmlrpcval object form, which needs to be decoded 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 ' <pre> ';
Var_dump ($response);
print ' </pre> ';
} else {
Print Errors
print ' Error: '. $RSP->faultcode (). $RSP->faultstring (). <br> ';
}

Show Messages
And then we'll look at the contents of the message.
$msg->createpayload ();
print ' request:<xmp> '. $msg->payload. " </xmp> ';
print ' response:<xmp> '. $RSP->serialize (). " </xmp> ';

?>

2, Python service side

#-*-coding:gb2312-*-
Import Simplexmlrpcserver

#定义自己的CMS类
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.