利用python怎么调用php文件中的函数-创新互联

本篇文章为大家展示了利用python怎么调用php文件中的函数,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

南通网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。成都创新互联公司从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司

测试环境

1、操作系统:macos10.13.2

2、php版本:PHP 7.1.7(mac自带)

3、python版本:python3.6.0

4、python库:subprocess

调用php函数

php命令行调用php文件中的函数

php文件:test_hello.php

terminal执行php命令

# 字符串中包含空格、逗号、反斜杠,需要使用""来确定为1个参数
php -f test_hello.php hello_world "My name is John\\, age is 20."
php -f test_hello.php hello_world2 "My name is John\\, age is 20." "My hometown is BaoDing."
php -f test_hello.php hello_world3 "My name is John\\, age is 20."

执行结果

利用python怎么调用php文件中的函数

python子进程执行php命令行

python文件:test.py,将test_hello.php与test.py放在同目录下运行

import subprocess


class Test(object):
 def run(self, cmd):
 proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) # 开启子进程
 res = proc.stdout.read()
 if res:
  res = res.decode()
 return res


cmd1 = 'php -f test_hello.php hello_world "My name is John\\, age is 20."'
cmd2 = 'php -f test_hello.php hello_world2 "My name is John\\, age is 20." "My hometown is BaoDing."'
cmd3 = 'php -f test_hello.php hello_world3 "My name is John\\, age is 20."'
obj = Test()
for i in [cmd1, cmd2, cmd3]:
 res = obj.run(cmd1)
 print(res)
 print("*" * 10)

上述内容就是利用python怎么调用php文件中的函数,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


文章名称:利用python怎么调用php文件中的函数-创新互联
网页网址:http://myzitong.com/article/deepeo.html