dongyiyu882684 2013-01-16 07:05
浏览 14
已采纳

有没有正确的方法来使用PHP的Python代码?

There is some functionality written in python which I need to use in PHP. I need to pass parameters to python function and pass result(at least simple types: integer, float, tuple, etc.) back into php?. Can it be done?

  • 写回答

2条回答 默认 最新

  • doulin8374 2013-01-16 08:04
    关注

    You should use XML-RPC (remote procedure calling). It's dead-simple to setup a Python-Server for this, and in PHP, go with http://php.net/manual/de/ref.xmlrpc.php .

    E.g., as example (taken from the 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()
    

    sets up a fully working XML-RPC-server.

    Then every method of MyFuncs is available for use from any programming language that supports XML-RPC, including PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分