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.

5
  • 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: php.net/manual/en/book.curl.php Commented Apr 25, 2017 at 8:48
  • have a look at this stackoverflow.com/questions/819182/… Commented Apr 25, 2017 at 8:51
  • @arkascha A domain path won't resolve internally, only an absolute path. Thanks. Commented Apr 25, 2017 at 9:16
  • @Thu thanks, for external domains it's very easy. It's internal that's the issue. Commented Apr 25, 2017 at 9: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. Commented Apr 25, 2017 at 9:52

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but experiencing exactly the same issue as when using file_get_contents. A domain path won't resolve internally, only an absolute path.
Thanks. Having the same issue as the ob_get_contents in my question where a query string causes it to not find the file.
What query string are you talking about exactly? From $_GET?
For example: '/dir1/dir2/dir3/dir4/myScript.php' works fine. But '/dir1/dir2/dir3/dir4/myScript.php?foo=bar' fails to find the file.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.