0

I have a php page that outputs html to the browser based on a query string that is parsed. The issue I am having is that I need to retrieve this html source code dynamically via php.

The following code will not work because it tries to resolve an absolute path as it's on the same server:

$url = 'http://example.com/myScript.php';
$html = file_get_contents($url);

If I manually set the absolute path it just returns the php contents as text (not executed like a browser would do):

$url = '/dir1/dir2/dir3/dir4/myScript.php';
$html = file_get_contents($url);

I then researched it and found that using ob_get_contents could work. The code below works as expected, executing the script and returning the html output.

$url = '/dir1/dir2/dir3/dir4/myScript.php';
ob_start();
include($url);
$html = ob_get_contents();
ob_end_clean();

The problem with the above solution is that as soon as I put the query string on the end it fails. I think this is because it's treating the query string as part of the file name.

steve
  • 471
  • 6
  • 15
  • The first example should work or throw a specific error if blocked in the configuration, not sure what you actually observe. As an alternative you can take a look at php's `cURL` extension: http://php.net/manual/en/book.curl.php – arkascha Apr 25 '17 at 08:48
  • have a look at this http://stackoverflow.com/questions/819182/how-do-i-get-the-html-code-of-a-web-page-in-php – Barcenal Apr 25 '17 at 08:51
  • @arkascha A domain path won't resolve internally, only an absolute path. Thanks. – steve Apr 25 '17 at 09:16
  • @Thu thanks, for external domains it's very easy. It's internal that's the issue. – steve Apr 25 '17 at 09:17
  • @steve Sorry, but that is not true. Certainly a hostname _will_ get resolved on any system that has a working name resolution. And obviously the correct entries for the domain have to be set in the domain name system (DNS). This most likely is the issue you case. But that is not a php thing, you have to configure your name resolution! And that certainly is the correct approach. Do not try to get around that with questionable work arounds. – arkascha Apr 25 '17 at 09:52

1 Answers1

4

Use PHPs ob_get_contents

<?php
    ob_start();

    $original_get = $_GET;
    $_GET = ["query" => "tags", "you" => "need", "in" => "file.php"];
    $file = "file.php";

    include($file);

    $_GET = $original_get;

    $content = ob_get_contents();
    ob_clean();

    echo $content;
b0ne
  • 653
  • 3
  • 10
  • Thanks, but experiencing exactly the same issue as when using `file_get_contents`. A domain path won't resolve internally, only an absolute path. – steve Apr 25 '17 at 09:16
  • Thanks. Having the same issue as the `ob_get_contents` in my question where a query string causes it to not find the file. – steve Apr 25 '17 at 09:46
  • What query string are you talking about exactly? From $_GET? – b0ne Apr 25 '17 at 09:49
  • For example: `'/dir1/dir2/dir3/dir4/myScript.php'` works fine. But `'/dir1/dir2/dir3/dir4/myScript.php?foo=bar'` fails to find the file. – steve Apr 25 '17 at 09:53
  • 1
    Of course it fails because it's a file path not an URL. Try the code now, just change line 5 with the query tags you need in your included file. – b0ne Apr 25 '17 at 10:04