0

Is there a way, in Python, to run a single php syntax command? I know I can just run os.system to call a separate php file but that seem excessive. So I want no separate file if possible.

Something similar to:

if send_mail == True:
   #something to run single line php: mail($to,$txt,$subject)
PhantomQuest
  • 349
  • 4
  • 12
  • 3
    Possible duplicate of [Execute php code in Python](https://stackoverflow.com/questions/8984287/execute-php-code-in-python) – Green Cloak Guy Jun 13 '19 at 18:38
  • I want to do no separate file if possible. – PhantomQuest Jun 13 '19 at 18:47
  • @PhantomQuest How would `$to`, `$txt`, and `$subject` be getting values? – Patrick Q Jun 13 '19 at 18:48
  • @PatrickQ Hard coding – PhantomQuest Jun 13 '19 at 18:50
  • @PhantomQuest If you don't want to import any modules, then `os.system` is all you have. Is there a particular reason *why* you don't want to import anything, even though `subprocess` is part of the python standard library (and thus comes pre-installed, along with all the other built-in tools)? – Green Cloak Guy Jun 13 '19 at 18:51
  • It feels excessive to write a single line of php in an entire file then export that out. Sorta like how we use lambda functions. And overall curious if its possible to run separate languages (though I can see why not). Thought there might be a library for that. – PhantomQuest Jun 13 '19 at 18:54
  • @PhantomQuest Just to clarify, when you are objecting to using "separate file", are you objecting to the usage of `subprocess` completely or are you objecting to the call to a PHP file (such as `/path/to/your/script.php`) within the `subprocess.call` command? – Patrick Q Jun 13 '19 at 19:08
  • Just the calling of the PHP file. I'm really looking for a function in Python that will let me pass `mail('someone@email.com','txt','subject')` directly. – PhantomQuest Jun 13 '19 at 19:15
  • In that case, the suggested duplicate is still applicable, you just need to use `php -r 'command here';` – Patrick Q Jun 13 '19 at 19:23
  • So would final syntax be `subprocess.call('php -r mail('someone@email.com','txt','subject')')` – PhantomQuest Jun 13 '19 at 19:27
  • @PhantomQuest No, that's going to give you mismatched quoting issues. I don't use Python much, but it would probably be something like either `subprocess.call('php -r \'mail("someone@email.com","txt","subject")\');')` or `subprocess.call('php -r "mail(\'someone@email.com\',\'txt\',\'subject\'\)");'` – Patrick Q Jun 13 '19 at 19:39

1 Answers1

0

You can use subprocess library for doing this.

For example:


import subprocess

subprocess.call("php /path to php file")

I hope it helps.

ParthS007
  • 2,581
  • 1
  • 22
  • 37