How on earth do I get the HTML output from a PHP script? Basically I’m passing an argument to a page and expect to just get the HTML output. I’ve tried using CURL and file_get_contents() and I’m getting the actual PHP, not the HTML. The files I want to load in to the script are PHP and in the same directory. Seems so simple but I’ll be damned if I can get it to work!
Use output buffering. Call ob_start() before you start outputting HTML and ob_get_contents() to assign the output to a variable which you can use. If you don’t want to output the contents, you can use ob_get_clean(). Remember, you must flush the output buffer if you want to output content after starting buffering. The documentation linked to above offers a good explanation.
Sorry, I don’t think I explained very well. I’m already getting the data in to a variable, it’s just that when I echo it I see PHP code, so the file was loaded as raw PHP, rather than HTML output
$data = ob_get_contents();
Is one approach. If that isn’t a viable solution, use:
$data = file_get_contents(“http://www.myserver.com/path/to/thefile.php”);
No, including is a definite no, it will not work. Using the full domain name is an approach I hadn’t considered though. Seemed pointless when the two files are in the same directory, but it’s a worth trying. I hope the .htaccess security won’t cause a problem!
Because of the nature of this page; it won’t work. All that is happening is that the script that I’m calling is checking all other PHP files in the same directory to see whether they are part of an undefined collection or not. I don’t want to include them all, that would be silly. If they are a part of the collection then a link is generated and displayed, if they’re not then they’re ignored. The script has to assume that new files will be added that it won’t know about in the future. There is no database to query so we can’t retrieve a list of installed files from there either. So, what I’ve done is coded in a simple flag at the top of each of the files that belong to the collection and I’m just getting the first few bytes instead. New files will just need to have the same flag, which is no hardship as I’ll be managing the files. I did consider doing something with the filename but I don’t want to restrict the filenames like that as people may choose to rename them and this script needs to handle it.
In hindsight, calling each page via a normal HTTP request would’ve been bad too as each page would’ve executed. My plan was to pass an argument via the URL that would have it echo a string that would tell me that it belongs to this collection and then die, but the it occurred to me that other scripts that aren’t part of the collection would all execute too, and not knowing what they are, that could be tragic.
So, the moral of the story is to think things through before you start writing something!
Could you not organise them in folders to avoid this? Or perhaps have a file that maintains a list of files in the collection?