Python communicates with PHP via XMLRPC: Server-side python, PHP for clients.
Server side: xmlrpc_server.py
#!/usr/bin/python#Coding:utf-8 fromSimplexmlrpcserverImportSimplexmlrpcserver fromSocketserverImportThreadingMixInclassThreadxmlrpcserver (ThreadingMixIn, Simplexmlrpcserver):Pass#define your own CMS classclassMycms: defechostring (Self, STRs):s = u'From Python-' returnS +STRsdefSayHello (self):return "Hello XMLPRC"#Create an instanceCMS =mycms () server= Threadxmlrpcserver (("localhost", 8888), allow_none=True)#Registerserver.register_instance (CMS)#the server executes and listens on this portPrint "Listening on port 8888"Server.serve_forever ()
Client: xmlrpc_client.php
<?PHP//XML-RPC Client Demo program///require (' libphpxmlrpc/xmlrpc.inc ');include(' Libphpxmlrpc/xmlrpc.inc ');$xmlrpc _internalencoding= ' UTF-8 ';//Create a client object, three parameters in turn path, hostname, Port$client=NewXmlrpc_client ('/', ' localhost ', 8888);//create Xmlrpcval object, which allows the encoding of our variable//creates Xmlrpcval objects, encodes our PHP variable into the XML form required by Xml-rpc$inputString= Php_xmlrpc_encode (' Hello world ');//create an array of parameters//although we only have one parameter, we still want to convert the form of the array, because the second parameter of xmlrpcmsg is a parameter table$parameters=Array($inputString);//Create the message object//creates a XML-RPC message with parameters for the remote method name and the parameter table$msg=NewXmlrpcmsg (' echostring ',$parameters);///$msg =new xmlrpcmsg (' SayHello ');//$client->request_charset_encoding = ' utf-8 ';//send the message, get the response//sends a paper, the return value $RSP is a Xmlrpcresp object, it contains the following three methods://FaultCode () error code, if successful will return 0//faultstring () error message//value () return value, exists as a Xmlrpcval object and needs to be decoded before PHP is used$client->debug =true;$RSP=$client->send ($msg);//Check for errorsif($RSP->faultcode () ==0) { //decode the response to a PHP type//xmlrpc_decode () function is used to decode the Xmlrpcval object $response=php_xmlrpc_decode ($RSP-value ()); //Print Results Print' <pre> '; Var_dump($response); ///echo $response; Print' </pre> ';} Else { //Print Errors Print' Error: '.$RSP->faultcode (). ', '.$RSP->faultstring (). ' <br> ';}//Show messages//Check the contents of the message/*$msg->createpayload ();p rint ' request:<xmp> '. $msg->payload. ' </xmp> ';p rint ' response:<xmp> '. $RSP->serialize (). ' </xmp> ';*/?>
Where the client needs the PHP language's XMLRPC Development library.
: http://download.csdn.net/detail/lsmallstop/7741211
After downloading, the Lib directory is copied and named Libphpxmlrpc, and placed in the same directory as the PHP file above.