• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python如何调用php文件中的函数详解

python 搞代码 4年前 (2022-01-07) 37次浏览 已收录 0个评论

这篇文章主要给大家介绍了关于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_hel<mark style="color:transparent">来源gaodaimacom搞#^代%!码&网</mark>lo.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如何调用php文件中函数的文章就介绍到这了,更多相关python调用php函数内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是python如何调用php文件中的函数详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python如何调用php文件中的函数详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址