executing Python script in PHP and exchanging data between the two

前端 未结 4 999
挽巷
挽巷 2020-11-27 10:37

Is it possible to run a Python script within PHP and transferring variables from each other ?

I have a class that scraps websites for data in a certain global way. i

相关标签:
4条回答
  • 2020-11-27 11:03

    You are looking for "interprocess communication" (IPC) - you could use something like XML-RPC, which basically lets you call a function in a remote process, and handles the translation of all the argument data-types between languages (so you could call a PHP function from Python, or vice versa - as long as the arguments are of a supported type)

    Python has a builtin XML-RPC server and a client

    The phpxmlrpc library has both a client and server

    There are examples for both, Python server and client, and a PHP client and server

    0 讨论(0)
  • 2020-11-27 11:06

    Just had the same problem and wanted to share my solution. (follows closely what Amadan suggests)

    python piece

    import subprocess
    
    output = subprocess.check_output(["php", path-to-my-php-script, input1])
    

    you could also do: blah = input1 instead of just submitting an unnamed arg... and then use the $_GET['blah'].

    php piece

    $blah = $argv[1];
    
    
    
    if( isset($blah)){
    
        // do stuff with $blah
    
    }else{
        throw new \Exception('No blah.');
    }
    
    0 讨论(0)
  • 2020-11-27 11:07

    The best bet is running python as a subprocess and capturing its output, then parsing it.

    $pythonoutput = `/usr/bin/env python pythoncode.py`;
    

    Using JSON would probably help make it easy to both produce and parse in both languages, since it's standard and both languages support it (well, at least non-ancient versions do). In Python,

    json.dumps(stuff)
    

    and then in PHP

    $stuff = json_decode($pythonoutput);
    

    You could also explicitly save the data as files, or use sockets, or have many different ways to make this more efficient (and more complicated) depending on the exact scenario you need, but this is the simplest.

    0 讨论(0)
  • 2020-11-27 11:15

    You can generally communicate between languages by using common language formats, and using stdin and stdout to communicate the data.

    Example with PHP/Python using a shell argument to send the initial data via JSON

    PHP:

    // This is the data you want to pass to Python
    $data = array('as', 'df', 'gh');
    
    // Execute the python script with the JSON data
    $result = shell_exec('python /path/to/myScript.py ' . escapeshellarg(json_encode($data)));
    
    // Decode the result
    $resultData = json_decode($result, true);
    
    // This will contain: array('status' => 'Yes!')
    var_dump($resultData);
    

    Python:

    import sys, json
    
    # Load the data that PHP sent us
    try:
        data = json.loads(sys.argv[1])
    except:
        print "ERROR"
        sys.exit(1)
    
    # Generate some data to send to PHP
    result = {'status': 'Yes!'}
    
    # Send it to stdout (to PHP)
    print json.dumps(result)
    
    0 讨论(0)
提交回复
热议问题