最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python调用php文件中的函数代码示例解析
时间:2020-12-29 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下python调用php文件中的函数代码示例解析,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
前言
python调用php代码实现思路:php文件可通过在terminal中使用php命令行进行调用,因此可使用python开启子进程执行命令行代码。函数所需的参数可通过命令行传递。
测试环境
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文件: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批量下载apk代码示例
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27