Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

วิธีเรียกไฟล์ Python จากภายใน PHP

เพื่อเรียกไฟล์ Python จากภายในไฟล์ PHP ต้องใช้ฟังก์ชัน shell_exec

ตัวอย่าง

<?php 
   $command = escapeshellcmd('/usr/custom/test.py'); 
   $output = shell_exec($command); 
   echo $output; 
?>

สิ่งนี้จะเรียกสคริปต์ แต่ในไฟล์ .py ของคุณ ต้องระบุ interpreter ด้วย บรรทัดแรกของไฟล์ py ให้เพิ่ม:

#!/usr/bin/env python

ทางเลือกอื่น: ระบุ interpreter ตอนสั่งรันคำสั่ง

ตัวอย่าง

<?php 
   $command = escapeshellcmd('python3 /usr/custom/test.py'); 
   $output = shell_exec($command); 
   echo $output; 
?>