상세 컨텐츠

본문 제목

[ Python ] PHP에서 Python 파일 호출하기

BackEnd/Python

by SangHoonE 2022. 2. 14. 15:57

본문

반응형

사용환경: WINDOW, LINUX, Python 3.10.2, PHP 8.0.3 (이전 버전도 가능)

 

PHP에서 Python의 파일을 호출하는 방법

PHP에서 shell_exec 를 통하여 Python 파일을 호출할 수 있다.

// 사용할 php파일

$output = shell_exec("python-text.py");	   // python-text.py : 파이썬 파일 이름 
echo $output;

최대한 내용을 짧게 하고 싶어서 명령어 없이 작성하였다.

 

다른 방법도 있다,

// 사용할 php 파일

$output = exec( "python python-file-name.py" );
echo $output;

파이썬 파일 이름 앞에 py, python, python3 이라든지 파이썬 명령어를 사용해도 무방하다. 

// python.py

helloThere = 'hello'
print(helloThere)

결과 출력 - hello

 

다음 포스팅은 php에서 매개변수(파라미터)전달하는 방법을 포스팅하겠습니다.

 

 

 

 

PHP: exec - Manual

If you're trying to use exec in a script that uses signal SIGCHLD, (i.e. pcntl_signal(SIGCHLD,'sigHandler');) it will return -1 as the exit code of the command (although output is correct!). To resolve this remove the signal handler and add it again after

www.php.net

 

 

PHP에서 Python 파일 실행하기

php에서 외부 프로그램을 실행시킬 일이 없을 것 같았는데 그 일이 생겼다. 그래서 구글링한 결과 간단하게 사용할 수 있는 것을 발견하고 소스를 추가해봤다. EXEC Function php manual은 다음을 참고

jeffrey-oh.tistory.com

 

관련글 더보기

댓글 영역