executing Python script in PHP and exchanging data between the two

前端 未结 4 1019
挽巷
挽巷 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: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.');
    }
    

提交回复
热议问题