有一些用python编写的功能,我需要在PHP中使用。我需要将参数传递给python函数并将结果(至少简单类型:整数,浮点数,元组等)传递回php?。可以吗?
答案 0 :(得分:3)
答案 1 :(得分:1)
您应该使用 XML-RPC (远程过程调用)。为此设置Python-Server非常简单,在PHP中,请使用http://php.net/manual/de/ref.xmlrpc.php。
例如,(取自docs)
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
requestHandler=RequestHandler)
server.register_introspection_functions()
# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)
# Register a function under a different name
def adder_function(x,y):
return x + y
server.register_function(adder_function, 'add')
# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'div').
class MyFuncs:
def div(self, x, y):
return x // y
server.register_instance(MyFuncs())
# Run the server's main loop
server.serve_forever()
设置一个完全有效的XML-RPC服务器。
然后MyFuncs
的每个方法都可以从任何支持XML-RPC的编程语言中使用,包括PHP。