PHP in Perl

D

Dave F

Is it possible to embed some PHP script in an HTML stream being
returned from a Perl script? I know it sounds wacky, but I'd like
to use chart library that uses PHP's support for the GD library.
PHP works on the system (I know .php files in the html
folder are served fine, so PHP is running.)

I've tried:

1. The standard <?php ... script area ... ?> within the HTML
being built by Perl -- HTML comes OK, nothing for the PHP

2. Script within a <SCRIPT TYPE='php'> ... script area ... </SCRIPT>
Same as 1.

3. Using print "content-type: application/x-php\n\n";
Instead of print "content-type: text/html\n\n";
This pops a window saying Mozilla needs to know what to use
to handle this file type.

Is there any way to tell the web server to run the content returned
by Perl though the PHP processor? I am trying to do this on both a
Windows/Sambar system and an Apache/Mandrake 9.2 system.

Any information is appreciated.

- Dave Fischer
 
B

bma3

The reason "script" doesn't work doesn't work is because the script tag
is for client side scripts. PHP, like Perl, is server-side scripting.
So you need a way for the server to run it.

There are two ways I can think of to do this. You can configure Apache
to first run the script through perl and then through php, and then the
second approach you tried would work. This seems like a real pain in
the butt though.

Or, you can Perl execute php. The approach I would use is to have a
separate source file for php, with the extension ".php" so you won't
have the security issue of someone being able to view the source.

Then I'd say in the perl script,
"system foo.php" and die "cannot run script".

Of course, you were probably thinking you'd like to generate the php
source in perl. If you're using a database or a read only text file or
something to get the data from, I'd suggest just learning how to get
PHP to access it. However, you may be in a situation where this just
won't work, and will need to use temporary files or pipes.

I don't want to write out how to do it unless you need to know how, but
if you do, just reply and I'll figure out some instructions.
 
S

Sherm Pendley

Dave said:
Is it possible to embed some PHP script in an HTML stream being
returned from a Perl script? I know it sounds wacky, but I'd like
to use chart library that uses PHP's support for the GD library.

Well, let's look at the simplest case first. You'd produce an image with
PHP by putting the following in your HTML:

<img src="/images/chart.php">

The browser fetches the HTML page first. When it gets the HTML back and
finds the above img element in it, it then fetches the image data from
the PHP script indicated by the src attribute. It doesn't matter whether
the HTML is produced by the same scripting language as the image data,
because it's two entirely separate requests anyway.

Now suppose you need to pass query parameters to the image-producing
script. Just append the appropriate query string - the URI module makes
that easy:

#!/usr/bin/perl
use warnings;
use strict;

use URI;

my $img_src = new URI('/images/chart.php');
$img_src->query_form( 'foo'=>'hello', 'bar'=>'world');

print "<img src='$img_src'>";

If you want to pass along all of the query parameters your Perl CGI
received, use the query_form() method along with CGI.pm's Vars() method,
like so:

#!/usr/bin/perl
use warnings;
use strict;

use CGI;
use URI;

my $q = new CGI;
my $img_src = new URI('/images/chart.php');
$img_src->query_form( $q->Vars() );

print "<img src='$img_src'>";

Note that from the point of view of the Perl script producing the HTML
data, it doesn't matter in the least what will be handling the request
for the image. It could be another Perl script, a PHP script, Macromedia
Generator - whatever.

sherm--
 
S

Shawn Corey

Sherm said:
Well, let's look at the simplest case first. You'd produce an image with
PHP by putting the following in your HTML:

<img src="/images/chart.php">

Make sure that chart.php sends a "Content-type: ..." before the image,
as in (and remember the extra blank line):

Content-type: image/png



--- Shawn
 
D

Dave F

Dave said:
Is it possible to embed some PHP script in an HTML stream being
returned from a Perl script? I know it sounds wacky, but I'd like
to use chart library that uses PHP's support for the GD library.
PHP works on the system (I know .php files in the html
folder are served fine, so PHP is running.)

I've tried:

1. The standard <?php ... script area ... ?> within the HTML
being built by Perl -- HTML comes OK, nothing for the PHP

2. Script within a <SCRIPT TYPE='php'> ... script area ... </SCRIPT>
Same as 1.

3. Using print "content-type: application/x-php\n\n";
Instead of print "content-type: text/html\n\n";
This pops a window saying Mozilla needs to know what to use
to handle this file type.

Is there any way to tell the web server to run the content returned
by Perl though the PHP processor? I am trying to do this on both a
Windows/Sambar system and an Apache/Mandrake 9.2 system.

Any information is appreciated.

- Dave Fischer

Thanks to Sherm and bma3 for the help. I tried both setting the server
to process HTM and HTML through PHP and also the idea of using Perl to
generate a PHP file and then call it (calling in an <IMG tage didn't
seem to invokde the PHP process). These still didn't work (and I was
trying to stay away from generating files that would be accessed by an
HTML page).

So I came to my senses and decided to re-code the Perl process in PHP.
Only slight problem is needing to learn PHP -- but I needed to do that
anyhow. Also, the code is very similar to what I already had in Perl.
PHP seems like Perl for VB programmers.
 
H

Helgi Briem

Is it possible to embed some PHP script in an HTML stream being
returned from a Perl script? I know it sounds wacky, but I'd like
to use chart library that uses PHP's support for the GD library.
PHP works on the system (I know .php files in the html
folder are served fine, so PHP is running.)

??? Why not simply use GD directly from Perl?
Far easier, I would have thought.

--
Helgi Briem hbriem AT simnet DOT is

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
D

Dave F

Helgi said:
??? Why not simply use GD directly from Perl?
Far easier, I would have thought.

--
Helgi Briem hbriem AT simnet DOT is

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Because, as Sherm Pendley pointed out, I was confused and thought that
the PHP library I was using would allow me to stream an image from the
script area (like an applet). This way I would avoid the issue of
creating graphic files on the server that would either need to be
created using
unique names (and periodically cleaned up) or created with the same name
-- which I was worried could create problems if two charts were being
created at the same time. I have used the Perl/GD solution in the past
to create charts, which are then shown with <IMG> in an HTML page (and
it works well). I have also used the KavaChart Java applet, which works
well.

I may still use this PHP library, even though I will have the same file
name or multi-user issues with the PHP script files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,771
Messages
2,569,587
Members
45,097
Latest member
RayE496148
Top