including python code in php - php

I want to include the following python code into php. The name of the python file is hello.py. The contents are
print "Hello World"
I want to call this python script in php and show the same in webpage

Not really possible like that. What you could do is use exec or passthru to run the python in the terminal and return the results. Best option would be to do a separate call to the python script and combine them on the client.

Related

Call Python script from php with exec()

I have one python script for handling outlook emails using Outlook MAPI. I have to call this script from PHP code. If I call script from cmd it is fine, but when I do it from PHP doesn't work and there are no errors. Also if I just call some other python script with simple file writing code from same PHP script it works. I have no idea what to do. I am trying with exec('python c:/xampp/SCRIPT_LOCATION/SCRIPT.py') as well as shell_exec(), system()... but with no progression.
Try this:
$python = 'C:\\Python27\\python.exe';
$pyscript = 'C:\\xampp\\SCRIPT_LOCATION\\SCRIPT.py';
exec("$python $pyscript");

Executing Prolog from SWI and using the output in PHP

I have SWI Prolog working in my vm where I'm going to be working on a PHP/HTML website. I've been following this guide: http://www.j-paine.org/dobbs/prolog_from_php.html#appendix to set up my php file to receive the output from the executed prolog file. The problem that I'm having is that it seems that the output from SWI's command line (which I'm executing from my bash terminal) isn't being captured as a standard output that I can get PHP to recognize. So, in my terminal, when I execute SWI and a test prolog file, it writes out like I need it to, but I can't figure out how to capture that output to then use in PHP.
Any ideas or suggestions would be really great. Thanks!

Generate an output file with python inside a php script

I need help in figuring out on how to generate an output file in python inside php script.
So here is the problem:
I have a php script that lets user upload files.
Those files are stored in the server.
After that those files will be run on a python script, and will generate output files.
After that the php script will get those output files, and send an email to the user.
I want to make it automatically send to the user after they upload and click the submit button.
I have all those done, until trying to call a python script inside the php script
The python script inside the php script does not execute the function where it generates the output files.
This is the function that I use for calling the bash script:
system('bash /data/home/rxs061/public_html/test.sh');
I also have used the function exec(); and shell_exec();
But does not work.
The problem here, the function does not run on the command line, so the output file won't be generated.
This is inside the bash script (test.sh):
python /home/rxs061/public_html/test.py
outfile=$PWD/test.out
echo "$PWD/"
The problem here it does not want to generate "test.out"
This is inside the the python script (test.py):
import sys
outfile = open('test.out', 'w')
outfile.write('it worked\n')
outfile.close()
This does not have to do with permissions.
How do you put this all together in a single php script?
Or are there any other solutions?
Or how to make it output files???
Oh I get all fix, it something to do with the permission.
After I do chmod 777, it is all fixed

To call a perl script as an external program in a PHP script

I want to call a perl script as an external program in a PHP script, once the PHP script finished its run, it should send back the output to a html page. I tried using exec command to call the perl script but it dint work out. please help with this simple perl script, so that i can try using the same !
Perl script:
#!/usr/bin/perl -s
$var1 = 'high';
print $var1;
Thanks ahead of time !!
the output of commands called via exec will be stored in the output parameter that you give (hence why it's specified as by reference) either print out the contents of that array or use the passthru() function instead
references:
http://www.php.net/manual/en/function.exec.php
http://www.php.net/manual/en/function.passthru.php
so just
passthru('/usr/bin/perl yourscript.pl');
instead of
exec('/usr/bin/perl yourscript.pl');
I would suggest trying
exec ('path_to_your_script')
This will work for any shell executable or command call, whenever you want to execute it from your PHP script.

Include python script/program in PHP

I am new to Python and currently have to write a python program/script to be integrated inside PHP. Basically I have one stand-alone python program that will be executed through CRONTAB that grabs data from a remote device and exports the data to an XML file. I also have to write another python program/script to be include inside PHP to read the XML and echo/print the data onto the screen. So my question is that
What is the difference between python program and script? Given my python program, I included #!/usr/bin/env python at the beginning of the file but I still cant execute it using ./scriptName.py
How to include and run a python program/script inside the PHP file? Inside the python script, how to output the data using the same manner as echo in PHP? The include part in my PHP is:
<td><p><img src="img/today.gif" alt="today" width="180" height="40" /></p>
<?php include "monthphotos.php"; ?>
<?php include "todayatcpp/index.php"; ?>
</td>
I am new to beginner programmer so some of my questions can be obscure. Sorry!
The only way you can run a python script inside of a PHP script is to use the exec call. With that you will be able to capture the output from the python script and be able to use it in PHP.

Categories