2

My question may be incorrect or even strange, but I'm really interested in such programming experience, and there is two reasons for that:

  1. As a PHP developer I should do my work so I can't just switch to other programming language that easy; however, there is a lot of things that causes a lot of pain to write in PHP.

  2. As a Python beginner I'm already a huge fan of this language, and there are things that can be done a lot easier and, IMHO, in more righteous way that PHP implementation suggests.

For example, I've been writing a broadcasting multiple-connection socket server in PHP, and anybody who has done similar thing would understand how many restrictions will cause such solution - detecting disconnect if client just closed browser is dreadful. Looking at broadcasting server implementations in Python makes me feel more comfortable.

Also, a think about applications that could work, say, in offline mode to gather user input and sending it to the processing server later, or stand-alone applications that are connected to a website, etc.

Searching the web is poor in this case. All I've found is PiP, but it was released too long ago and not documented well - there is probably a good reason for that.

I would be glad to hear any thoughts about this, because I understand that this idea is kind of crazy and looks like not a lot of people is concerned about it.

3
  • whats the question here? Commented Jan 10, 2014 at 21:01
  • How can I do it - combine PHP with Python modules? If yes, should I even think about it? If yes, is there are best ways of doing this? Commented Jan 10, 2014 at 21:04
  • 1
    in general just stick with one ... sometimes you do want to use a tool in php you can do this with exec in python you can do this with os.system while that pip thing is pretty cool I would not reccommend that as you will just confuse other people who work on/with your code base (regardless of if they know python or php, most developers actually can do either, but to see them mixed like that is confusing at best) Commented Jan 10, 2014 at 21:13

1 Answer 1

2
+50

Some time ago I ran into a similar dilemma. The solution I found was use xml-rpc to expose python objects and methods so I can use them from php scripts. Here I left you the documentation of both.

Python: Python xml-rpc. PHP: XML-PHP

EDIT: Adding example. The examples are the same that in the documentation. I just changed them a bit to make them shorter. In client.php I only call the div function from python server. Add the others your self.

server.py

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()

client.php

<html>
<head><title>xmlrpc</title></head>
<body>
<h1>Php - Python - XMLRPC Demo</h1>
<?php

    // Note that the path to xmlrpc.inc file is relative.
    // to this file.
    include("xmlrpc/lib/xmlrpc.inc");

    // Params to python function 10 and 5.
    // Build the message you want send.
    // The message takes the function name and the params. See doc for details on how
    // build params, is pretty easy.
    $msg = new xmlrpcmsg( "div", array(new xmlrpcval(10, "int"), new xmlrpcval(5, "int")) );
    // Build a XMLRCP - Client.
    $client = new xmlrpc_client("/RPC2", "localhost", 8000);
    // And send the message.
    $response = $client->send($msg);


    // From here all should look familier to you.
    if(!$response->faultCode())
    {
        $v=$response->value();
        echo "The result from div is" . htmlspecialchars($v->scalarval());

    }
    else
    {
        print "An error occurred: ";
        print "Code: " . htmlspecialchars($r->faultCode())
            . " Reason: '" . htmlspecialchars($r->faultString()) . "'</pre><br/>";
    }

?>
<hr/>
</body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

That's interesting indeed, but after several hours I still have little understanding how to use it. Could you please share a simple example of calling Python method from within PHP-script?
Thank you. This will probably work well for solutions that operate on client-server basis. And there is no easy way out to use PHP and Python combined in one file, for obvious reasons...
Yeah, I'm kind of mentioned this PiP project in my question : ) Anyway, looks like this is finale. Thank you, Raydel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.