0

Let's say I have two websites. A Java web app running at www.server100.com, and a PHP web app running at www.server200.com.

Let's say I have a servlet http://www.server100.com/webapp1/getImageServlet that returns the following HTML content, where the filename in the html (ABC123.jpg) is a different filename for every hour of the day:

<div id="dynamicImage">
  <img src="http://www.server100.com/ABC123.jpg">
</div>

Now, let's say I have a PHP file here: http://www.server200.com/test1.php. How do I include the HTML that results from the servlet in my PHP file?

I'm thinking I want to do something like ...

<?php
  Print "<html><body>";
  Print "Hi!  Let's see this hour's image!";
  include "http://www.server100.com/webapp1/getImageServlet";
  Print "</body></html>";
?>

Any ideas are greatly appreciated! And would it simplify things if the Java app and the PHP app were running on the same server? Thanks!

  • I might not understand question well, but PHP file can obtain also HTML just not in `?>` tags, or in case you can `echo()` HTML out `AHOY'?>` anyway what is the output of `http://www.server100.com/webapp1/getImageServlet` ? – Kyslik Sep 20 '13 at 00:54
  • Pretty new to PHP, thanks for the help. The .../getImageServlet returns the
    block.
    –  Sep 20 '13 at 01:02
  • also please use `echo 'string';` or `echo $variable;` instead of `print()` see thread about it [here](http://stackoverflow.com/q/7094118/1564365). – Kyslik Sep 20 '13 at 01:13

2 Answers2

0

I think I have figured it out ...

Print file_get_contents("http://www.server100.com/webapp1/getImageServlet");

... seems to do what I need.

  • oh nice, you got it yourself ;) also note that `print()` is from Perl, you might take a look at [echo](http://php.net/manual/en/function.echo.php) – Kyslik Sep 20 '13 at 01:05
0
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title</title>
    </head>
    <body>
    <?php 

    //your PHP goes here
        file_get_contents("http://www.server100.com/webapp1/getImageServlet");
    ?>    
    </body>
</html>

save this as whatever.php


if your servlet outputs HTML just do this

<?php

$handle = fopen("http://www.server100.com/webapp1/getImageServlet", "r"); 

$contents = '';

while (!feof($handle)) {

 $contents .= fread($handle, 8192);

}

fclose($handle);

echo $contents; //printing it all out

?>

also echoing file_get_contents() should work just fine

Kyslik
  • 8,217
  • 5
  • 54
  • 87
  • 1
    Thanks! What about the `file_get_contents` code I posted in my answer below. Does that do the same thing as you `fopen`? Or are there problems with mine? Thanks! –  Sep 20 '13 at 01:04
  • I think it is just fine. I didn't get question right at the first. And you answered while I was adding/editing mine answer. – Kyslik Sep 20 '13 at 01:06