Reputation:
I'd like to avoid doing any needless HTTP requests on my site to improve performance. So, I'd like to include a local PHP on my server without using cURL. The problem is that the PHP I'm calling expects some parameters to be passed via HTTP GET. I'd like to keep it that way since I also want to be able to access those PHP files from other places.
What is the best way of doing this? Can I access the output of a local PHP file while giving it GET parameters?
Upvotes: 0
Views: 1832
Reputation: 191
Assuming you're on a linux box, you could exec the script and pick up the output with something like:
exec ("php myPhpFile.php arg1 arg2 arg3 > myPhpOutput.html");
then open the outputted file for read.
For this to work however (and it is ugly!) you would need to modify myPhpFile and implement a method to test for $_GET paramaters. If it doesn't find them then load the properties using argv
Improvement (I was thinking lazily :))
shell_exec will return the output as a string. so
$filecontents = shell_exec("php yourfile.php arg1 arg2 arg3...");
will return the processed output of a php file.
Upvotes: 2
Reputation: 151738
I'd like to avoid doing any needless HTTP requests on my site to improve performance
include
also makes an HTTP request when you supply an HTTP URI to it, so instead of the client's asynchronous include (for example using AJAX or an iframe) you do it synchronously on the server, therefore making the page load even slower.
So while the include may work as intended (you want to include the output, right?), it will definitely not speed up your site.
the PHP I'm calling expects some parameters to be passed via HTTP GET. I'd like to keep it that way since I also want to be able to access those PHP files from other places.
Then alter those files and set the appropriate variables before including them. Or even better, refactor it into functions. For example, if the file you want to include looks like this:
$username = $_GET['username'];
print "User: $username";
Then refactor into a function and store in a separate file:
function PrintUsername($username)
{
print "User: $username";
}
And call it appropriately:
include('printusername.php');
PrintUsername($_GET['username']);
(You might want to throw in an isset()
here and there.) Now you can alter your code that also needs this output:
include('printusername.php');
PrintUsername($someOtherVariable);
Now you don't have to rely on URL or $_GET
or include
magic, but simply use all functions as they're meant to.
Upvotes: 3
Reputation: 16462
This is ugly, but works.
$_GET['foo'] = 1;
$_GET['bar'] = 2;
include '/path/to/file.php';
Upvotes: 0