python如何調(diào)用php文件中的函數(shù)詳解
前言
python調(diào)用php代碼實(shí)現(xiàn)思路:php文件可通過(guò)在terminal中使用php命令行進(jìn)行調(diào)用,因此可使用python開啟子進(jìn)程執(zhí)行命令行代碼。函數(shù)所需的參數(shù)可通過(guò)命令行傳遞。
測(cè)試環(huán)境
1、操作系統(tǒng):macos10.13.2
2、php版本:PHP 7.1.7(mac自帶)
3、python版本:python3.6.0
4、python庫(kù):subprocess
調(diào)用php函數(shù)
php命令行調(diào)用php文件中的函數(shù)
php文件:test_hello.php
<?php function hello_world($s1) { $str1 = $s1; echo $str1; echo "\n"; } function hello_world2($s1, $s2) { $str1 = $s1; $str2 = $s2; echo $s1; echo "**********"; echo $s2; echo "\n"; } // 獲取參數(shù),索引為0為調(diào)用的文件路徑,索引為1為調(diào)用的函數(shù),索引為2為函數(shù)傳入?yún)?shù)$s1,索引為3為函數(shù)參數(shù)$s2 var_dump($argv); // exit; // 調(diào)用函數(shù) $func_name = $argv[1]; if ($func_name == "hello_world") { // 參數(shù)1 $param1 = $argv[2]; hello_world($param1); } elseif ($func_name == "hello_world2") { // 參數(shù)1 $param1 = $argv[2]; // 參數(shù)2 $param2 = $argv[3]; hello_world2($param1, $param2); } else { echo "the function $func_name is not exist !"; } ?>
terminal執(zhí)行php命令
# 字符串中包含空格、逗號(hào)、反斜杠,需要使用""來(lái)確定為1個(gè)參數(shù) 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."
執(zhí)行結(jié)果
python子進(jìn)程執(zhí)行php命令行
python文件:test.py,將test_hello.php與test.py放在同目錄下運(yùn)行
import subprocess class Test(object): def run(self, cmd): proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) # 開啟子進(jìn)程 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)
到此這篇關(guān)于python如何調(diào)用php文件中函數(shù)的文章就介紹到這了,更多相關(guān)python調(diào)用php函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用python如何實(shí)現(xiàn)泛型函數(shù)
這篇文章主要介紹了使用python如何實(shí)現(xiàn)泛型函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Python中schedule模塊定時(shí)任務(wù)的使用方法(2)
這篇文章主要介紹了Python中schedule模塊定時(shí)任務(wù)的使用方法,文章基于上一篇文章的內(nèi)容展開的后續(xù),需要的朋友可以參考一下2022-05-05Python Pytest裝飾器@pytest.mark.parametrize詳解
本文主要介紹了Python Pytest裝飾器@pytest.mark.parametrize詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08python3 與python2 異常處理的區(qū)別與聯(lián)系
這篇文章主要介紹了python3 與python2 異常處理的區(qū)別與聯(lián)系的相關(guān)資料,需要的朋友可以參考下2016-06-06詳解Python?Flask?API?示例演示(附cookies和session)
這篇文章主要為大家介紹了Python?Flask?API?示例演示(附cookies和session)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Python實(shí)現(xiàn)批量合并圖片到word文檔
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)批量合并指定文件夾中的所有圖片并插入到Word文檔中并保存,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10python實(shí)現(xiàn)tail實(shí)時(shí)查看服務(wù)器日志示例
今天小編就為大家分享一篇python實(shí)現(xiàn)tail實(shí)時(shí)查看服務(wù)器日志示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12