title: How I embed a python script in php?
body: <p>I know that there are similar questions on StackOverflow but I've tried all of them and none of them worked. On my laptop, I have an Apache server, a website built with PHP and a Python script.</p>
  
  <p>Attempts:</p>
  
  <p>1) <a href="https://stackoverflow.com/questions/5497540/how-to-call-a-python-script-from-php">system</a></p>
  
  <pre><code>$mystring = system('python myscript.py myargs', $retval); 
  </code></pre>
  
  <p>2) and 3) <a href="https://stackoverflow.com/questions/14047979/executing-python-script-in-php-and-exchanging-data-between-the-two">JSON</a> and $temp = exec($command, $output);</p>
  
  <p>php:</p>
  
  <pre><code>#first case
  command= 'C:\wamp\www\com\non.py file';
  $temp = exec($command, $output);
  echo(" output ");
  
  echo $output;
  echo " hello ";
  echo $temp;
  
  #second case
  // Execute the python script with the JSON data
  $result = shell_exec('python /confronto/non.py '); 
  #. escapeshellarg(json_encode($data)));
  
  // Decode the result
  $resultData = json_decode($result, true);
  
  // This will contain: array('status' =&gt; 'Yes!')
  var_dump($resultData);
  </code></pre>
  
  <p>python:</p>
  
  <pre><code>import sys, json
  
  def tests():
      b = 2
      print("inside test")
      print(b)
      a = 4
      return a
  
  #if __name__ == "__main__":
  
  c = tests()
  print("main")
  print(c)
  # Generate some data to send to PHP
  result = {'status': 'Yes!'}
  
  # Send it to stdout (to PHP)
  print json.dumps(result)
  </code></pre>
  
  <p>4) apache configuration is setted:</p>
  
  <pre><code>AddHandler cgi-script .cgi .py 
  </code></pre>