Include python script/program in PHP - 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.

Related

including python code in 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.

Run a php script in the background

I am trying to run a php script in the background on windows.
Basically my goal is to run a exe file in the background and receive a signal when its done.
I'm open to any suggestions.
Thanks.
make a .bat file and on that bat file reference php file then it will work
if your using linux environment, i suggest creating a bash file and put your commands.
and if your using windows environment, use .bat file and put your commands.
php filename.php <arguments>
php another_filename.php <arguments>
and then run in in the terminal.. for the signal, you can exit() your php script based on what the condition your script needs..
UPDATE
Regarding your comment, if you want the webpage to create an executable then you must use the backtick or exec() in your web php file
for example, in your web php file:
//other php codes
echo `php runthisfile.php`;
echo exec('php runthisfile.php');
//other php codes
then if this is encountered, in the background, it will run the runthisfile.php file.. the backtick is a special symbol in php and exec() is a function that enables you to execute command line instructions..
and regarding the signalling, there are many approach.. some are:
in the background file, you can store a value in the database that the web page is listening to
make a soap call that uses xml as its data medium
or if what I think is right, just use AJAX in your web.. it really is helpful

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

How to run php in an HTML file running python?

I get an "unhandled exception" every time I try to run this standard include in an html file running python:
<?php
//add header with tabs
include('includes/templates/common/tpl_header.php');
?>
I'm trying to run it right under the body tag that looks like this:
<body py:match="body" py:attrs="h.append_class_attr(attrs, 'mcore-body')" py:with="
attrs = h.attrs_to_dict(select('#*'));
body_class = attrs.get('class', '').split(' ');
advertising_banner_html = g.settings['advertising_banner_html'];
">
... sorry but I'm not the most avid programmer, how do you get that puppy to run?!
HTML is HTML, Python is Python and PHP is PHP.
It's THREE different technologies, which cannot be mixed.
You can't "run standard PHP include" in HTML file.
You can't "run standard PHP include" in Python file.
You can run PHP code in PHP file only.
You can't run Python code in HTML file. You can run Python code in Python file only.
So, whole question makes no sense.
It seems you should forget PHP while working with Python and use Python features for including files. Of course it shouldn't be PHP file, but Python or at least HTML file
You can't "run" PHP inside Pytho.
Genshi has xi:include but that can include only local files.
It can apparently somehow be tricked into loading data from remote URLs - Here's an answer with some pointers (but no ready-made solution) how to do it.
If you manage to make Genshi fetch a remote file, you can point it to a local PHP URL: http://localhost/scripts/myscript.php Be aware however that this will start a remote PHP instance, and probably be slower than a pure filesystem lookup.
I have been in this situation myself when customizing Trac installs. The workaround I always ended up using was having a PHP script run manually or frequently through a cron job, and write into a static file that can be included using xi:include.

Categories