search

Home  >  Q&A  >  body text

How to call Python from PHP (Anaconda environment)

//1.php

<?php
   $order = 'python /home/python/1.py';
      $data = shell_exec($order);
      var_dump($data);
?> 
//1.py

#!usr/bin/env python`
import os
f = open('/home/python/1.txt','w')
print('OKOKOK')
f.write('OK')
f.close()

1. The above can successfully call and execute py

//2.php

<?php
   $order = 'python /home/python/2.py';
      $data = shell_exec($order);
      var_dump($data);
?> 
//2.py

#!usr/bin/env python`
import requests
import os

r = requests.get('http://ip.taobao.com/service/getIpInfo.php?ip=0.0.0.0')
print(r.text)

f = open('/home/python/2.txt','w')
f.write(r.text)
f.close()

2. The above cannot be called and executed (the file permissions are also 777)

centos
php environment: PHP7 (no high-risk functions are disabled)
python default environment: python 3.6 (requests module has been installed)
Anaconda path: /root/anaconda3
Common environment You need to execute the command source activate python36
$PYTHONPATH /root/anaconda3/bin/python3.6

The problem has been found
PHP runs python 2.7

Later redirect python
ln -s /root/anaconda3/bin/python3.6 /usr/bin/python
ln -s /root/anaconda3/bin/python3.6 /usr/bin/ python2
n -s /root/anaconda3/bin/python3.6 /usr/bin/python3

As a result, PHP cannot execute py~~

淡淡烟草味淡淡烟草味2755 days ago644

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:14:39

    Did you not install the requests module

    reply
    0
  • Cancelreply