Prusprus
Prusprus

Reputation: 8065

Get .php file locally using curl

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.

Upvotes: 2

Views: 6407

Answers (2)

Peter
Peter

Reputation: 16923

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

Upvotes: 4

Jason Swett
Jason Swett

Reputation: 45094

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.

Upvotes: 1

Related Questions