2

I'm using curl in PHP to return the content of a PHP file. I want to do this locally because I will be accessing multiple PHP files during the same script, so it would be faster to open the file directly.

However, I want to be able to push parameters into the PHP files (treat them exactly as PHP files on the web, but grabbing them locally), as I want to push parameters into the scripts which will be generating additional dynamic content when I grab it.

Is this possible to do if I call the files locally? I've tried using the file:///, calling the file directly, but this won't run the PHP code found in these files.

Any ideas?

edit

Sorry for the confusion: -This is currently running on a webserver, and I am currently calling http:// (and not file:///) so the PHP contained in those files can be executed. However, I find this to be slow because I'm generating multiple curl() calls that are essentially calling the server itself multiple times.

Prusprus
  • 7,987
  • 9
  • 42
  • 57
  • use `http://` ? like `http://localhost/file.php` ? – Peter Jan 23 '13 at 20:29
  • 3
    What is the reason to not just use `require` / `include`? – hank Jan 23 '13 at 20:30
  • You would have to accesses it like you would through the webserver. There is however, rarely a good reason to do this. If you have the files locally, why not include the directly? – datasage Jan 23 '13 at 20:30
  • @Peter, I'm currently doing this, but it's slow, because it's essentially establishing a new connection for every page (via curl), no? – Prusprus Jan 23 '13 at 20:32
  • I'm using curl because I need to save the content of each PHP file in variable, as I'm pushing them in a PDF generator. The idea is to have a library of .PHP files that contain the static content of pages to be pushed to the PDF, with a couple of dynamic strings passed along to them from my main script calling the curls. – Prusprus Jan 23 '13 at 20:33
  • This is what we have functions for.. – hank Jan 23 '13 at 20:35
  • If you are going to use CURL, you need to send everything via an http connection. Which essentially means that you will create a new connection/request for every curl include. – datasage Jan 23 '13 at 20:35
  • You can use include and control the output with output buffering. You may need to fix some variables that are required by the script, but it is a preferable way to capture the output. – datasage Jan 23 '13 at 20:36

2 Answers2

4

you can do trick like:

function php_to_string($php_file, $new_GET = false, $new_POST = false) {
    // replacing $_GET, $_POST if necessary
    if($new_GET) {
        $old_GET = $_GET;            
        $_GET = $new_GET;
    }
    if($new_POST) {
        $old_POST = $_POST;            
        $_POST = $new_POST;
    }
    ob_start();
    include($php_file);
    // restoring $_GET, $_POST if necessary
    if(isset($old_GET)) {
       $_GET = $old_GET;
    }
    if(isset($old_POST)) {
       $_POST = $old_POST;
    }
    return ob_get_clean();
}

$content = php_to_string('my_file.php');
$content = php_to_string('my_file.php', Array('id'=>23)); // http://localhost/my_fie.php?id=23

But please mind it may overwrite your existing variables, causing bugs (for example duplicate defines) etc. so you may use sandbox solution

Community
  • 1
  • 1
Peter
  • 16,453
  • 8
  • 51
  • 77
1

I believe you would want to set up a web server (e.g. Apache) on your local machine so you can go to http://localhost/script.php?param1=foo&param2=bar instead of file:///path/to/script.php. The difference is that when you do file:///, the files are just opened, but if you go through Apache, the scripts are actually executed.

As for passing arguments to your scripts, use the query string for that (e.g. ?param1=foo, etc.).

I don't know why you're doing what you're doing, but hopefully that helps you do it.

Jason Swett
  • 43,526
  • 67
  • 220
  • 351
  • Thanks, I'm actually running this on a web server (sorry didn't mention this). I'm currently running is as http://, but this is generally slow because I'm calling multiple PHP pages in order to get their content one by one. Essentially I'm looking for the speed of file:/// while still getting the execution and parameterization of http. – Prusprus Jan 23 '13 at 20:35