How to run php in an HTML file running python? - php

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.

Related

Make output of cli based PHP script viewable from web without piping to a file?

I have a command line PHP script that runs constantly (infinite loop) on my server in a 'screen' session. The PHP script outputs various lines of data using echo.
What I would like to do is create a PHP web script to interface the command line script so that I can view the echo output without having to SSH into the server.
I had considered writing/piping all of the echo statements to a text file, and then having the web script read the text file. The problem here is that the text file will grow to several megabytes in the space of only a few minutes.
Does anyone know of a more elegant solution?
I think expect_popen will work for you, if you have it available.
Another option is to used named pipes - no disk usage, the reading end has output available as it comes.
The CLI script can write to a file like so:
file_put_contents( '/var/log/cli-log-'.date('YmdHi').'.log', $data );
Thereby a new log file being created every minute to keep the file size down. You can then clean up the directory at that point, deleting previous log files or moving them or whatever you want to do.
Then the web script can read from the current log file like so:
$log = file_get_contents( '/var/log/cli-log-'.date('YmdHi').'.log' );
As Elias Van Ootegem suggested, I would definitely recommend a cron instead of an constantly running script.
If you want to view the data from a web script you can do a few things....one is write the data to a log file or a database so you can pull it out later....I would consider limiting what you output if you there is so much data (if that is a possiblity).
I have a lot of crons email me data, not sure if that would work for you but I figured I would mention it.
The most elegant suggestion I can think of is to run the commands using exec in a web script which will directly output to the browse if you use : http://php.net/manual/en/function.flush.php

Run a php script in ruby on rails

In one of my Rails application I need to execute a PHP file from the Controller of Ruby on Rails app. The PHP file meant for some database editing.
The php file located in my "public" folder
Is there is any way for doing that?
If you want to talk to the process in some way, use IO.popen("php <script>"). Or you could use the backticks option (not easy with the markup here), which returns the string the process writes to stdout. If you need no communication, system "php <script>" returns you true or false wherever the command succeded and you've got no chance to communicate further.
If you have unsafe input, use Array#shelljoin to escape it.
Why not rewrite the script in ruby?
Try `php #{RAILS_ROOT}/public/php_script.php` or %x["php #{RAILS_ROOT}/public/php_script.php"]
System command doesn't work in rails but some code like the above should be ok.
well, does system('php public/php_script.php') work?
My problem solved by this line in my Controller
result = D:\\rails\\php\\php.exe -f D:\\Rails\\rails_apps\\project\\public\\df.php po

PHP in python through bash

As I am messing around with Python, I wanted to write an application that would have its own web server. I am currently using a written code from this website for http server: Python Web Server and this is the direct link of the script
This script can handle html but I wanted to add php to it too. Since I know that in Ubuntu I can run php commands within the terminal, I wanted to implement the same logic to this script.
I added these lines:
import os
os.system('php '+filepath)
However my plan didn't go as well as planned... The "<? echo 'hello world'; ?>" that was in my test.php echoed the string to the terminal that ran the webserver.py and it only returned the output message "0" to the web browser.
Is there a way to capture the output of the php file into a string in bash so that I can make the python webserver script output that string with os.system ?
I'm using Ubuntu 11.10.
You can use getoutput from the commands package for this simple case.
from commands import getoutput
response = getoutput('php myscript.php')
You should eschew the use of os.system() in favor of the more modern subprocess module.
You might especially look at popen.communicate().

Cleanest way to read config settings from PHP file and upload entire project code using shell script

I am a newbie with shell scripting so need a few ideas on parsing a PHP file using a shell script.
Ours is a PHP project and I am improving our shell script which is used to upload code to production server.
There is one PHP config file production.settings.php which needs to be read during upload, for a few constants -
BASE_PATH (path to project root on prod server)
db_host, db_name etc. (database settings of prod database - to be used for taking a backup of the database before upload)
Question
How to read the value of the constants?
They are defined like this:
define("BASE_PATH","/path/to/project/root");
How to read the first uncommented value of the constant?
Note - The constant may be defined more than once in the same file (let's assume the possibilty - this may happen by mistake or there may be commented instances of the line)
So far I am only able to get the number of lines containing the string define("BASE_PATH" using grep in my shell script -
cd ..
PROJECT_ROOT=$PWD
result= grep -ic 'define("BASE_PATH",' $PROJECT_ROOT'/config/main.settings.php'
echo "see"$result
Is this method of parsing good enough or a yml file would be better? Is there any shell command/snippet for doing this so that I can get the result by writing lesser amount of code?
Updates
Check my other questions for more details on this:-
Manipulating an array (printed by php-cli) in shell script,
Assigning values printed by PHP CLI to shell variables,
Initiating dynamic variables (variable variables) in bash shell script
just do it using the php, then call your shell script to invoke the php script.
Assuming you have your bunch of defines defined in defs.php:
define('NAME', 'JOHN');
define('HOBBY', 'FISHING');
then create a php script get_defs.php:
require_once 'defs.php';
$const = get_defined_constants(true);
foreach($const['user'] as $k => $v) {
echo "export $k=$v";
}
then in your shell script, run it like so:
`php get_defs.php`
What happen is, get_defs.php will output bunch of export KEY=VALUE, then shell will run those commands outputted by your php get_defs.php.
Why don't you just code with PHP CLI? That's what you understand? Also maybe you could put constants in a ini file and read them?
If youre comforttable with PHP then use PHP to write the shell script. IF you go this route i would move all config settings to a config file... INI, YAML, XML, whetever floats your boat. Then i would modify the bootstrap of the application that defines your constants to also read from this config file. That way you can use it in botht your script and the app without having to change it.

How can I pass a url or variable from Perl to PHP?

I have two existing scripts that work fine as individuals.
The main script is Perl. I wish to execute the PHP script from a sub in the Perl script.
Usually, the PHP script is just run via direct url e.g. http://me.com/phpscript.php?foo=bar
I would like to just call the PHP script from the Perl and pass the foo var so, I don't need to hit the PHP script with my browser to process the data.
I am not talented enough to rewrite the PHP script to Perl.
I tried exec("http://me.com/phpscript.php?foo=bar"); and include and system to no avail.
I have read and searched but, found only solutions to call Perl from PHP.
I really appreciate the great guidance I always fine here.
Seems like LWP::UserAgent should work for this scenario.
require LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get('http://me.com/phpscript.php?foo=bar');
if ($response->is_success) {
print $response->decoded_content; # or whatever
}
else {
die $response->status_line;
}
You can directly run a php file if you add #!PathToPhp
./myscript.php
in that file using argc or argv or args you can get arguments passed to this file most basic is args
#!/bin/php
<?php
foreach($args as $key => $value){
echo "\n".$key.":".$value;
If the script is located on the local filesystem, you should be able to exec it directly using the php interpreter and the path to the file. To access it via the web, use the LWP package.
For example:
exec('/usr/bin/php', 'myscript.php', #arguments);
Note that command-line arguments are handled differently than URL arguments; your PHP script will probably need to be modified to use them correctly.
There is a CPAN package that aims to provide a bridge between PHP and Perl:
This class encapsulates an embedded PHP5 intepreter. It provides proxy methods (via AUTOLOAD) to all the functions declared in the PHP interpreter, transparent conversion of Perl datatypes to PHP (and vice-versa), and the ability for PHP to similarly call Perl subroutines and access the Perl symbol table.
The goal of this package is to construct a transaparent bridge for running PHP code and Perl code side-by-side.
Not sure how stable this is though. See
PHP::Interpreter
Integrating PHP and Perl

Categories